[kotlin] Fix language injection for vararg parameter

#KTIJ-10950 Fixed

GitOrigin-RevId: ecdbcf4c7ce2dafec073c4b21d06eed1c9f65348
This commit is contained in:
Thunderblade73
2024-10-12 21:51:25 +02:00
committed by intellij-monorepo-bot
parent 87e1ebdb33
commit b7a27fe7c3
5 changed files with 45 additions and 5 deletions

View File

@@ -180,6 +180,11 @@ public abstract class K2HighlightingMetaInfoTestGenerated extends AbstractK2High
runTest("../../idea/tests/testData/highlighterMetaInfo/jvm/KotlinSimpleInjection.kt");
}
@TestMetadata("KotlinVarargsParametersInjection.kt")
public void testKotlinVarargsParametersInjection() throws Exception {
runTest("../../idea/tests/testData/highlighterMetaInfo/jvm/KotlinVarargsParametersInjection.kt");
}
@TestMetadata("NonExistingKotlinMethodFromJava.kt")
public void testNonExistingKotlinMethodFromJava() throws Exception {
runTest("../../idea/tests/testData/highlighterMetaInfo/jvm/NonExistingKotlinMethodFromJava.kt");

View File

@@ -180,6 +180,11 @@ public abstract class K1HighlightingMetaInfoTestGenerated extends AbstractK1High
runTest("testData/highlighterMetaInfo/jvm/KotlinSimpleInjection.kt");
}
@TestMetadata("KotlinVarargsParametersInjection.kt")
public void testKotlinVarargsParametersInjection() throws Exception {
runTest("testData/highlighterMetaInfo/jvm/KotlinVarargsParametersInjection.kt");
}
@TestMetadata("NonExistingKotlinMethodFromJava.kt")
public void testNonExistingKotlinMethodFromJava() throws Exception {
runTest("testData/highlighterMetaInfo/jvm/NonExistingKotlinMethodFromJava.kt");

View File

@@ -0,0 +1,10 @@
// FIR_IDENTICAL
// CHECK_SYMBOL_NAMES
// HIGHLIGHTER_ATTRIBUTES_KEY
import org.intellij.lang.annotations.Language
fun foo(@Language("kotlin") vararg s: String){}
fun bar(vararg s: String, @Language("kotlin") name: String){}
val f = foo("fun foo(){}","fun foo(){}","fun foo(){}")
val b = bar("fun foo(){}","fun foo(){}","fun foo(){}", name = "fun foo(){}")

View File

@@ -0,0 +1,10 @@
// FIR_IDENTICAL
// CHECK_SYMBOL_NAMES
// HIGHLIGHTER_ATTRIBUTES_KEY
<!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_KEYWORD'")!>import<!> org.intellij.lang.annotations.<!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_ANNOTATION'")!>Language<!>
fun <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_FUNCTION_DECLARATION'")!>foo<!>(<!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_ANNOTATION'")!>@Language<!>("kotlin") <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_BUILTIN_ANNOTATION'")!>vararg<!> <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_PARAMETER'")!>s<!>: <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_CLASS'")!>String<!>){}
fun <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_FUNCTION_DECLARATION'")!>bar<!>(<!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_BUILTIN_ANNOTATION'")!>vararg<!> <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_PARAMETER'")!>s<!>: <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_CLASS'")!>String<!>, <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_ANNOTATION'")!>@Language<!>("kotlin") <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_PARAMETER'")!>name<!>: <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_CLASS'")!>String<!>){}
val <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_PACKAGE_PROPERTY'")!>f<!> = <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_PACKAGE_FUNCTION_CALL'")!>foo<!>("fun <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_FUNCTION_DECLARATION'")!>foo<!>(){}","fun <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_FUNCTION_DECLARATION'")!>foo<!>(){}","fun <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_FUNCTION_DECLARATION'")!>foo<!>(){}")
val <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_PACKAGE_PROPERTY'")!>b<!> = <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_PACKAGE_FUNCTION_CALL'")!>bar<!>("fun foo(){}","fun foo(){}","fun foo(){}", <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_NAMED_ARGUMENT'")!>name =<!> "fun <!HIGHLIGHTING("severity='SYMBOL_TYPE_SEVERITY'; highlightingTextAttributesKey='KOTLIN_FUNCTION_DECLARATION'")!>foo<!>(){}")

View File

@@ -408,12 +408,22 @@ abstract class KotlinLanguageInjectionContributorBase : LanguageInjectionContrib
private fun injectionForKotlinCall(argument: KtValueArgument, ktFunction: KtFunction, reference: PsiReference): InjectionInfo? {
val argumentName = argument.getArgumentName()?.asName
val argumentIndex = (argument.parent as KtValueArgumentList).arguments.indexOf(argument)
// Prefer using argument name if present
val ktParameter = if (argumentName != null) {
ktFunction.valueParameters.firstOrNull { it.nameAsName == argumentName }
val argumentListIndex = (argument.parent as KtValueArgumentList).arguments.indexOf(argument)
val valueParameters = ktFunction.valueParameters
val argumentIndex = if (argumentName != null) {
null
} else {
ktFunction.valueParameters.getOrNull(argumentIndex)
// Skip vararg check if name is present
(argumentListIndex downTo 0).firstNotNullOfOrNull { index ->
valueParameters.getOrNull(index)?.takeIf { it.isVarArg }?.let { index }
}
} ?: argumentListIndex
// Prefer using argument name if present
val ktParameter = if (argumentName != null) {
valueParameters.firstOrNull { it.nameAsName == argumentName }
} else {
valueParameters.getOrNull(argumentIndex)
} ?: return null
return injectionForKotlinCall(ktParameter, reference, argumentName, argumentIndex)
}