User: rednesto Date: 10 Jul 24 12:25 Revision: 389e45d411241fcf2f8bc52ede8656c48c9e59c5 Summary: Translation: TranslationIdentifier instances cleanup Also support instance final fields for reference identifiers TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=9394&personal=false Index: src/main/kotlin/translations/identification/LiteralTranslationIdentifier.kt =================================================================== --- src/main/kotlin/translations/identification/LiteralTranslationIdentifier.kt (revision 3a2175f12ed5a6843df98363bce0c011e3e1b1c0) +++ src/main/kotlin/translations/identification/LiteralTranslationIdentifier.kt (revision 389e45d411241fcf2f8bc52ede8656c48c9e59c5) @@ -25,22 +25,16 @@ class LiteralTranslationIdentifier : TranslationIdentifier() { override fun identify(element: ULiteralExpression): TranslationInstance? { - val statement = element.uastParent - if (statement != null && element.value is String) { - val project = element.sourcePsi?.project - ?: return null - val result = identify(project, element, statement, element) - return result?.copy( - key = result.key.copy( - infix = result.key.infix.replace( - CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED, - "", - ), - ), - ) - } + val statement = element.uastParent ?: return null + if (element.value !is String) { - return null - } + return null + } + val project = element.sourcePsi?.project ?: return null + val result = identify(project, element, statement, element) ?: return null + val infix = result.key.infix.replace(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED, "") + return result.copy(key = result.key.copy(infix = infix)) + } + override fun elementClass(): Class = ULiteralExpression::class.java } Index: src/main/kotlin/translations/identification/ReferenceTranslationIdentifier.kt =================================================================== --- src/main/kotlin/translations/identification/ReferenceTranslationIdentifier.kt (revision 3a2175f12ed5a6843df98363bce0c011e3e1b1c0) +++ src/main/kotlin/translations/identification/ReferenceTranslationIdentifier.kt (revision 389e45d411241fcf2f8bc52ede8656c48c9e59c5) @@ -21,44 +21,35 @@ package com.demonwav.mcdev.translations.identification import com.intellij.codeInsight.completion.CompletionUtilCore -import com.intellij.psi.JavaPsiFacade -import com.intellij.psi.impl.source.PsiClassReferenceType -import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.uast.UField +import com.intellij.psi.PsiManager +import com.intellij.psi.PsiType import org.jetbrains.uast.ULiteralExpression import org.jetbrains.uast.UReferenceExpression +import org.jetbrains.uast.UVariable import org.jetbrains.uast.resolveToUElement class ReferenceTranslationIdentifier : TranslationIdentifier() { override fun identify(element: UReferenceExpression): TranslationInstance? { - val reference = element.resolveToUElement() ?: return null val statement = element.uastParent ?: return null val project = element.sourcePsi?.project ?: return null - - if (reference is UField) { - val scope = GlobalSearchScope.allScope(project) - val stringClass = - JavaPsiFacade.getInstance(project).findClass("java.lang.String", scope) ?: return null - val isConstant = reference.isStatic && reference.isFinal - val type = reference.type as? PsiClassReferenceType ?: return null - val resolved = type.resolve() ?: return null - if (isConstant && (resolved.isEquivalentTo(stringClass) || resolved.isInheritor(stringClass, true))) { - val referenceElement = reference.uastInitializer as? ULiteralExpression ?: return null - val result = identify(project, element, statement, referenceElement) - - return result?.copy( - key = result.key.copy( - infix = result.key.infix.replace( - CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED, - "", - ), - ), - ) + val reference = element.resolveToUElement() as? UVariable ?: return null + if (!reference.isFinal) { + return null - } + } - } + val resolveScope = element.sourcePsi?.resolveScope ?: return null + val psiManager = PsiManager.getInstance(project) + val stringType = PsiType.getJavaLangString(psiManager, resolveScope) + if (!stringType.isAssignableFrom(reference.type)) { - return null - } + return null + } + val referenceElement = reference.uastInitializer as? ULiteralExpression ?: return null + val result = identify(project, element, statement, referenceElement) ?: return null + + val infix = result.key.infix.replace(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED, "") + return result.copy(key = result.key.copy(infix = infix)) + } + override fun elementClass(): Class = UReferenceExpression::class.java }