Skip to content

Commit

Permalink
Fix #2325 Make lang annotator fixes bulk compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
RedNesto committed Jul 6, 2024
1 parent f798d6d commit caca374
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,32 @@ package com.demonwav.mcdev.translations.intentions
import com.demonwav.mcdev.translations.Translation
import com.demonwav.mcdev.translations.TranslationFiles
import com.demonwav.mcdev.translations.index.TranslationInverseIndex
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.search.GlobalSearchScope

class RemoveDuplicatesIntention(private val translation: Translation) : PsiElementBaseIntentionAction() {
class RemoveDuplicatesIntention(
private val translation: Translation,
element: PsiElement
) : LocalQuickFixAndIntentionActionOnPsiElement(element) {
override fun getText() = "Remove duplicates (keep this translation)"

override fun getFamilyName() = "Minecraft localization"

override fun isAvailable(project: Project, editor: Editor?, element: PsiElement) = true

override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
val keep = TranslationFiles.seekTranslation(element) ?: return
override fun invoke(
project: Project,
file: PsiFile,
editor: Editor?,
startElement: PsiElement,
endElement: PsiElement
) {
val keep = TranslationFiles.seekTranslation(startElement) ?: return
val entries = TranslationInverseIndex.findElements(
translation.key,
GlobalSearchScope.fileScope(element.containingFile),
GlobalSearchScope.fileScope(file),
)
for (other in entries) {
if (other !== keep) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@
package com.demonwav.mcdev.translations.intentions

import com.demonwav.mcdev.translations.TranslationFiles
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.util.IncorrectOperationException
import com.intellij.psi.PsiFile

class RemoveUnmatchedEntryIntention : PsiElementBaseIntentionAction() {
class RemoveUnmatchedEntryIntention(element: PsiElement) : LocalQuickFixAndIntentionActionOnPsiElement(element) {
override fun getText() = "Remove translation"

override fun isAvailable(project: Project, editor: Editor, element: PsiElement) = true

override fun getFamilyName() = "Minecraft"

@Throws(IncorrectOperationException::class)
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
TranslationFiles.remove(TranslationFiles.seekTranslation(element) ?: return)
override fun invoke(
project: Project,
file: PsiFile,
editor: Editor?,
startElement: PsiElement,
endElement: PsiElement
) {
TranslationFiles.remove(TranslationFiles.seekTranslation(startElement) ?: return)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TranslationFileAnnotator : Annotator {
if (translation.key != translation.trimmedKey) {
annotations.newAnnotation(HighlightSeverity.WARNING, "Translation key contains whitespace at start or end.")
.range(element)
.newFix(TrimKeyIntention()).registerFix()
.newFix(TrimKeyIntention(element)).universal().registerFix()
.create()
}
}
Expand All @@ -58,7 +58,7 @@ class TranslationFileAnnotator : Annotator {
val count = TranslationIndex.getTranslations(element.containingFile).count { it.key == translation.key }
if (count > 1) {
annotations.newAnnotation(HighlightSeverity.WARNING, "Duplicate translation keys \"${translation.key}\".")
.newFix(RemoveDuplicatesIntention(translation)).registerFix()
.newFix(RemoveDuplicatesIntention(translation, element)).universal().registerFix()
.create()
}
}
Expand All @@ -71,7 +71,7 @@ class TranslationFileAnnotator : Annotator {
}
val warningText = "Translation key not included in default localization file."
annotations.newAnnotation(HighlightSeverity.WARNING, warningText)
.newFix(RemoveUnmatchedEntryIntention()).registerFix()
.newFix(RemoveUnmatchedEntryIntention(element)).universal().registerFix()
.create()
}
}
27 changes: 19 additions & 8 deletions src/main/kotlin/translations/intentions/TrimKeyIntention.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,39 @@ package com.demonwav.mcdev.translations.intentions

import com.demonwav.mcdev.translations.TranslationFiles
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.util.IncorrectOperationException
import com.intellij.psi.PsiFile

class TrimKeyIntention : PsiElementBaseIntentionAction() {
class TrimKeyIntention(element: PsiElement) : LocalQuickFixAndIntentionActionOnPsiElement(element) {
override fun getText() = "Trim translation key"

override fun getFamilyName() = "Minecraft"

override fun isAvailable(project: Project, editor: Editor, element: PsiElement): Boolean {
override fun isAvailable(
project: Project,
file: PsiFile,
editor: Editor?,
startElement: PsiElement,
endElement: PsiElement
): Boolean {
val translation = TranslationFiles.toTranslation(
TranslationFiles.seekTranslation(element) ?: return false,
TranslationFiles.seekTranslation(startElement) ?: return false,
) ?: return false

return translation.key != translation.trimmedKey
}

@Throws(IncorrectOperationException::class)
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
val entry = TranslationFiles.seekTranslation(element) ?: return
override fun invoke(
project: Project,
file: PsiFile,
editor: Editor?,
startElement: PsiElement,
endElement: PsiElement
) {
val entry = TranslationFiles.seekTranslation(startElement) ?: return
if (!FileModificationService.getInstance().preparePsiElementForWrite(entry)) {
return
}
Expand Down

0 comments on commit caca374

Please sign in to comment.