User: 7410 Date: 23 Feb 26 10:15 Revision: 3e136d02d3a85988f80f5f637f30131472a351ef Summary: Fix translation folding when the args are given with an explicit array (#2578) * Fix translation folding when the args are given with an explicit array * Don't depend on kotlin plugin & deal correctly with arrays as vararg elements TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10430&personal=false Index: src/main/kotlin/translations/identification/TranslationIdentifier.kt =================================================================== --- src/main/kotlin/translations/identification/TranslationIdentifier.kt (revision 5b01b4e8dfb1f319422859b7a365902b6768b06e) +++ src/main/kotlin/translations/identification/TranslationIdentifier.kt (revision 3e136d02d3a85988f80f5f637f30131472a351ef) @@ -31,8 +31,8 @@ import com.demonwav.mcdev.util.constantValue import com.demonwav.mcdev.util.descriptor import com.demonwav.mcdev.util.findModule +import com.demonwav.mcdev.util.mapToArray import com.demonwav.mcdev.util.referencedMethod -import com.demonwav.mcdev.util.toTypedArray import com.intellij.codeInsight.AnnotationUtil import com.intellij.codeInsight.completion.CompletionUtilCore import com.intellij.codeInspection.dataFlow.CommonDataflow @@ -40,11 +40,12 @@ import com.intellij.openapi.util.RecursionManager import com.intellij.psi.CommonClassNames import com.intellij.psi.JavaPsiFacade +import com.intellij.psi.PsiArrayType import com.intellij.psi.PsiElement import com.intellij.psi.PsiEllipsisType import com.intellij.psi.PsiExpression import com.intellij.psi.PsiParameter -import com.intellij.psi.PsiType +import com.intellij.psi.PsiPrimitiveType import java.util.IllegalFormatException import java.util.MissingFormatArgumentException import org.jetbrains.uast.UCallExpression @@ -54,7 +55,7 @@ import org.jetbrains.uast.UQualifiedReferenceExpression import org.jetbrains.uast.evaluateString import org.jetbrains.uast.getContainingUClass -import org.jetbrains.uast.util.isArrayInitializer +import org.jetbrains.uast.util.isNewArrayWithInitializer object TranslationIdentifier { fun identify( @@ -203,26 +204,21 @@ } val elements = args.drop(index) - return extractVarArgs(psiParam.type, elements) + return extractVarArgs(elements)?.mapToArray { it.paramDisplayString() } } - private fun extractVarArgs(type: PsiType, elements: List): Array? { - return if (elements[0].getExpressionType() == type) { - val initializer = elements[0] - if (initializer is UCallExpression && initializer.isArrayInitializer()) { - // We're dealing with an array initializer, let's analyse it! - initializer.valueArguments - .asSequence() - .map { it.paramDisplayString() } - .toTypedArray() + private fun extractVarArgs(elements: List): List? { + val arg = elements.singleOrNull() ?: return elements + val arrayType = arg.getExpressionType() as? PsiArrayType ?: return elements + if (arrayType.componentType is PsiPrimitiveType) return elements + return if (arg is UCallExpression && arg.isNewArrayWithInitializer()) { + // We're dealing with an array initializer, let's use its elements! + arg.valueArguments - } else { - // We're dealing with a more complex expression that results in an array, give up + } else { + // We're dealing with a more complex expression that results in an array, give up - return null + null - } + } - } else { - elements.asSequence().map { it.paramDisplayString() }.toTypedArray() - } + } - } fun UExpression.paramDisplayString(): String { val visited = mutableSetOf()