From b8eee9d001a8d28a7fb17829df6353859b7be906 Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Sat, 4 Sep 2021 08:26:06 +0000 Subject: [PATCH] KotlinHighLevelFunctionParameterInfoHandler: Properly handle vararg parameter arguments. The computation for which parameter to highlight did not account for multiple arguments for vararg parameters. GitOrigin-RevId: dce019225af47749425c80c628332a895a549a57 --- ...inHighLevelFunctionParameterInfoHandler.kt | 33 ++++++++++++++++--- .../FirParameterInfoTestGenerated.java | 30 +++++++++++++++++ .../ParameterInfoTestGenerated.java | 30 +++++++++++++++++ .../parameterInfo/functionCall/Vararg.kt | 8 +++++ .../VarargFirstArgTrailingComma.kt | 8 +++++ .../VarargMultipleArgsTrailingComma.kt | 8 +++++ .../functionCall/VarargNamedArg.kt | 9 +++++ .../functionCall/VarargSpreadArg.kt | 9 +++++ .../functionCall/VarargUpdateOnTyping.kt | 11 +++++++ 9 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/Vararg.kt create mode 100644 plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargFirstArgTrailingComma.kt create mode 100644 plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargMultipleArgsTrailingComma.kt create mode 100644 plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargNamedArg.kt create mode 100644 plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargSpreadArg.kt create mode 100644 plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargUpdateOnTyping.kt diff --git a/plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinHighLevelFunctionParameterInfoHandler.kt b/plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinHighLevelFunctionParameterInfoHandler.kt index 99cee337030a..bb85d33f99a9 100644 --- a/plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinHighLevelFunctionParameterInfoHandler.kt +++ b/plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinHighLevelFunctionParameterInfoHandler.kt @@ -191,6 +191,23 @@ abstract class KotlinHighLevelParameterInfoWithCallHandlerBase argumentToParameterIndex[arguments[currentArgumentIndex]] + afterTrailingComma -> { + // If last argument is for a vararg parameter, then the argument about to be entered at the cursor should also be + // for that same vararg parameter. + val parameterForLastArgument = argumentMapping[arguments.last()] + if (parameterForLastArgument?.isVararg == true) { + parameterToIndex[parameterForLastArgument] + } else { + null + } + } + else -> null + } + var hasTypeMismatchBeforeCurrent = false for ((index, argument) in arguments.withIndex()) { if (index >= currentArgumentIndex) break @@ -216,7 +233,8 @@ abstract class KotlinHighLevelParameterInfoWithCallHandlerBase() val text = buildString { @@ -383,6 +402,7 @@ abstract class KotlinHighLevelParameterInfoWithCallHandlerBase valueParameterCount) + val isInPositionToEnterArgument = !supportsTrailingCommas && afterTrailingComma + val tooManyArgs = allParametersUsed && (isInPositionToEnterArgument || hasUnmappedArgument) val isDisabled = tooManyArgs || hasTypeMismatchBeforeCurrent || hasUnmappedArgumentBeforeCurrent @@ -473,6 +495,7 @@ abstract class KotlinHighLevelParameterInfoWithCallHandlerBase, val isCallResolvedToCandidate: Boolean, val hasTypeMismatchBeforeCurrent: Boolean, + val highlightParameterIndex: Int?, ) data class CandidateInfo( diff --git a/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/parameterInfo/FirParameterInfoTestGenerated.java b/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/parameterInfo/FirParameterInfoTestGenerated.java index 17bea12e2cea..d0ce3ca8d992 100644 --- a/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/parameterInfo/FirParameterInfoTestGenerated.java +++ b/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/parameterInfo/FirParameterInfoTestGenerated.java @@ -436,6 +436,36 @@ public abstract class FirParameterInfoTestGenerated extends AbstractFirParameter public void testUpdateOnTyping() throws Exception { runTest("../idea/tests/testData/parameterInfo/functionCall/UpdateOnTyping.kt"); } + + @TestMetadata("Vararg.kt") + public void testVararg() throws Exception { + runTest("../idea/tests/testData/parameterInfo/functionCall/Vararg.kt"); + } + + @TestMetadata("VarargFirstArgTrailingComma.kt") + public void testVarargFirstArgTrailingComma() throws Exception { + runTest("../idea/tests/testData/parameterInfo/functionCall/VarargFirstArgTrailingComma.kt"); + } + + @TestMetadata("VarargMultipleArgsTrailingComma.kt") + public void testVarargMultipleArgsTrailingComma() throws Exception { + runTest("../idea/tests/testData/parameterInfo/functionCall/VarargMultipleArgsTrailingComma.kt"); + } + + @TestMetadata("VarargNamedArg.kt") + public void testVarargNamedArg() throws Exception { + runTest("../idea/tests/testData/parameterInfo/functionCall/VarargNamedArg.kt"); + } + + @TestMetadata("VarargSpreadArg.kt") + public void testVarargSpreadArg() throws Exception { + runTest("../idea/tests/testData/parameterInfo/functionCall/VarargSpreadArg.kt"); + } + + @TestMetadata("VarargUpdateOnTyping.kt") + public void testVarargUpdateOnTyping() throws Exception { + runTest("../idea/tests/testData/parameterInfo/functionCall/VarargUpdateOnTyping.kt"); + } } @RunWith(JUnit3RunnerWithInners.class) diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/parameterInfo/ParameterInfoTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/parameterInfo/ParameterInfoTestGenerated.java index e59b6ea335c3..099edea3299a 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/parameterInfo/ParameterInfoTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/parameterInfo/ParameterInfoTestGenerated.java @@ -436,6 +436,36 @@ public abstract class ParameterInfoTestGenerated extends AbstractParameterInfoTe public void testUpdateOnTyping() throws Exception { runTest("testData/parameterInfo/functionCall/UpdateOnTyping.kt"); } + + @TestMetadata("Vararg.kt") + public void testVararg() throws Exception { + runTest("testData/parameterInfo/functionCall/Vararg.kt"); + } + + @TestMetadata("VarargFirstArgTrailingComma.kt") + public void testVarargFirstArgTrailingComma() throws Exception { + runTest("testData/parameterInfo/functionCall/VarargFirstArgTrailingComma.kt"); + } + + @TestMetadata("VarargMultipleArgsTrailingComma.kt") + public void testVarargMultipleArgsTrailingComma() throws Exception { + runTest("testData/parameterInfo/functionCall/VarargMultipleArgsTrailingComma.kt"); + } + + @TestMetadata("VarargNamedArg.kt") + public void testVarargNamedArg() throws Exception { + runTest("testData/parameterInfo/functionCall/VarargNamedArg.kt"); + } + + @TestMetadata("VarargSpreadArg.kt") + public void testVarargSpreadArg() throws Exception { + runTest("testData/parameterInfo/functionCall/VarargSpreadArg.kt"); + } + + @TestMetadata("VarargUpdateOnTyping.kt") + public void testVarargUpdateOnTyping() throws Exception { + runTest("testData/parameterInfo/functionCall/VarargUpdateOnTyping.kt"); + } } @RunWith(JUnit3RunnerWithInners.class) diff --git a/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/Vararg.kt b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/Vararg.kt new file mode 100644 index 000000000000..4208d0faf51a --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/Vararg.kt @@ -0,0 +1,8 @@ +fun m(x: Boolean, vararg y: Int) = 2 + +fun d() { + m(true, 1, 2) +} +/* +Text: (x: Boolean, vararg y: Int), Disabled: false, Strikeout: false, Green: true +*/ \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargFirstArgTrailingComma.kt b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargFirstArgTrailingComma.kt new file mode 100644 index 000000000000..10c1ab7c8f84 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargFirstArgTrailingComma.kt @@ -0,0 +1,8 @@ +fun m(x: Boolean, vararg y: Int) = 2 + +fun d() { + m(true,) +} +/* +Text: (x: Boolean, vararg y: Int), Disabled: false, Strikeout: false, Green: true +*/ \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargMultipleArgsTrailingComma.kt b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargMultipleArgsTrailingComma.kt new file mode 100644 index 000000000000..aae5e737c8a6 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargMultipleArgsTrailingComma.kt @@ -0,0 +1,8 @@ +fun m(x: Boolean, vararg y: Int) = 2 + +fun d() { + m(true, 1, 2,) +} +/* +Text: (x: Boolean, vararg y: Int), Disabled: false, Strikeout: false, Green: true +*/ \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargNamedArg.kt b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargNamedArg.kt new file mode 100644 index 000000000000..7ec9dbbf0ac9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargNamedArg.kt @@ -0,0 +1,9 @@ +fun m(x: Boolean, vararg y: Int) = 2 + +fun d() { + val a = intArrayOf(1, 2, 3) + m(y = a, x = true) +} +/* +Text: ([vararg y: Int], [x: Boolean]), Disabled: false, Strikeout: false, Green: true +*/ \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargSpreadArg.kt b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargSpreadArg.kt new file mode 100644 index 000000000000..817b19a3ce48 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargSpreadArg.kt @@ -0,0 +1,9 @@ +fun m(x: Boolean, vararg y: Int) = 2 + +fun d() { + val a = intArrayOf(1, 2, 3) + m(true, 1, *a, 4) +} +/* +Text: (x: Boolean, vararg y: Int), Disabled: false, Strikeout: false, Green: true +*/ \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargUpdateOnTyping.kt b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargUpdateOnTyping.kt new file mode 100644 index 000000000000..8e2835a893a8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/parameterInfo/functionCall/VarargUpdateOnTyping.kt @@ -0,0 +1,11 @@ +fun m(x: Boolean, vararg y: Int) = 2 + +fun d() { + m(true, ) +} + +// TYPE: "true, " + +/* +Text: (x: Boolean, vararg y: Int), Disabled: true, Strikeout: false, Green: true +*/ \ No newline at end of file