Revert "Perform shorten references under modal dialog"

This reverts commit 0b822aa4
This commit is contained in:
Vladimir Dolzhenko
2020-10-05 18:00:02 +02:00
parent dcf18ec500
commit 71d76bdb4b
8 changed files with 60 additions and 150 deletions
@@ -15,5 +15,3 @@ text.loading.kotlin.script.definitions=Loading kotlin script definitions
text.loading.kotlin.script.configuration=Loading script configuration
implement.members.handler.title=Implement Members
override.members.handler.title=Override Members
shorten.references=Shortening references
shorten.references.pass.0=Shortening references, pass {0}
@@ -5,27 +5,22 @@
package org.jetbrains.kotlin.idea.core
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.SmartPsiElementPointer
import com.intellij.psi.impl.source.PostprocessReformattingAspect
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement
import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.core.util.KotlinIdeaCoreBundle
import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport
import org.jetbrains.kotlin.idea.imports.getImportableTargets
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.references.resolveToDescriptors
import org.jetbrains.kotlin.idea.util.*
import org.jetbrains.kotlin.idea.util.application.disablePostprocessFormattingInside
import org.jetbrains.kotlin.idea.util.application.isWriteAccessAllowed
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
@@ -59,8 +54,6 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
}
companion object {
private val LOG = Logger.getInstance(ShortenReferences::class.java)
@JvmField
val DEFAULT = ShortenReferences()
@@ -197,8 +190,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
//TODO: that's not correct since we have options!
val elementsToUse = dropNestedElements(elements)
val project = file.project
val helper = ImportInsertHelper.getInstance(project)
val helper = ImportInsertHelper.getInstance(file.project)
val failedToImportDescriptors = LinkedHashSet<DeclarationDescriptor>()
@@ -210,9 +202,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
}
}
var pass = 0
while (true) {
pass++
// Processors order is important here so that enclosing elements are not shortened before their children are, e.g.
// test.foo(this@A) -> foo(this)
val processors: List<ShorteningProcessor<*>> = runReadAction {
@@ -224,41 +214,24 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
)
}
val step1And2 = {
// step 1: collect qualified elements to analyze (no resolve at this step)
val visitors = processors.map { it.collectElementsVisitor }
runReadAction {
for (visitor in visitors) {
for (element in elementsToUse) {
visitor.options = options(element)
element.accept(visitor)
}
// step 1: collect qualified elements to analyze (no resolve at this step)
val visitors = processors.map { it.collectElementsVisitor }
runReadAction {
for (visitor in visitors) {
for (element in elementsToUse) {
visitor.options = options(element)
element.accept(visitor)
}
// step 2: analyze collected elements with resolve and decide which can be shortened now and which need descriptors to be imported before shortening
val allElementsToAnalyze = visitors.flatMap { visitor -> visitor.getElementsToAnalyze().map { it.element } }.toSet()
val bindingContext =
if (isWriteAccessAllowed()) {
allowResolveInDispatchThread {
file.getResolutionFacade().analyze(allElementsToAnalyze, BodyResolveMode.PARTIAL_WITH_CFA)
}
} else {
file.getResolutionFacade().analyze(allElementsToAnalyze, BodyResolveMode.PARTIAL_WITH_CFA)
}
processors.forEach { it.analyzeCollectedElements(bindingContext) }
}
}
if (isWriteAccessAllowed()) {
LOG.warn("Shorten reference should not be invoked under write action")
step1And2.invoke()
} else {
ProgressManager.getInstance().runProcessWithProgressSynchronously(
step1And2, KotlinIdeaCoreBundle.message("shorten.references.pass.0", pass), false, project
)
// step 2: analyze collected elements with resolve and decide which can be shortened now and which need descriptors to be imported before shortening
val allElementsToAnalyze = visitors.flatMap { visitor -> visitor.getElementsToAnalyze().map { it.element } }.toSet()
val bindingContext = allowResolveInDispatchThread {
file.getResolutionFacade().analyze(allElementsToAnalyze, BodyResolveMode.PARTIAL_WITH_CFA)
}
processors.forEach { it.analyzeCollectedElements(bindingContext) }
}
// step 3: shorten elements that can be shortened right now
@@ -274,10 +247,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
for (descriptor in descriptorsToImport) {
assert(descriptor !in failedToImportDescriptors)
val result =
runWriteAction {
helper.importDescriptor(file, descriptor)
}
val result = helper.importDescriptor(file, descriptor)
if (result != ImportDescriptorResult.ALREADY_IMPORTED) {
anyChange = true
}
@@ -417,9 +387,10 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
val element = elementPointer.element ?: continue
if (!element.isValid) continue
var newElement: KtElement? = null
// we never want any reformatting to happen because sometimes it causes strange effects (see KT-11633)
val newElement = element.project.disablePostprocessFormattingInside {
runWriteAction { shortenElement(element, options(element)) }
PostprocessReformattingAspect.getInstance(element.project).disablePostprocessFormattingInside {
newElement = shortenElement(element, options(element))
}
if (element in elementSetToUpdate && newElement != element) {
@@ -431,10 +402,8 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
fun removeRootPrefixes() {
for (pointer in collectElementsVisitor.getElementsWithRootPrefix()) {
val element = runReadAction { pointer.element } ?: continue
runWriteAction {
shortenElement(element, Options.DEFAULT)
}
val element = pointer.element ?: continue
shortenElement(element, Options.DEFAULT)
}
}