diff --git a/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/quickfix/HighLevelQuickFixMultiFileTestGenerated.java b/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/quickfix/HighLevelQuickFixMultiFileTestGenerated.java index 555d4134a4a0..358237923e56 100644 --- a/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/quickfix/HighLevelQuickFixMultiFileTestGenerated.java +++ b/plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/quickfix/HighLevelQuickFixMultiFileTestGenerated.java @@ -590,6 +590,11 @@ public abstract class HighLevelQuickFixMultiFileTestGenerated extends AbstractHi runTest("../idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionComponentNoOperator.test"); } + @TestMetadata("multiFunctionCandidate.test") + public void testMultiFunctionCandidate() throws Exception { + runTest("../idea/tests/testData/quickfix/autoImports/multiFunctionCandidate.test"); + } + @TestMetadata("nestedClass.before.Main.kt") public void testNestedClass() throws Exception { runTest("../idea/tests/testData/quickfix/autoImports/nestedClass.before.Main.kt"); diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/AbstractImportFix.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/AbstractImportFix.kt index 0634c101c4c8..c058330e09f1 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/AbstractImportFix.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/AbstractImportFix.kt @@ -186,8 +186,24 @@ internal abstract class ImportFixBase protected constructor( } else { KotlinBundle.message("fix.import.kind.0.name.1.2", kind.toText(size), firstName, size - 1) } - } else { + } else if (kind.groupedByPackage) { KotlinBundle.message("fix.import.kind.0.name.1.2", kind.toText(1), firstName, 0) + } else { + val groupBy = sortedNames.map { it.name }.toSortedSet().groupBy { + val endIndex = it.indexOf(".") + if (endIndex > 0) it.substring(0, endIndex) else it + } + val value = groupBy.entries.first().value + val first = value.first() + val multiple = if (value.size == 1) 0 else 1 + if (groupBy.size == 1) { + if (value.size == 2) { + KotlinBundle.message("fix.import.kind.0.name.1.and.name.2", kind.toText(value.size), first, value.last()) + } else + KotlinBundle.message("fix.import.kind.0.name.1.2", kind.toText(1), first, multiple) + } else { + KotlinBundle.message("fix.import.kind.0.name.1.2", kind.toText(1), first.substring(first.indexOf(".") + 1), multiple) + } } } else { KotlinBundle.message("fix.import") @@ -196,10 +212,10 @@ internal abstract class ImportFixBase protected constructor( private class ImportName(val kind: ImportKind, val name: String, val priority: ComparablePriority) - private enum class ImportKind(private val key: String) { - CLASS("text.class.0"), + private enum class ImportKind(private val key: String, val groupedByPackage: Boolean = false) { + CLASS("text.class.0", true), PROPERTY("text.property.0"), - OBJECT("text.object.0"), + OBJECT("text.object.0", true), FUNCTION("text.function.0"), EXTENSION_PROPERTY("text.extension.property.0"), EXTENSION_FUNCTION("text.extension.function.0"); diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java index 502d37e4da69..61ab715537d0 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -693,6 +693,11 @@ public abstract class QuickFixMultiFileTestGenerated extends AbstractQuickFixMul runTest("testData/quickfix/autoImports/multiDeclarationExtensionComponentNoOperator.test"); } + @TestMetadata("multiFunctionCandidate.test") + public void testMultiFunctionCandidate() throws Exception { + runTest("testData/quickfix/autoImports/multiFunctionCandidate.test"); + } + @TestMetadata("nestedClass.before.Main.kt") public void testNestedClass() throws Exception { runTest("testData/quickfix/autoImports/nestedClass.before.Main.kt"); diff --git a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsMany.test b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsMany.test index 4f8db0eb5d5b..863a96c942f7 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsMany.test +++ b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsMany.test @@ -1,5 +1,5 @@ // FILE: first.before.kt -// "Import extension function 'Some.component1'" "true" +// "Import extension functions 'Some.component1', 'Some.component2'" "true" // ERROR: Destructuring declaration initializer of type Some must have a 'component1()' function // ERROR: Destructuring declaration initializer of type Some must have a 'component2()' function /* IGNORE_FIR */ @@ -36,7 +36,7 @@ operator fun Some.component2() = 3 // FILE: first.after.kt -// "Import extension function 'Some.component1'" "true" +// "Import extension functions 'Some.component1', 'Some.component2'" "true" // ERROR: Destructuring declaration initializer of type Some must have a 'component1()' function // ERROR: Destructuring declaration initializer of type Some must have a 'component2()' function /* IGNORE_FIR */ diff --git a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereFull.test b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereFull.test index c36ca1b2fdcd..88af986dc466 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereFull.test +++ b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereFull.test @@ -1,5 +1,5 @@ // FILE: first.before.kt -// "Import extension function 'Some.component1'" "true" +// "Import extension functions 'Some.component1', 'Some.component2'" "true" // ERROR: Destructuring declaration initializer of type Some must have a 'component1()' function // ERROR: Destructuring declaration initializer of type Some must have a 'component2()' function /* IGNORE_FIR */ @@ -35,7 +35,7 @@ operator fun Some.component1() = 1 // FILE: first.after.kt -// "Import extension function 'Some.component1'" "true" +// "Import extension functions 'Some.component1', 'Some.component2'" "true" // ERROR: Destructuring declaration initializer of type Some must have a 'component1()' function // ERROR: Destructuring declaration initializer of type Some must have a 'component2()' function /* IGNORE_FIR */ diff --git a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereNotDeprecated.test b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereNotDeprecated.test index 0a12cd9d8a39..ed52a2a42f0b 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereNotDeprecated.test +++ b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiDeclarationExtensionAllComponentsPrefereNotDeprecated.test @@ -1,5 +1,5 @@ // FILE: first.before.kt -// "Import extension function 'Some.component2'" "true" +// "Import extension functions 'Some.component1', 'Some.component2'" "true" // ERROR: Destructuring declaration initializer of type Some must have a 'component1()' function // ERROR: Destructuring declaration initializer of type Some must have a 'component2()' function /* IGNORE_FIR */ @@ -37,7 +37,7 @@ operator fun Some.component2() = 3 // FILE: first.after.kt -// "Import extension function 'Some.component2'" "true" +// "Import extension functions 'Some.component1', 'Some.component2'" "true" // ERROR: Destructuring declaration initializer of type Some must have a 'component1()' function // ERROR: Destructuring declaration initializer of type Some must have a 'component2()' function /* IGNORE_FIR */ diff --git a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiFunctionCandidate.test b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiFunctionCandidate.test new file mode 100644 index 000000000000..d47b0891af22 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/multiFunctionCandidate.test @@ -0,0 +1,28 @@ +// FILE: first.before.kt +// "Import function 'min'" "true" +// ERROR: Unresolved reference: min +// WITH_STDLIB +/* IGNORE_FIR */ + +package testing + +fun testing() { + val x = min(1, 2) +} +//----------------------- + + +// FILE: first.after.kt +// "Import function 'min'" "true" +// ERROR: Unresolved reference: min +// WITH_STDLIB +/* IGNORE_FIR */ + +package testing + +import java.util.Collections.min + +fun testing() { + val x = min(1, 2) +} +//----------------------- diff --git a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/operatorAssignPlusTwoVariantsDifferentPackages.test b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/operatorAssignPlusTwoVariantsDifferentPackages.test index 3359919cc0f9..0e8be746aece 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/operatorAssignPlusTwoVariantsDifferentPackages.test +++ b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/operatorAssignPlusTwoVariantsDifferentPackages.test @@ -1,5 +1,5 @@ // FILE: first.before.kt -// "Import extension function 'Some.plusAssign'" "true" +// "Import extension functions 'Some.plus', 'Some.plusAssign'" "true" // ERROR: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public operator fun String?.plus(other: Any?): String defined in kotlin /* IGNORE_FIR */ @@ -31,7 +31,7 @@ operator fun Some.plus(i: Int) : Some = this // FILE: first.after.kt -// "Import extension function 'Some.plusAssign'" "true" +// "Import extension functions 'Some.plus', 'Some.plusAssign'" "true" // ERROR: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public operator fun String?.plus(other: Any?): String defined in kotlin /* IGNORE_FIR */