Optimize Imports: do not keep imports of unimportable entities

This commit is contained in:
Pavel V. Talanov
2014-03-05 17:59:57 +04:00
parent c87cc0dde1
commit 809be9b069
7 changed files with 64 additions and 17 deletions
@@ -164,21 +164,6 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Refere
return collectedData
}
private fun DeclarationDescriptor.canBeReferencedViaImport(): Boolean {
if (this is PackageViewDescriptor || DescriptorUtils.isTopLevelDeclaration(this)) {
return true
}
val parent = getContainingDeclaration()!!
if (parent !is ClassDescriptor || !parent.canBeReferencedViaImport()) {
return false
}
// inner class constructors can't be referenced via import
if (this is ConstructorDescriptor && parent.isInner()) {
return false
}
return this is ClassDescriptor || this is ConstructorDescriptor
}
private fun createReferenceData(element: PsiElement, startOffset: Int, fqName: FqName): ReferenceData {
val range = element.range
return ReferenceData(range.start - startOffset, range.end - startOffset, fqName.asString(), null)
@@ -20,6 +20,8 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
import org.jetbrains.jet.lang.resolve.name.FqName
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor
import org.jetbrains.jet.lang.resolve.DescriptorUtils
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
public val DeclarationDescriptor.importableFqName: FqName?
get() {
@@ -31,4 +33,19 @@ public val DeclarationDescriptor.importableFqName: FqName?
else {
null
}
}
}
public fun DeclarationDescriptor.canBeReferencedViaImport(): Boolean {
if (this is PackageViewDescriptor || DescriptorUtils.isTopLevelDeclaration(this)) {
return true
}
val parent = getContainingDeclaration()!!
if (parent !is ClassDescriptor || !parent.canBeReferencedViaImport()) {
return false
}
// inner class constructors can't be referenced via import
if (this is ConstructorDescriptor && parent.isInner()) {
return false
}
return this is ClassDescriptor || this is ConstructorDescriptor
}
@@ -98,7 +98,8 @@ public class KotlinImportOptimizer() : ImportOptimizer {
val reference = element.getReference()
if (reference is JetReference) {
val referencedDescriptors = reference.resolveToDescriptors()
usedQualifiedNames.addAll(referencedDescriptors.map { it.importableFqName }.filterNotNull())
val importableDescriptors = referencedDescriptors.filter { it.canBeReferencedViaImport() }
usedQualifiedNames.addAll(importableDescriptors.map { it.importableFqName }.filterNotNull())
}
super.visitJetElement(element)
}
@@ -0,0 +1,10 @@
package test
import dependency.*
fun f(a: A, t: T) {
a.f()
a.p
t.f
t.p
}
@@ -0,0 +1,15 @@
package dependency
class A {
fun f() {
}
val p: Int = 1
}
trait T {
fun f() {
}
val p: Int = 1
}
@@ -0,0 +1,14 @@
package test
import dependency.*
import dependency.A.f
import dependency.A.p
import dependency.T.f
import dependency.T.p
fun f(a: A, t: T) {
a.f()
a.p
t.f
t.p
}
@@ -46,6 +46,11 @@ public class OptimizeImportsTestGenerated extends AbstractOptimizeImportsTest {
doTest("idea/testData/editor/optimizeImports/ArrayAccessExpression");
}
@TestMetadata("ClassMemberImported")
public void testClassMemberImported() throws Exception {
doTest("idea/testData/editor/optimizeImports/ClassMemberImported");
}
@TestMetadata("ComponentFunction")
public void testComponentFunction() throws Exception {
doTest("idea/testData/editor/optimizeImports/ComponentFunction");