Import: IDE should import class from root package

#KT-21422 Fixed
This commit is contained in:
Dmitry Gridin
2019-06-28 18:13:34 +07:00
parent 148eed2a12
commit 074829bcea
9 changed files with 51 additions and 6 deletions
@@ -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<DeclarationDescriptor>
) : AutoImportVariant {
override val descriptorsToImport: Collection<DeclarationDescriptor>
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()
}
+1
View File
@@ -0,0 +1 @@
class RootClass
+6
View File
@@ -0,0 +1,6 @@
// IMPORT: class: RootClass
package non.root.name
fun test() {
RootClass()
}
+8
View File
@@ -0,0 +1,8 @@
// IMPORT: class: RootClass
package non.root.name
import RootClass
fun test() {
RootClass()
}
@@ -0,0 +1,9 @@
// "Import" "true"
// ERROR: Unresolved reference: RootClass
package non.root.name
import RootClass
fun test() {
RootClass()
}
@@ -0,0 +1 @@
class RootClass
@@ -0,0 +1,7 @@
// "Import" "true"
// ERROR: Unresolved reference: RootClass
package non.root.name
fun test() {
RootClass<caret>()
}
@@ -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");
@@ -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");