diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ImportMemberIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ImportMemberIntention.kt index 176abe18857..a2aa1e23bae 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ImportMemberIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ImportMemberIntention.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInsight.intention.HighPriorityAction import com.intellij.openapi.editor.Editor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.ShortenReferences @@ -16,7 +17,6 @@ import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.references.resolveMainReferenceToDescriptors import org.jetbrains.kotlin.idea.util.ImportDescriptorResult import org.jetbrains.kotlin.idea.util.ImportInsertHelper -import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.resolve.BindingContext @@ -33,7 +33,14 @@ class ImportMemberIntention : SelfTargetingOffsetIndependentIntention { qualifiedExpression -> val selector = qualifiedExpression.getQualifiedElementSelector() as? KtNameReferenceExpression - selector?.getReferencedNameAsName() == fqName.shortName() && targetFqName(qualifiedExpression) == fqName + selector?.getReferencedNameAsName() == fqName.shortName() && target(qualifiedExpression)?.importableFqName == fqName } val userTypes = file.collectDescendantsOfType { userType -> val selector = userType.getQualifiedElementSelector() as? KtNameReferenceExpression - selector?.getReferencedNameAsName() == fqName.shortName() && targetFqName(userType) == fqName + selector?.getReferencedNameAsName() == fqName.shortName() && target(userType)?.importableFqName == fqName } //TODO: not deep ShortenReferences.DEFAULT.process(qualifiedExpressions + userTypes) } - private fun targetFqName(qualifiedElement: KtElement): FqName? { + private fun target(qualifiedElement: KtElement): DeclarationDescriptor? { val nameExpression = qualifiedElement.getQualifiedElementSelector() as? KtNameReferenceExpression ?: return null val receiver = nameExpression.getReceiverExpression() ?: return null val bindingContext = qualifiedElement.analyze(BodyResolveMode.PARTIAL) @@ -77,6 +84,6 @@ class ImportMemberIntention : SelfTargetingOffsetIndependentIntention) +} + +fun foo(a: A) {} + +fun bar(b: B) {} + +enum class A { + SUCCESS, ERROR +} + +enum class B { + SUCCESS, ERROR +} + diff --git a/idea/testData/intentions/importMember/AlreadyImportedSameNameClass2.kt b/idea/testData/intentions/importMember/AlreadyImportedSameNameClass2.kt new file mode 100644 index 00000000000..1cae75e2b3c --- /dev/null +++ b/idea/testData/intentions/importMember/AlreadyImportedSameNameClass2.kt @@ -0,0 +1,22 @@ +// IS_APPLICABLE: false +package foo.bar + +import foo.bar.A.* + +fun test() { + foo(SUCCESS) + bar(B.SUCCESS) +} + +fun foo(a: A) {} + +fun bar(b: B) {} + +enum class A { + SUCCESS, ERROR +} + +enum class B { + SUCCESS, ERROR +} + diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index e0b505ba549..8bf42c08c31 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -8973,6 +8973,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/importMember"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("AlreadyImportedSameNameClass.kt") + public void testAlreadyImportedSameNameClass() throws Exception { + runTest("idea/testData/intentions/importMember/AlreadyImportedSameNameClass.kt"); + } + + @TestMetadata("AlreadyImportedSameNameClass2.kt") + public void testAlreadyImportedSameNameClass2() throws Exception { + runTest("idea/testData/intentions/importMember/AlreadyImportedSameNameClass2.kt"); + } + @TestMetadata("ClassCallChain.kt") public void testClassCallChain() throws Exception { runTest("idea/testData/intentions/importMember/ClassCallChain.kt");