From 074829bcead63c3ef9234a552245438947ea1900 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Fri, 28 Jun 2019 18:13:34 +0700 Subject: [PATCH] Import: IDE should import class from root package #KT-21422 Fixed --- .../kotlin/idea/actions/KotlinAddImportAction.kt | 15 +++++++++------ .../addImport/ImportFromRoot.dependency.kt | 1 + idea/testData/addImport/ImportFromRoot.kt | 6 ++++++ idea/testData/addImport/ImportFromRoot.kt.after | 8 ++++++++ .../quickfix/autoImports/importFromRoot.after.kt | 9 +++++++++ .../importFromRoot.before.Dependency.kt | 1 + .../autoImports/importFromRoot.before.Main.kt | 7 +++++++ .../kotlin/addImport/AddImportTestGenerated.java | 5 +++++ .../quickfix/QuickFixMultiFileTestGenerated.java | 5 +++++ 9 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 idea/testData/addImport/ImportFromRoot.dependency.kt create mode 100644 idea/testData/addImport/ImportFromRoot.kt create mode 100644 idea/testData/addImport/ImportFromRoot.kt.after create mode 100644 idea/testData/quickfix/autoImports/importFromRoot.after.kt create mode 100644 idea/testData/quickfix/autoImports/importFromRoot.before.Dependency.kt create mode 100644 idea/testData/quickfix/autoImports/importFromRoot.before.Main.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/actions/KotlinAddImportAction.kt b/idea/src/org/jetbrains/kotlin/idea/actions/KotlinAddImportAction.kt index b979a6bcb8f..4714ea8ed30 100644 --- a/idea/src/org/jetbrains/kotlin/idea/actions/KotlinAddImportAction.kt +++ b/idea/src/org/jetbrains/kotlin/idea/actions/KotlinAddImportAction.kt @@ -51,6 +51,7 @@ import org.jetbrains.kotlin.idea.references.resolveMainReferenceToDescriptors import org.jetbrains.kotlin.idea.util.ImportInsertHelper import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.isOneSegmentFQN import org.jetbrains.kotlin.name.parentOrNull import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtFile @@ -239,18 +240,21 @@ class KotlinAddImportAction internal constructor( // for class or package we use ShortenReferences because we not necessary insert an import but may want to // insert partly qualified name - val importAlias = descriptor.importableFqName?.let { file.findAliasByFqName(it) } - if (importAlias != null || descriptor is ClassDescriptor || descriptor is PackageViewDescriptor) { + val importableFqName = descriptor.importableFqName + val importAlias = importableFqName?.let { file.findAliasByFqName(it) } + if (importableFqName?.isOneSegmentFQN() != true && + (importAlias != null || descriptor is ClassDescriptor || descriptor is PackageViewDescriptor) + ) { if (element is KtSimpleNameExpression) { if (importAlias != null) { importAlias.nameIdentifier?.copy()?.let { element.getIdentifier()?.replace(it) } val resultDescriptor = element.resolveMainReferenceToDescriptors().firstOrNull() - if (descriptor.importableFqName == resultDescriptor?.importableFqName) { + if (importableFqName == resultDescriptor?.importableFqName) { return@forEach } } - descriptor.importableFqName?.let { + importableFqName?.let { element.mainReference.bindToFqName( it, KtSimpleNameReference.ShorteningMode.FORCED_SHORTENING @@ -343,8 +347,7 @@ private class SingleImportVariant( val descriptors: Collection ) : AutoImportVariant { override val descriptorsToImport: Collection - get() = - listOf(descriptors.singleOrNull() ?: descriptors.sortedBy { if (it is ClassDescriptor) 0 else 1 }.first()) + get() = listOf(descriptors.singleOrNull() ?: descriptors.minBy { if (it is ClassDescriptor) 0 else 1 }!!) override val hint: String get() = excludeFqNameCheck.asString() } diff --git a/idea/testData/addImport/ImportFromRoot.dependency.kt b/idea/testData/addImport/ImportFromRoot.dependency.kt new file mode 100644 index 00000000000..171cfa1957b --- /dev/null +++ b/idea/testData/addImport/ImportFromRoot.dependency.kt @@ -0,0 +1 @@ +class RootClass \ No newline at end of file diff --git a/idea/testData/addImport/ImportFromRoot.kt b/idea/testData/addImport/ImportFromRoot.kt new file mode 100644 index 00000000000..15609869632 --- /dev/null +++ b/idea/testData/addImport/ImportFromRoot.kt @@ -0,0 +1,6 @@ +// IMPORT: class: RootClass +package non.root.name + +fun test() { + RootClass() +} \ No newline at end of file diff --git a/idea/testData/addImport/ImportFromRoot.kt.after b/idea/testData/addImport/ImportFromRoot.kt.after new file mode 100644 index 00000000000..5d397ac6ca6 --- /dev/null +++ b/idea/testData/addImport/ImportFromRoot.kt.after @@ -0,0 +1,8 @@ +// IMPORT: class: RootClass +package non.root.name + +import RootClass + +fun test() { + RootClass() +} \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/importFromRoot.after.kt b/idea/testData/quickfix/autoImports/importFromRoot.after.kt new file mode 100644 index 00000000000..a19688d2a3e --- /dev/null +++ b/idea/testData/quickfix/autoImports/importFromRoot.after.kt @@ -0,0 +1,9 @@ +// "Import" "true" +// ERROR: Unresolved reference: RootClass +package non.root.name + +import RootClass + +fun test() { + RootClass() +} \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/importFromRoot.before.Dependency.kt b/idea/testData/quickfix/autoImports/importFromRoot.before.Dependency.kt new file mode 100644 index 00000000000..171cfa1957b --- /dev/null +++ b/idea/testData/quickfix/autoImports/importFromRoot.before.Dependency.kt @@ -0,0 +1 @@ +class RootClass \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/importFromRoot.before.Main.kt b/idea/testData/quickfix/autoImports/importFromRoot.before.Main.kt new file mode 100644 index 00000000000..e4c26095f5b --- /dev/null +++ b/idea/testData/quickfix/autoImports/importFromRoot.before.Main.kt @@ -0,0 +1,7 @@ +// "Import" "true" +// ERROR: Unresolved reference: RootClass +package non.root.name + +fun test() { + RootClass() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java b/idea/tests/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java index 26367b12ac5..91e0c5774f1 100644 --- a/idea/tests/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java @@ -199,6 +199,11 @@ public class AddImportTestGenerated extends AbstractAddImportTest { runTest("idea/testData/addImport/ImportFromObject.kt"); } + @TestMetadata("ImportFromRoot.kt") + public void testImportFromRoot() throws Exception { + runTest("idea/testData/addImport/ImportFromRoot.kt"); + } + @TestMetadata("ImportFunctionBug.kt") public void testImportFunctionBug() throws Exception { runTest("idea/testData/addImport/ImportFunctionBug.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java index 64936309bcd..702ccb96aec 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -633,6 +633,11 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes runTest("idea/testData/quickfix/autoImports/importAliasPropertyAlreadyExists.before.Main.kt"); } + @TestMetadata("importFromRoot.before.Main.kt") + public void testImportFromRoot() throws Exception { + runTest("idea/testData/quickfix/autoImports/importFromRoot.before.Main.kt"); + } + @TestMetadata("importInFirstPartInQualifiedExpression.before.Main.kt") public void testImportInFirstPartInQualifiedExpression() throws Exception { runTest("idea/testData/quickfix/autoImports/importInFirstPartInQualifiedExpression.before.Main.kt");