Skip to content

Commit 3e136d0

Browse files
authored
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
1 parent 5b01b4e commit 3e136d0

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/main/kotlin/translations/identification/TranslationIdentifier.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,21 @@ import com.demonwav.mcdev.util.constantStringValue
3131
import com.demonwav.mcdev.util.constantValue
3232
import com.demonwav.mcdev.util.descriptor
3333
import com.demonwav.mcdev.util.findModule
34+
import com.demonwav.mcdev.util.mapToArray
3435
import com.demonwav.mcdev.util.referencedMethod
35-
import com.demonwav.mcdev.util.toTypedArray
3636
import com.intellij.codeInsight.AnnotationUtil
3737
import com.intellij.codeInsight.completion.CompletionUtilCore
3838
import com.intellij.codeInspection.dataFlow.CommonDataflow
3939
import com.intellij.openapi.project.Project
4040
import com.intellij.openapi.util.RecursionManager
4141
import com.intellij.psi.CommonClassNames
4242
import com.intellij.psi.JavaPsiFacade
43+
import com.intellij.psi.PsiArrayType
4344
import com.intellij.psi.PsiElement
4445
import com.intellij.psi.PsiEllipsisType
4546
import com.intellij.psi.PsiExpression
4647
import com.intellij.psi.PsiParameter
47-
import com.intellij.psi.PsiType
48+
import com.intellij.psi.PsiPrimitiveType
4849
import java.util.IllegalFormatException
4950
import java.util.MissingFormatArgumentException
5051
import org.jetbrains.uast.UCallExpression
@@ -54,7 +55,7 @@ import org.jetbrains.uast.UMethod
5455
import org.jetbrains.uast.UQualifiedReferenceExpression
5556
import org.jetbrains.uast.evaluateString
5657
import org.jetbrains.uast.getContainingUClass
57-
import org.jetbrains.uast.util.isArrayInitializer
58+
import org.jetbrains.uast.util.isNewArrayWithInitializer
5859

5960
object TranslationIdentifier {
6061
fun identify(
@@ -203,24 +204,19 @@ object TranslationIdentifier {
203204
}
204205

205206
val elements = args.drop(index)
206-
return extractVarArgs(psiParam.type, elements)
207+
return extractVarArgs(elements)?.mapToArray { it.paramDisplayString() }
207208
}
208209

209-
private fun extractVarArgs(type: PsiType, elements: List<UExpression>): Array<String>? {
210-
return if (elements[0].getExpressionType() == type) {
211-
val initializer = elements[0]
212-
if (initializer is UCallExpression && initializer.isArrayInitializer()) {
213-
// We're dealing with an array initializer, let's analyse it!
214-
initializer.valueArguments
215-
.asSequence()
216-
.map { it.paramDisplayString() }
217-
.toTypedArray()
218-
} else {
219-
// We're dealing with a more complex expression that results in an array, give up
220-
return null
221-
}
210+
private fun extractVarArgs(elements: List<UExpression>): List<UExpression>? {
211+
val arg = elements.singleOrNull() ?: return elements
212+
val arrayType = arg.getExpressionType() as? PsiArrayType ?: return elements
213+
if (arrayType.componentType is PsiPrimitiveType) return elements
214+
return if (arg is UCallExpression && arg.isNewArrayWithInitializer()) {
215+
// We're dealing with an array initializer, let's use its elements!
216+
arg.valueArguments
222217
} else {
223-
elements.asSequence().map { it.paramDisplayString() }.toTypedArray()
218+
// We're dealing with a more complex expression that results in an array, give up
219+
null
224220
}
225221
}
226222

0 commit comments

Comments
 (0)