Used correct function for fetching elements in range instead of strange one from daemon.impl
This commit is contained in:
+67
-49
@@ -16,39 +16,52 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.codeInsight
|
package org.jetbrains.kotlin.idea.codeInsight
|
||||||
|
|
||||||
import com.intellij.openapi.editor.RangeMarker
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
|
|
||||||
import org.jetbrains.kotlin.name.FqName
|
|
||||||
import com.intellij.openapi.project.Project
|
|
||||||
import com.intellij.openapi.util.Ref
|
|
||||||
import com.intellij.openapi.project.DumbService
|
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
|
||||||
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 com.intellij.openapi.application.ApplicationManager
|
||||||
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.editor.RangeMarker
|
||||||
|
import com.intellij.openapi.progress.ProcessCanceledException
|
||||||
|
import com.intellij.openapi.project.DumbService
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.openapi.util.Ref
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
|
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||||
|
import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedShortening
|
||||||
|
import org.jetbrains.kotlin.idea.conversion.copy.end
|
||||||
|
import org.jetbrains.kotlin.idea.conversion.copy.range
|
||||||
|
import org.jetbrains.kotlin.idea.conversion.copy.start
|
||||||
|
import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport
|
||||||
|
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||||
|
import org.jetbrains.kotlin.idea.references.JetMultiReference
|
||||||
|
import org.jetbrains.kotlin.idea.references.JetReference
|
||||||
|
import org.jetbrains.kotlin.idea.references.JetSimpleNameReference
|
||||||
|
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
||||||
|
import org.jetbrains.kotlin.name.FqName
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.elementsInRange
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingTraceContext
|
||||||
|
import org.jetbrains.kotlin.resolve.ImportPath
|
||||||
|
import org.jetbrains.kotlin.resolve.JetModuleUtil
|
||||||
|
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver
|
||||||
|
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver.LookupMode
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||||
import org.jetbrains.kotlin.types.ErrorUtils
|
import org.jetbrains.kotlin.types.ErrorUtils
|
||||||
import org.jetbrains.kotlin.idea.conversion.copy.*
|
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||||
|
import java.awt.datatransfer.Transferable
|
||||||
import java.awt.datatransfer.UnsupportedFlavorException
|
import java.awt.datatransfer.UnsupportedFlavorException
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import java.util.ArrayList
|
||||||
import org.jetbrains.kotlin.idea.imports.*
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
|
|
||||||
import com.intellij.openapi.progress.ProcessCanceledException
|
|
||||||
import org.jetbrains.kotlin.idea.references.*
|
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
|
||||||
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver.LookupMode
|
|
||||||
import org.jetbrains.kotlin.resolve.*
|
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.*
|
|
||||||
import org.jetbrains.kotlin.idea.util.*
|
|
||||||
import org.jetbrains.kotlin.utils.*
|
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedShortening
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.*
|
|
||||||
import com.intellij.psi.*
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
//NOTE: this class is based on CopyPasteReferenceProcessor and JavaCopyPasteReferenceProcessor
|
//NOTE: this class is based on CopyPasteReferenceProcessor and JavaCopyPasteReferenceProcessor
|
||||||
public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<KotlinReferenceTransferableData>() {
|
public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<KotlinReferenceTransferableData>() {
|
||||||
@@ -110,46 +123,51 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Kotlin
|
|||||||
): List<KotlinReferenceData> {
|
): List<KotlinReferenceData> {
|
||||||
val result = ArrayList<KotlinReferenceData>()
|
val result = ArrayList<KotlinReferenceData>()
|
||||||
for (range in toTextRanges(startOffsets, endOffsets)) {
|
for (range in toTextRanges(startOffsets, endOffsets)) {
|
||||||
for (element in CollectHighlightsUtil.getElementsInRange(file, range.start, range.end)) {
|
for (element in file.elementsInRange(range)) {
|
||||||
result.addReferenceDataFromElement(element, file, range.start, startOffsets, endOffsets)
|
result.addReferenceDataInsideElement(element, file, range.start, startOffsets, endOffsets)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun MutableCollection<KotlinReferenceData>.addReferenceDataFromElement(
|
private fun MutableCollection<KotlinReferenceData>.addReferenceDataInsideElement(
|
||||||
element: PsiElement,
|
element: PsiElement,
|
||||||
file: JetFile,
|
file: JetFile,
|
||||||
startOffset: Int,
|
startOffset: Int,
|
||||||
startOffsets: IntArray,
|
startOffsets: IntArray,
|
||||||
endOffsets: IntArray
|
endOffsets: IntArray
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if (PsiTreeUtil.getParentOfType(element, *IGNORE_REFERENCES_INSIDE) != null) return
|
if (PsiTreeUtil.getParentOfType(element, *IGNORE_REFERENCES_INSIDE) != null) return
|
||||||
|
|
||||||
val reference = element.getReference() as? JetReference ?: return
|
element.accept(object : PsiElementVisitor() {
|
||||||
|
override fun visitElement(element: PsiElement) {
|
||||||
|
element.acceptChildren(this)
|
||||||
|
|
||||||
val descriptors = reference.resolveToDescriptors((element as JetElement).analyze()) //TODO: we could use partial body resolve for all references together
|
val reference = element.getReference() as? JetReference ?: return
|
||||||
//check whether this reference is unambiguous
|
|
||||||
if (reference !is JetMultiReference<*> && descriptors.size() > 1) return
|
|
||||||
|
|
||||||
for (descriptor in descriptors) {
|
val descriptors = reference.resolveToDescriptors((element as JetElement).analyze()) //TODO: we could use partial body resolve for all references together
|
||||||
val declarations = DescriptorToSourceUtilsIde.getAllDeclarations(file.getProject(), descriptor)
|
//check whether this reference is unambiguous
|
||||||
val declaration = declarations.singleOrNull()
|
if (reference !is JetMultiReference<*> && descriptors.size() > 1) return
|
||||||
if (declaration != null && declaration.isInCopiedArea(file, startOffsets, endOffsets)) continue
|
|
||||||
|
|
||||||
if (!descriptor.isExtension) {
|
for (descriptor in descriptors) {
|
||||||
if (element !is JetNameReferenceExpression) continue
|
val declarations = DescriptorToSourceUtilsIde.getAllDeclarations(file.getProject(), descriptor)
|
||||||
if (element.getIdentifier() == null) continue // skip 'this' etc
|
val declaration = declarations.singleOrNull()
|
||||||
if (element.getReceiverExpression() != null) continue
|
if (declaration != null && declaration.isInCopiedArea(file, startOffsets, endOffsets)) continue
|
||||||
|
|
||||||
|
if (!descriptor.isExtension) {
|
||||||
|
if (element !is JetNameReferenceExpression) continue
|
||||||
|
if (element.getIdentifier() == null) continue // skip 'this' etc
|
||||||
|
if (element.getReceiverExpression() != null) continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val fqName = descriptor.importableFqName ?: continue
|
||||||
|
if (!descriptor.canBeReferencedViaImport()) continue
|
||||||
|
|
||||||
|
val kind = referenceDataKind(descriptor) ?: continue
|
||||||
|
add(KotlinReferenceData(element.range.start - startOffset, element.range.end - startOffset, fqName.asString(), kind))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
val fqName = descriptor.importableFqName ?: continue
|
|
||||||
if (!descriptor.canBeReferencedViaImport()) continue
|
|
||||||
|
|
||||||
val kind = referenceDataKind(descriptor) ?: continue
|
|
||||||
add(KotlinReferenceData(element.range.start - startOffset, element.range.end - startOffset, fqName.asString(), kind))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun referenceDataKind(descriptor: DeclarationDescriptor): KotlinReferenceData.Kind? {
|
private fun referenceDataKind(descriptor: DeclarationDescriptor): KotlinReferenceData.Kind? {
|
||||||
|
|||||||
Reference in New Issue
Block a user