Do not resolve references pasted into the same origin
#KT-37993 Fixed
This commit is contained in:
+2
-1
@@ -16,7 +16,8 @@ class BasicKotlinReferenceTransferableData(
|
||||
val imports: List<String>,
|
||||
val sourceTextOffset: Int,
|
||||
val sourceText: String,
|
||||
val textRanges: List<TextRange>
|
||||
val textRanges: List<TextRange>,
|
||||
val locationFqName: String?
|
||||
) : TextBlockTransferableData, Cloneable, Serializable {
|
||||
override fun getFlavor() = dataFlavor
|
||||
override fun getOffsetCount() = 0
|
||||
|
||||
+33
-1
@@ -117,6 +117,11 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<BasicKotlinRefe
|
||||
val text = file.text.substring(offsetDelta)
|
||||
val ranges = startOffsets.indices.map { TextRange(startOffsets[it], endOffsets[it]) }
|
||||
|
||||
val locationFqName = if (startOffsets.size == 1) {
|
||||
val fqName = file.namedDeclarationFqName(startOffsets[0] - 1)
|
||||
fqName?.takeIf { it == file.namedDeclarationFqName(endOffsets[0] + 1) }
|
||||
} else null
|
||||
|
||||
return listOf(
|
||||
BasicKotlinReferenceTransferableData(
|
||||
sourceFileUrl = file.virtualFile.url,
|
||||
@@ -124,11 +129,17 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<BasicKotlinRefe
|
||||
imports = imports,
|
||||
sourceTextOffset = offsetDelta,
|
||||
sourceText = text,
|
||||
textRanges = ranges
|
||||
textRanges = ranges,
|
||||
locationFqName = locationFqName
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun KtFile.namedDeclarationFqName(offset: Int): String? =
|
||||
if (offset in 0 until textLength)
|
||||
PsiTreeUtil.getNonStrictParentOfType(this.findElementAt(offset), KtNamedDeclaration::class.java)?.fqName?.asString()
|
||||
else null
|
||||
|
||||
private fun collectReferenceData(
|
||||
textRanges: List<TextRange>,
|
||||
file: KtFile,
|
||||
@@ -267,9 +278,30 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<BasicKotlinRefe
|
||||
val file = PsiDocumentManager.getInstance(project).getPsiFile(document)
|
||||
if (file !is KtFile) return
|
||||
|
||||
if (isPastedToTheSameOrigin(file, caretOffset, values)) return
|
||||
|
||||
processReferenceData(project, editor, file, bounds.startOffset, values.single())
|
||||
}
|
||||
|
||||
private fun isPastedToTheSameOrigin(
|
||||
file: KtFile,
|
||||
caretOffset: Int,
|
||||
values: List<BasicKotlinReferenceTransferableData>
|
||||
): Boolean {
|
||||
if (values.size == 1 && values[0].sourceFileUrl == file.virtualFile.url &&
|
||||
values[0].locationFqName != null &&
|
||||
// check locationFqName on position before pasted snippet
|
||||
values[0].locationFqName == file.namedDeclarationFqName(caretOffset - 1)
|
||||
) {
|
||||
val currentImports = file.importDirectives.map { it.text }.toSet()
|
||||
val originalImports = values.flatMap { it.imports }.toSet()
|
||||
if (currentImports == originalImports) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun processReferenceData(
|
||||
project: Project,
|
||||
editor: Editor,
|
||||
|
||||
Reference in New Issue
Block a user