More accurate import conflict detection
This commit is contained in:
@@ -184,8 +184,19 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
|||||||
else
|
else
|
||||||
file.getImportDirectives()
|
file.getImportDirectives()
|
||||||
|
|
||||||
//TODO: is that correct? What if function is imported and we need to import class?
|
// check there is an explicit import of a class/package with the same name already
|
||||||
if (imports.any { it.getImportedName() == name.asString() }) return ImportDescriptorResult.FAIL
|
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 fqName = target.importableFqNameSafe
|
||||||
val packageFqName = fqName.parent()
|
val packageFqName = fqName.parent()
|
||||||
@@ -368,11 +379,8 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun JetImportDirective.getImportedName(): String? = if (isAllUnder()) null else JetPsiUtil.getAliasName(this)?.getIdentifier()
|
|
||||||
|
|
||||||
private fun targetFqName(ref: JetReferenceExpression): FqName? {
|
private fun targetFqName(ref: JetReferenceExpression): FqName? {
|
||||||
return ref.resolveTargets().map { it.importableFqName }.toSet().singleOrNull()
|
return ref.resolveTargets().map { it.importableFqName }.toSet().singleOrNull()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun JetReferenceExpression.resolveTargets(): Collection<DeclarationDescriptor> {
|
private fun JetReferenceExpression.resolveTargets(): Collection<DeclarationDescriptor> {
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
Failed to add import
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package pack1
|
||||||
|
|
||||||
|
fun ArrayList(){}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// IMPORT: java.util.ArrayList
|
||||||
|
import pack1.ArrayList
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
ArrayList()
|
||||||
|
ArrayList<String>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// IMPORT: java.util.ArrayList
|
||||||
|
import pack1.ArrayList
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
ArrayList()
|
||||||
|
ArrayList<String>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package dependency
|
||||||
|
|
||||||
|
fun div(char c1, char c2){}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// IMPORT: java.sql.Date
|
||||||
|
import java.sql.*
|
||||||
|
import java.util as Date
|
||||||
|
|
||||||
|
val d: Date.Date = Date.Date()
|
||||||
@@ -48,12 +48,6 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("CannotImportClass3.kt")
|
|
||||||
public void testCannotImportClass3() throws Exception {
|
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/CannotImportClass3.kt");
|
|
||||||
doTest(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("ClassAlreadyImported1.kt")
|
@TestMetadata("ClassAlreadyImported1.kt")
|
||||||
public void testClassAlreadyImported1() throws Exception {
|
public void testClassAlreadyImported1() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ClassAlreadyImported1.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ClassAlreadyImported1.kt");
|
||||||
@@ -180,6 +174,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ImportClassWhenFunctionImported.kt")
|
||||||
|
public void testImportClassWhenFunctionImported() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportClassWhenFunctionImported.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("ImportFunctionBug.kt")
|
@TestMetadata("ImportFunctionBug.kt")
|
||||||
public void testImportFunctionBug() throws Exception {
|
public void testImportFunctionBug() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFunctionBug.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFunctionBug.kt");
|
||||||
@@ -192,6 +192,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ImportSecondFunction.kt")
|
||||||
|
public void testImportSecondFunction() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportSecondFunction.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("KeywordNames.kt")
|
@TestMetadata("KeywordNames.kt")
|
||||||
public void testKeywordNames() throws Exception {
|
public void testKeywordNames() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/KeywordNames.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/KeywordNames.kt");
|
||||||
@@ -234,6 +240,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("PackageDoesNotConflictWithClass.kt")
|
||||||
|
public void testPackageDoesNotConflictWithClass() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/PackageDoesNotConflictWithClass.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("PropertyAlreadyImported1.kt")
|
@TestMetadata("PropertyAlreadyImported1.kt")
|
||||||
public void testPropertyAlreadyImported1() throws Exception {
|
public void testPropertyAlreadyImported1() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/PropertyAlreadyImported1.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/PropertyAlreadyImported1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user