Skip to content

Commit

Permalink
Re-add prefix and suffix support
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Sep 21, 2023
1 parent c609766 commit e3ecfb7
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ class LiteralTranslationIdentifier : TranslationIdentifier<PsiLiteralExpression>
if (element.value is String) {
val result = identify(element.project, element, statement, element)
return result?.copy(
key = result.key.replace(
CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED,
"",
key = result.key.copy(
infix = result.key.infix.replace(
CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED,
"",
),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class ReferenceTranslationIdentifier : TranslationIdentifier<PsiReferenceExpress
val result = identify(element.project, element, statement, referenceElement)

return result?.copy(
key = result.key.replace(
CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED,
"",
key = result.key.copy(
infix = result.key.infix.replace(
CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED,
"",
),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class TranslationIdentifier<T : PsiElement> {
foldingElement,
function.matchedIndex,
referenceElement,
translationKey,
TranslationInstance.Key(translationKey),
formatted,
if (superfluousParams >= 0) FormattingError.SUPERFLUOUS else null,
superfluousParams,
Expand All @@ -81,7 +81,7 @@ abstract class TranslationIdentifier<T : PsiElement> {
foldingElement,
function.matchedIndex,
referenceElement,
translationKey,
TranslationInstance.Key(translationKey),
translation,
FormattingError.MISSING,
)
Expand All @@ -91,7 +91,7 @@ abstract class TranslationIdentifier<T : PsiElement> {
null,
function.matchedIndex,
referenceElement,
translationKey,
TranslationInstance.Key(translationKey),
null,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ data class TranslationInstance(
val foldingElement: PsiElement?,
val foldStart: Int,
val referenceElement: PsiElement?,
val key: String,
val key: Key,
val text: String?,
val formattingError: FormattingError? = null,
val superfluousVarargStart: Int = -1,
) {
data class Key(val prefix: String, val infix: String, val suffix: String) {
constructor(infix: String) : this("", infix, "")

val full = (prefix + infix + suffix).trim()
}

companion object {
enum class FormattingError {
MISSING, SUPERFLUOUS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,21 @@ class ChangeTranslationQuickFix(private val name: String) : LocalQuickFix {
val key = LiteralTranslationIdentifier().identify(literal)?.key ?: return
val popup = ChooseByNamePopup.createPopup(
project,
TranslationGotoModel(project),
TranslationGotoModel(project, key.prefix, key.suffix),
null,
)
popup.invoke(
object : ChooseByNamePopupComponent.Callback() {
override fun elementChosen(element: Any) {
val selectedKey = (element as PsiNamedElement).name ?: return
literal.containingFile.runWriteAction {
val insertion = selectedKey.substring(
key.prefix.length,
selectedKey.length - key.suffix.length,
)
literal.replace(
JavaPsiFacade.getInstance(project).elementFactory.createExpressionFromText(
"\"$selectedKey\"",
"\"$insertion\"",
literal.context,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class NoTranslationInspection : TranslationInspection() {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
try {
val literal = descriptor.psiElement as PsiLiteralExpression
val key = literal.value as String
val translation = LiteralTranslationIdentifier().identify(literal)
val literalValue = literal.value as String
val key = translation?.key?.copy(infix = literalValue)?.full ?: literalValue
val result = Messages.showInputDialog(
"Enter default value for \"$key\":",
"Create Translation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.intellij.psi.PsiNamedElement
import com.intellij.util.indexing.FindSymbolParameters
import java.util.TreeSet

class TranslationGotoModel(project: Project) :
class TranslationGotoModel(project: Project, private val prefix: String, private val suffix: String) :
ContributorsBasedGotoByModel(
project,
arrayOf(
Expand All @@ -54,7 +54,12 @@ class TranslationGotoModel(project: Project) :
val result = TreeSet<PsiNamedElement> { o1, o2 ->
(o1 as PsiNamedElement).name?.compareTo((o2 as PsiNamedElement).name ?: return@TreeSet -1) ?: -1
}
result.addAll(superResult.map { it as PsiNamedElement },)
result.addAll(
superResult.map { it as PsiNamedElement }.filter {
val key = it.name ?: return@filter false
key.startsWith(prefix) && key.endsWith(suffix)
},
)
return result.toArray()
}

Expand Down
15 changes: 9 additions & 6 deletions src/main/kotlin/translations/reference/TranslationReference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.demonwav.mcdev.translations.reference
import com.demonwav.mcdev.asset.PlatformAssets
import com.demonwav.mcdev.translations.TranslationConstants
import com.demonwav.mcdev.translations.TranslationFiles
import com.demonwav.mcdev.translations.identification.TranslationInstance
import com.demonwav.mcdev.translations.index.TranslationIndex
import com.demonwav.mcdev.translations.index.TranslationInverseIndex
import com.demonwav.mcdev.translations.lang.gen.psi.LangEntry
Expand All @@ -43,7 +44,7 @@ import com.intellij.util.IncorrectOperationException
class TranslationReference(
element: PsiElement,
textRange: TextRange,
val key: String,
val key: TranslationInstance.Key,
private val renameHandler: (element: PsiElement, range: TextRange, newName: String) -> PsiElement =
{ elem, range, newName ->
ElementManipulators.getManipulator(elem).handleContentChange(elem, range, newName)!!
Expand All @@ -52,7 +53,7 @@ class TranslationReference(
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
val project = myElement.project
val entries = TranslationInverseIndex.findElements(
key,
key.full,
GlobalSearchScope.allScope(project),
TranslationConstants.DEFAULT_LOCALE,
)
Expand All @@ -62,11 +63,13 @@ class TranslationReference(
override fun getVariants(): Array<Any?> {
val project = myElement.project
val defaultTranslations = TranslationIndex.getAllDefaultTranslations(project)
val pattern = Regex("${Regex.escape(key.prefix)}(.*?)${Regex.escape(key.suffix)}")
return defaultTranslations
.filter { it.key.isNotEmpty() }
.map { entry ->
.mapNotNull { entry -> pattern.matchEntire(entry.key)?.let { entry to it } }
.map { (entry, match) ->
LookupElementBuilder
.create(entry.key)
.create(if (match.groups.size <= 1) entry.key else match.groupValues[1])
.withIcon(PlatformAssets.MINECRAFT_ICON)
.withTypeText(TranslationConstants.DEFAULT_LOCALE)
.withPresentableText(entry.key)
Expand All @@ -84,7 +87,7 @@ class TranslationReference(
return false
}

return (element is LangEntry && element.key == key) ||
(element is JsonProperty && element.name == key)
return (element is LangEntry && element.key == key.full) ||
(element is JsonProperty && element.name == key.full)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class TranslationReferenceSearch : QueryExecutor<PsiReference, ReferencesSearch.
val highlighted = it.file?.findElementAt(it.rangeInElement!!.startOffset)
val ref = highlighted?.parent?.references
?.find { ref -> ref is TranslationReference } as TranslationReference?
if (ref?.key == key) {
if (ref?.key?.full == key) {
consumer.process(ref)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/translations/reference/contributors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package com.demonwav.mcdev.translations.reference

import com.demonwav.mcdev.translations.TranslationFiles
import com.demonwav.mcdev.translations.identification.TranslationIdentifier
import com.demonwav.mcdev.translations.identification.TranslationInstance
import com.demonwav.mcdev.translations.lang.gen.psi.LangEntry
import com.demonwav.mcdev.translations.lang.gen.psi.LangTypes
import com.intellij.json.JsonElementTypes
Expand Down Expand Up @@ -85,7 +86,7 @@ class JsonReferenceContributor : PsiReferenceContributor() {
TranslationReference(
element,
nameTextRange.shiftRight(1).grown(-2),
entry.name,
TranslationInstance.Key("", entry.name, ""),
) { elem, _, newName ->
(elem as JsonProperty).setName(newName)
},
Expand All @@ -110,7 +111,7 @@ class LangReferenceContributor : PsiReferenceContributor() {
TranslationReference(
element,
TextRange(0, entry.key.length),
entry.key,
TranslationInstance.Key("", entry.key, ""),
),
)
}
Expand Down

0 comments on commit e3ecfb7

Please sign in to comment.