KotlinCopyPasteReferenceProcessor: cleanup code

This commit is contained in:
Dmitry Gridin
2019-08-14 13:54:00 +07:00
parent bd5476082b
commit 693f42f33e
@@ -85,10 +85,8 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
val data = content.getTransferData(flavor) as? KotlinReferenceTransferableData ?: return listOf() val data = content.getTransferData(flavor) as? KotlinReferenceTransferableData ?: return listOf()
// copy to prevent changing of original by convertLineSeparators // copy to prevent changing of original by convertLineSeparators
return listOf(data.clone()) return listOf(data.clone())
} } catch (ignored: UnsupportedFlavorException) {
catch (ignored: UnsupportedFlavorException) { } catch (ignored: IOException) {
}
catch (ignored: IOException) {
} }
} }
@@ -105,14 +103,12 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
val collectedData = try { val collectedData = try {
collectReferenceData(file, startOffsets, endOffsets) collectReferenceData(file, startOffsets, endOffsets)
} } catch (e: ProcessCanceledException) {
catch (e: ProcessCanceledException) {
// supposedly analysis can only be canceled from another thread // supposedly analysis can only be canceled from another thread
// do not log ProcessCanceledException as it is rethrown by IdeaLogger and code won't be copied // do not log ProcessCanceledException as it is rethrown by IdeaLogger and code won't be copied
LOG.debug("ProcessCanceledException while analyzing references in ${file.getName()}. References can't be processed.") LOG.debug("ProcessCanceledException while analyzing references in ${file.getName()}. References can't be processed.")
return listOf() return listOf()
} } catch (e: Throwable) {
catch (e: Throwable) {
LOG.error("Exception in processing references for copy paste in file ${file.getName()}}", e) LOG.error("Exception in processing references for copy paste in file ${file.getName()}}", e)
return listOf() return listOf()
} }
@@ -128,8 +124,8 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
endOffsets: IntArray endOffsets: IntArray
): List<KotlinReferenceData> { ): List<KotlinReferenceData> {
val ranges = toTextRanges(startOffsets, endOffsets) val ranges = toTextRanges(startOffsets, endOffsets)
val elementsByRange = ranges.associateBy({ it }, { val elementsByRange = ranges.associateBy({ it }, { textRange ->
file.elementsInRange(it).filter { it is KtElement || it is KDocElement } file.elementsInRange(textRange).filter { it is KtElement || it is KDocElement }
}) })
val allElementsToResolve = elementsByRange.values.flatten().flatMap { it.collectDescendantsOfType<KtElement>() } val allElementsToResolve = elementsByRange.values.flatten().flatMap { it.collectDescendantsOfType<KtElement>() }
@@ -220,14 +216,17 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
} }
} }
private fun findReferencesToRestore(file: PsiFile, blockStart: Int, referenceData: Array<out KotlinReferenceData>): List<ReferenceToRestoreData> { private fun findReferencesToRestore(
file: PsiFile,
blockStart: Int,
referenceData: Array<out KotlinReferenceData>
): List<ReferenceToRestoreData> {
if (file !is KtFile) return listOf() if (file !is KtFile) return listOf()
val references = referenceData.map { it to findReference(it, file, blockStart) } val references = referenceData.map { it to findReference(it, file, blockStart) }
val bindingContext = try { val bindingContext = try {
file.getResolutionFacade().analyze(references.mapNotNull { it.second?.element }, BodyResolveMode.PARTIAL) file.getResolutionFacade().analyze(references.mapNotNull { it.second?.element }, BodyResolveMode.PARTIAL)
} } catch (e: Throwable) {
catch (e: Throwable) {
LOG.error("Failed to analyze references after copy paste", e) LOG.error("Failed to analyze references after copy paste", e)
return emptyList() return emptyList()
} }
@@ -272,8 +271,7 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
if (fileResolutionScope.findFunction(name, NoLookupLocation.FROM_IDE) { it.importableFqName == originalFqName } != null) { if (fileResolutionScope.findFunction(name, NoLookupLocation.FROM_IDE) { it.importableFqName == originalFqName } != null) {
return null // already imported return null // already imported
} }
} } else if (refData.kind == KotlinReferenceData.Kind.PROPERTY) {
else if (refData.kind == KotlinReferenceData.Kind.PROPERTY) {
if (fileResolutionScope.findVariable(name, NoLookupLocation.FROM_IDE) { it.importableFqName == originalFqName } != null) { if (fileResolutionScope.findVariable(name, NoLookupLocation.FROM_IDE) { it.importableFqName == originalFqName } != null) {
return null // already imported return null // already imported
} }
@@ -324,8 +322,7 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
if (reference is KtSimpleNameReference) { if (reference is KtSimpleNameReference) {
val pointer = smartPointerManager.createSmartPsiElementPointer(reference.element, file) val pointer = smartPointerManager.createSmartPsiElementPointer(reference.element, file)
bindingRequests.add(BindingRequest(pointer, fqName)) bindingRequests.add(BindingRequest(pointer, fqName))
} } else if (reference is KDocReference) {
else if (reference is KDocReference) {
descriptorsToImport.addAll(findImportableDescriptors(fqName, file)) descriptorsToImport.addAll(findImportableDescriptors(fqName, file))
} }
} else { } else {
@@ -350,10 +347,13 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
} }
} }
private fun findCallableToImport(fqName: FqName, file: KtFile): CallableDescriptor? private fun findCallableToImport(fqName: FqName, file: KtFile): CallableDescriptor? =
= findImportableDescriptors(fqName, file).firstIsInstanceOrNull<CallableDescriptor>() findImportableDescriptors(fqName, file).firstIsInstanceOrNull()
private fun showRestoreReferencesDialog(project: Project, referencesToRestore: List<ReferenceToRestoreData>): Collection<ReferenceToRestoreData> { private fun showRestoreReferencesDialog(
project: Project,
referencesToRestore: List<ReferenceToRestoreData>
): Collection<ReferenceToRestoreData> {
val fqNames = referencesToRestore.asSequence().map { it.refData.fqName }.toSortedSet() val fqNames = referencesToRestore.asSequence().map { it.refData.fqName }.toSortedSet()
if (ApplicationManager.getApplication().isUnitTestMode) { if (ApplicationManager.getApplication().isUnitTestMode) {
@@ -383,6 +383,7 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<KotlinReference
} }
companion object { companion object {
@get:TestOnly var declarationsToImportSuggested: Collection<String> = emptyList() @get:TestOnly
var declarationsToImportSuggested: Collection<String> = emptyList()
} }
} }