diff --git a/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt b/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt index 75044a267fe..21ba0c2bfbd 100644 --- a/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt +++ b/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt @@ -184,8 +184,19 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert else file.getImportDirectives() - //TODO: is that correct? What if function is imported and we need to import class? - if (imports.any { it.getImportedName() == name.asString() }) return ImportDescriptorResult.FAIL + // check there is an explicit import of a class/package with the same name already + val conflict = when (target) { + is ClassDescriptor -> topLevelScope.getClassifier(name) + is PackageViewDescriptor -> topLevelScope.getPackage(name) + else -> null + } + if (conflict != null && imports.any { + !it.isAllUnder() + && it.getImportPath()?.fqnPart() == conflict.importableFqNameSafe + && it.getImportPath()?.getImportedName() == name + }) { + return ImportDescriptorResult.FAIL + } val fqName = target.importableFqNameSafe val packageFqName = fqName.parent() @@ -368,11 +379,8 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert return result } - private fun JetImportDirective.getImportedName(): String? = if (isAllUnder()) null else JetPsiUtil.getAliasName(this)?.getIdentifier() - private fun targetFqName(ref: JetReferenceExpression): FqName? { return ref.resolveTargets().map { it.importableFqName }.toSet().singleOrNull() - } private fun JetReferenceExpression.resolveTargets(): Collection { diff --git a/idea/testData/addImport/CannotImportClass3.kt.after b/idea/testData/addImport/CannotImportClass3.kt.after deleted file mode 100644 index cfc992d7406..00000000000 --- a/idea/testData/addImport/CannotImportClass3.kt.after +++ /dev/null @@ -1 +0,0 @@ -Failed to add import \ No newline at end of file diff --git a/idea/testData/addImport/ImportClassWhenFunctionImported.dependency.kt b/idea/testData/addImport/ImportClassWhenFunctionImported.dependency.kt new file mode 100644 index 00000000000..f6e0a597b5e --- /dev/null +++ b/idea/testData/addImport/ImportClassWhenFunctionImported.dependency.kt @@ -0,0 +1,3 @@ +package pack1 + +fun ArrayList(){} \ No newline at end of file diff --git a/idea/testData/addImport/ImportClassWhenFunctionImported.kt b/idea/testData/addImport/ImportClassWhenFunctionImported.kt new file mode 100644 index 00000000000..7684775c9a8 --- /dev/null +++ b/idea/testData/addImport/ImportClassWhenFunctionImported.kt @@ -0,0 +1,7 @@ +// IMPORT: java.util.ArrayList +import pack1.ArrayList + +fun foo() { + ArrayList() + ArrayList() +} \ No newline at end of file diff --git a/idea/testData/addImport/ImportClassWhenFunctionImported.kt.after b/idea/testData/addImport/ImportClassWhenFunctionImported.kt.after new file mode 100644 index 00000000000..36203f014ca --- /dev/null +++ b/idea/testData/addImport/ImportClassWhenFunctionImported.kt.after @@ -0,0 +1,8 @@ +// IMPORT: java.util.ArrayList +import pack1.ArrayList +import java.util.* + +fun foo() { + ArrayList() + ArrayList() +} \ No newline at end of file diff --git a/idea/testData/addImport/ImportSecondFunction.dependency.kt b/idea/testData/addImport/ImportSecondFunction.dependency.kt new file mode 100644 index 00000000000..c57399eb747 --- /dev/null +++ b/idea/testData/addImport/ImportSecondFunction.dependency.kt @@ -0,0 +1,3 @@ +package dependency + +fun div(char c1, char c2){} \ No newline at end of file diff --git a/idea/testData/addImport/ImportSecondFunction.kt b/idea/testData/addImport/ImportSecondFunction.kt new file mode 100644 index 00000000000..894a2939910 --- /dev/null +++ b/idea/testData/addImport/ImportSecondFunction.kt @@ -0,0 +1,7 @@ +// IMPORT: dependency.div +import java.math.BigDecimal +import kotlin.math.div + +fun foo(d1: BigDecimal, d2: BigDecimal) { + val d = d1 / d2 +} \ No newline at end of file diff --git a/idea/testData/addImport/ImportSecondFunction.kt.after b/idea/testData/addImport/ImportSecondFunction.kt.after new file mode 100644 index 00000000000..39e29728337 --- /dev/null +++ b/idea/testData/addImport/ImportSecondFunction.kt.after @@ -0,0 +1,8 @@ +// IMPORT: dependency.div +import dependency.* +import java.math.BigDecimal +import kotlin.math.div + +fun foo(d1: BigDecimal, d2: BigDecimal) { + val d = d1 / d2 +} \ No newline at end of file diff --git a/idea/testData/addImport/CannotImportClass3.kt b/idea/testData/addImport/PackageDoesNotConflictWithClass.kt similarity index 100% rename from idea/testData/addImport/CannotImportClass3.kt rename to idea/testData/addImport/PackageDoesNotConflictWithClass.kt diff --git a/idea/testData/addImport/PackageDoesNotConflictWithClass.kt.after b/idea/testData/addImport/PackageDoesNotConflictWithClass.kt.after new file mode 100644 index 00000000000..dfdffa602e1 --- /dev/null +++ b/idea/testData/addImport/PackageDoesNotConflictWithClass.kt.after @@ -0,0 +1,5 @@ +// IMPORT: java.sql.Date +import java.sql.* +import java.util as Date + +val d: Date.Date = Date.Date() \ 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 9e4fcd00415..23e6fd1f7ef 100644 --- a/idea/tests/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java @@ -48,12 +48,6 @@ public class AddImportTestGenerated extends AbstractAddImportTest { doTest(fileName); } - @TestMetadata("CannotImportClass3.kt") - public void testCannotImportClass3() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/CannotImportClass3.kt"); - doTest(fileName); - } - @TestMetadata("ClassAlreadyImported1.kt") public void testClassAlreadyImported1() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ClassAlreadyImported1.kt"); @@ -180,6 +174,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest { doTest(fileName); } + @TestMetadata("ImportClassWhenFunctionImported.kt") + public void testImportClassWhenFunctionImported() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportClassWhenFunctionImported.kt"); + doTest(fileName); + } + @TestMetadata("ImportFunctionBug.kt") public void testImportFunctionBug() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFunctionBug.kt"); @@ -192,6 +192,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest { doTest(fileName); } + @TestMetadata("ImportSecondFunction.kt") + public void testImportSecondFunction() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportSecondFunction.kt"); + doTest(fileName); + } + @TestMetadata("KeywordNames.kt") public void testKeywordNames() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/KeywordNames.kt"); @@ -234,6 +240,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest { doTest(fileName); } + @TestMetadata("PackageDoesNotConflictWithClass.kt") + public void testPackageDoesNotConflictWithClass() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/PackageDoesNotConflictWithClass.kt"); + doTest(fileName); + } + @TestMetadata("PropertyAlreadyImported1.kt") public void testPropertyAlreadyImported1() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/PropertyAlreadyImported1.kt");