Optimize Imports: simplify code
This commit is contained in:
@@ -36,16 +36,13 @@ import java.awt.datatransfer.Transferable
|
|||||||
import com.intellij.codeInsight.CodeInsightSettings
|
import com.intellij.codeInsight.CodeInsightSettings
|
||||||
import com.intellij.codeInsight.editorActions.CopyPastePostProcessor
|
import com.intellij.codeInsight.editorActions.CopyPastePostProcessor
|
||||||
import org.jetbrains.jet.plugin.references.JetReference
|
import org.jetbrains.jet.plugin.references.JetReference
|
||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils
|
|
||||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
|
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import org.jetbrains.jet.lang.psi.JetCallExpression
|
import org.jetbrains.jet.lang.psi.JetCallExpression
|
||||||
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor
|
|
||||||
import org.jetbrains.jet.lang.types.ErrorUtils
|
import org.jetbrains.jet.lang.types.ErrorUtils
|
||||||
import org.jetbrains.jet.lang.psi.JetImportDirective
|
import org.jetbrains.jet.lang.psi.JetImportDirective
|
||||||
import org.jetbrains.jet.lang.psi.JetPackageDirective
|
import org.jetbrains.jet.lang.psi.JetPackageDirective
|
||||||
@@ -58,12 +55,10 @@ import java.awt.datatransfer.UnsupportedFlavorException
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
|
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
|
||||||
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
|
|
||||||
import org.jetbrains.jet.lang.psi.JetUserType
|
import org.jetbrains.jet.lang.psi.JetUserType
|
||||||
import org.jetbrains.jet.lang.psi.JetTypeReference
|
import org.jetbrains.jet.lang.psi.JetTypeReference
|
||||||
import com.intellij.util.containers.ContainerUtil
|
import com.intellij.util.containers.ContainerUtil
|
||||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils
|
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils
|
||||||
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor
|
|
||||||
import org.jetbrains.jet.plugin.imports.*
|
import org.jetbrains.jet.plugin.imports.*
|
||||||
|
|
||||||
//NOTE: this class is based on CopyPasteReferenceProcessor and JavaCopyPasteReferenceProcessor
|
//NOTE: this class is based on CopyPasteReferenceProcessor and JavaCopyPasteReferenceProcessor
|
||||||
@@ -128,8 +123,7 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Refere
|
|||||||
if (referencedDeclaration.isInCopiedArea(file, startOffsets, endOffsets)) {
|
if (referencedDeclaration.isInCopiedArea(file, startOffsets, endOffsets)) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
val isExpressionWithReceiver = element is JetSimpleNameExpression && element.getReceiverExpression() != null
|
if (isInReceiverScope(element, referencedDescriptor)) {
|
||||||
if (isExpressionWithReceiver && !referencedDescriptor.isExtension) {
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
val fqName = referencedDescriptor.importableFqName
|
val fqName = referencedDescriptor.importableFqName
|
||||||
@@ -361,7 +355,4 @@ private fun PsiElement.isInCopiedArea(fileCopiedFrom: JetFile, startOffsets: Int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val DeclarationDescriptor.isExtension: Boolean
|
|
||||||
get() = this is CallableDescriptor && getReceiverParameter() != null
|
|
||||||
|
|
||||||
private fun <T : Any> MutableCollection<T>.addIfNotNull(el: T?) = ContainerUtil.addIfNotNull(this, el)
|
private fun <T : Any> MutableCollection<T>.addIfNotNull(el: T?) = ContainerUtil.addIfNotNull(this, el)
|
||||||
@@ -22,6 +22,9 @@ import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor
|
|||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils
|
||||||
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor
|
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor
|
||||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
|
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
|
||||||
|
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
|
||||||
|
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
|
||||||
public val DeclarationDescriptor.importableFqName: FqName?
|
public val DeclarationDescriptor.importableFqName: FqName?
|
||||||
get() {
|
get() {
|
||||||
@@ -49,3 +52,12 @@ public fun DeclarationDescriptor.canBeReferencedViaImport(): Boolean {
|
|||||||
}
|
}
|
||||||
return this is ClassDescriptor || this is ConstructorDescriptor
|
return this is ClassDescriptor || this is ConstructorDescriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun isInReceiverScope(referenceElement: PsiElement, referencedDescriptor: DeclarationDescriptor): Boolean {
|
||||||
|
val isExpressionWithReceiver = referenceElement is JetSimpleNameExpression && referenceElement.getReceiverExpression() != null
|
||||||
|
return isExpressionWithReceiver && !referencedDescriptor.isExtension
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: move this utility to more appropriate place
|
||||||
|
public val DeclarationDescriptor.isExtension: Boolean
|
||||||
|
get() = this is CallableDescriptor && getReceiverParameter() != null
|
||||||
@@ -53,7 +53,10 @@ public class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUseful(importPath, usedQualifiedNames) && needImport(importPath, jetFile, directivesBeforeCurrent) && needImport(importPath, jetFile, directivesAfterCurrent)) {
|
if (isUseful(importPath, usedQualifiedNames)
|
||||||
|
&& needImport(importPath, jetFile, directivesBeforeCurrent)
|
||||||
|
&& needImport(importPath, jetFile, directivesAfterCurrent)
|
||||||
|
) {
|
||||||
directivesBeforeCurrent.add(anImport)
|
directivesBeforeCurrent.add(anImport)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -76,20 +79,6 @@ public class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
element?.acceptChildren(this)
|
element?.acceptChildren(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitUserType(`type`: JetUserType) {
|
|
||||||
val qualifier = `type`.getQualifier()
|
|
||||||
if (qualifier == null) {
|
|
||||||
super.visitUserType(`type`)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
val argumentList = `type`.getTypeArgumentList()
|
|
||||||
if (argumentList != null) {
|
|
||||||
super.visitTypeArgumentList(argumentList)
|
|
||||||
}
|
|
||||||
visitUserType(qualifier)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitJetElement(element: JetElement) {
|
override fun visitJetElement(element: JetElement) {
|
||||||
if (PsiTreeUtil.getParentOfType(element, javaClass<JetImportDirective>()) != null ||
|
if (PsiTreeUtil.getParentOfType(element, javaClass<JetImportDirective>()) != null ||
|
||||||
PsiTreeUtil.getParentOfType(element, javaClass<JetPackageDirective>()) != null) {
|
PsiTreeUtil.getParentOfType(element, javaClass<JetPackageDirective>()) != null) {
|
||||||
@@ -98,7 +87,9 @@ public class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
val reference = element.getReference()
|
val reference = element.getReference()
|
||||||
if (reference is JetReference) {
|
if (reference is JetReference) {
|
||||||
val referencedDescriptors = reference.resolveToDescriptors()
|
val referencedDescriptors = reference.resolveToDescriptors()
|
||||||
val importableDescriptors = referencedDescriptors.filter { it.canBeReferencedViaImport() }
|
val importableDescriptors = referencedDescriptors.filter {
|
||||||
|
it.canBeReferencedViaImport() && !isInReceiverScope(element, it)
|
||||||
|
}
|
||||||
usedQualifiedNames.addAll(importableDescriptors.map { it.importableFqName }.filterNotNull())
|
usedQualifiedNames.addAll(importableDescriptors.map { it.importableFqName }.filterNotNull())
|
||||||
}
|
}
|
||||||
super.visitJetElement(element)
|
super.visitJetElement(element)
|
||||||
|
|||||||
Reference in New Issue
Block a user