More accurate import conflict detection

This commit is contained in:
Valentin Kipyatkov
2015-03-31 22:21:45 +03:00
parent e22f28c061
commit 7e9314bd40
11 changed files with 72 additions and 12 deletions
@@ -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<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);
}
@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");