Fixed a bug in import insertion algorithm
This commit is contained in:
@@ -243,13 +243,16 @@ public class ImportInsertHelperImpl : ImportInsertHelper {
|
|||||||
.filter(::isVisible)
|
.filter(::isVisible)
|
||||||
.map { it.getName() }
|
.map { it.getName() }
|
||||||
|
|
||||||
val aliasNames = imports.map { it.getImportedName() }.filterNotNull().toSet()
|
val topLevelScope = resolutionFacade.getFileTopLevelScope(file)
|
||||||
var conflictCandidates: List<ClassifierDescriptor> = classNamesToImport
|
val conflictCandidates: List<ClassifierDescriptor> = classNamesToImport
|
||||||
.filter { it.asString() !in aliasNames }
|
|
||||||
.flatMap {
|
.flatMap {
|
||||||
importedScopes.map { scope -> scope.getClassifier(it) }.filterNotNull()
|
importedScopes.map { scope -> scope.getClassifier(it) }.filterNotNull()
|
||||||
}
|
}
|
||||||
.filter(::isVisible)
|
.filter { importedClass ->
|
||||||
|
isVisible(importedClass)
|
||||||
|
&& topLevelScope.getClassifier(importedClass.getName()) == importedClass /* check that class is really imported */
|
||||||
|
&& imports.all { it.getImportPath() != ImportPath(importedClass.importableFqNameSafe, false) } /* and not yet imported explicitly */
|
||||||
|
}
|
||||||
val conflicts = detectNeededImports(conflictCandidates)
|
val conflicts = detectNeededImports(conflictCandidates)
|
||||||
|
|
||||||
val addedImport = addImport(parentFqName, true)
|
val addedImport = addImport(parentFqName, true)
|
||||||
@@ -288,6 +291,8 @@ public class ImportInsertHelperImpl : ImportInsertHelper {
|
|||||||
val topLevelScope = resolutionFacade.getFileTopLevelScope(file)
|
val topLevelScope = resolutionFacade.getFileTopLevelScope(file)
|
||||||
val name = target.getName()
|
val name = target.getName()
|
||||||
|
|
||||||
|
// check if there is a conflicting class imported with * import
|
||||||
|
// (not with explicit import - explicit imports are checked before this method invocation)
|
||||||
val classifier = topLevelScope.getClassifier(name)
|
val classifier = topLevelScope.getClassifier(name)
|
||||||
if (classifier != null && detectNeededImports(listOf(classifier)).isNotEmpty()) {
|
if (classifier != null && detectNeededImports(listOf(classifier)).isNotEmpty()) {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package dependency
|
||||||
|
|
||||||
|
fun String.Date(p: Int) {}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// IMPORT: java.util.ArrayList
|
||||||
|
package p
|
||||||
|
|
||||||
|
import java.sql.*
|
||||||
|
import dependency.Date // import of function does not affect class name conflict
|
||||||
|
|
||||||
|
val d = Date(1)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IMPORT: java.util.ArrayList
|
||||||
|
package p
|
||||||
|
|
||||||
|
import java.sql.*
|
||||||
|
import dependency.Date // import of function does not affect class name conflict
|
||||||
|
import java.util.*
|
||||||
|
import java.sql.Date
|
||||||
|
|
||||||
|
val d = Date(1)
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.addImport;
|
package org.jetbrains.kotlin.addImport;
|
||||||
|
|
||||||
import com.intellij.testFramework.TestDataPath;
|
import com.intellij.testFramework.TestDataPath;
|
||||||
|
import org.jetbrains.kotlin.test.InnerTestClasses;
|
||||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||||
import org.jetbrains.kotlin.test.JetTestUtils;
|
import org.jetbrains.kotlin.test.JetTestUtils;
|
||||||
import org.jetbrains.kotlin.test.TestMetadata;
|
import org.jetbrains.kotlin.test.TestMetadata;
|
||||||
@@ -95,6 +96,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ConflictingNameAppearsAndHasUsage6.kt")
|
||||||
|
public void testConflictingNameAppearsAndHasUsage6() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ConflictingNameAppearsAndHasUsage6.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("ConflictingNameAppearsButUsageIsQualified.kt")
|
@TestMetadata("ConflictingNameAppearsButUsageIsQualified.kt")
|
||||||
public void testConflictingNameAppearsButUsageIsQualified() throws Exception {
|
public void testConflictingNameAppearsButUsageIsQualified() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ConflictingNameAppearsButUsageIsQualified.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ConflictingNameAppearsButUsageIsQualified.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user