Revert "Perform shorten references under modal dialog"
This reverts commit 0b822aa4
This commit is contained in:
+5
-19
@@ -16,20 +16,17 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.codeInsight.shorten
|
package org.jetbrains.kotlin.idea.codeInsight.shorten
|
||||||
|
|
||||||
import com.intellij.openapi.application.runReadAction
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.Key
|
import com.intellij.openapi.util.Key
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.codeStyle.CodeStyleManager
|
|
||||||
import org.jetbrains.kotlin.asJava.unwrapped
|
import org.jetbrains.kotlin.asJava.unwrapped
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
|
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.util.getJavaMemberDescriptor
|
import org.jetbrains.kotlin.idea.caches.resolve.util.getJavaMemberDescriptor
|
||||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||||
import org.jetbrains.kotlin.idea.core.ShortenReferences.Options
|
import org.jetbrains.kotlin.idea.core.ShortenReferences.Options
|
||||||
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
||||||
import org.jetbrains.kotlin.idea.util.application.assertIsDispatchThread
|
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
||||||
@@ -76,14 +73,14 @@ private fun Project.getOrCreateRefactoringRequests(): MutableSet<DelayedRefactor
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun KtElement.addToShorteningWaitSet(options: Options = Options.DEFAULT) {
|
fun KtElement.addToShorteningWaitSet(options: Options = Options.DEFAULT) {
|
||||||
assertIsDispatchThread()
|
assert(ApplicationManager.getApplication()!!.isWriteAccessAllowed) { "Write access needed" }
|
||||||
val project = project
|
val project = project
|
||||||
val elementPointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(this)
|
val elementPointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(this)
|
||||||
project.getOrCreateRefactoringRequests().add(ShorteningRequest(elementPointer, options))
|
project.getOrCreateRefactoringRequests().add(ShorteningRequest(elementPointer, options))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addDelayedImportRequest(elementToImport: PsiElement, file: KtFile) {
|
fun addDelayedImportRequest(elementToImport: PsiElement, file: KtFile) {
|
||||||
assertIsDispatchThread()
|
assert(ApplicationManager.getApplication()!!.isWriteAccessAllowed) { "Write access needed" }
|
||||||
file.project.getOrCreateRefactoringRequests() += ImportRequest(elementToImport.createSmartPointer(), file.createSmartPointer())
|
file.project.getOrCreateRefactoringRequests() += ImportRequest(elementToImport.createSmartPointer(), file.createSmartPointer())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,29 +98,18 @@ fun performDelayedRefactoringRequests(project: Project) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val elementToOptions = shorteningRequests.mapNotNull { req ->
|
val elementToOptions = shorteningRequests.mapNotNull { req -> req.pointer.element?.let { it to req.options } }.toMap()
|
||||||
runReadAction { req.pointer.element }?.let { it to req.options }
|
|
||||||
}.toMap()
|
|
||||||
val elements = elementToOptions.keys
|
val elements = elementToOptions.keys
|
||||||
val files = runReadAction { elements.map(KtElement::getContainingKtFile).toSet() }
|
|
||||||
//TODO: this is not correct because it should not shorten deep into the elements!
|
//TODO: this is not correct because it should not shorten deep into the elements!
|
||||||
ShortenReferences { elementToOptions[it] ?: Options.DEFAULT }.process(elements)
|
ShortenReferences { elementToOptions[it] ?: Options.DEFAULT }.process(elements)
|
||||||
|
|
||||||
files.forEach { file ->
|
|
||||||
file.importList?.let { importList ->
|
|
||||||
runWriteAction {
|
|
||||||
CodeStyleManager.getInstance(file.project).reformat(importList, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val importInsertHelper = ImportInsertHelper.getInstance(project)
|
val importInsertHelper = ImportInsertHelper.getInstance(project)
|
||||||
|
|
||||||
for ((file, requestsForFile) in importRequests.groupBy { it.filePointer.element }) {
|
for ((file, requestsForFile) in importRequests.groupBy { it.filePointer.element }) {
|
||||||
if (file == null) continue
|
if (file == null) continue
|
||||||
|
|
||||||
for (requestForFile in requestsForFile) {
|
for (requestForFile in requestsForFile) {
|
||||||
val elementToImport = runReadAction { requestForFile.elementToImportPointer.element }?.unwrapped ?: continue
|
val elementToImport = requestForFile.elementToImportPointer.element?.unwrapped ?: continue
|
||||||
val descriptorToImport = when (elementToImport) {
|
val descriptorToImport = when (elementToImport) {
|
||||||
is KtDeclaration -> elementToImport.unsafeResolveToDescriptor(BodyResolveMode.PARTIAL)
|
is KtDeclaration -> elementToImport.unsafeResolveToDescriptor(BodyResolveMode.PARTIAL)
|
||||||
is PsiMember -> elementToImport.getJavaMemberDescriptor()
|
is PsiMember -> elementToImport.getJavaMemberDescriptor()
|
||||||
|
|||||||
+10
-13
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.references
|
|||||||
import com.intellij.openapi.extensions.Extensions
|
import com.intellij.openapi.extensions.Extensions
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.PsiReference
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import com.intellij.util.IncorrectOperationException
|
import com.intellij.util.IncorrectOperationException
|
||||||
import com.intellij.util.SmartList
|
import com.intellij.util.SmartList
|
||||||
@@ -20,7 +21,6 @@ import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
|
|||||||
import org.jetbrains.kotlin.idea.core.*
|
import org.jetbrains.kotlin.idea.core.*
|
||||||
import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
|
import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
|
||||||
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
|
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
|
||||||
import org.jetbrains.kotlin.idea.util.application.disablePostprocessFormattingInside
|
|
||||||
import org.jetbrains.kotlin.lexer.KtToken
|
import org.jetbrains.kotlin.lexer.KtToken
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
|
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
|
||||||
@@ -171,18 +171,15 @@ class KtSimpleNameReferenceDescriptorsImpl(
|
|||||||
if (expression !is KtNameReferenceExpression) return expression
|
if (expression !is KtNameReferenceExpression) return expression
|
||||||
if (expression.parent is KtThisExpression || expression.parent is KtSuperExpression) return expression // TODO: it's a bad design of PSI tree, we should change it
|
if (expression.parent is KtThisExpression || expression.parent is KtSuperExpression) return expression // TODO: it's a bad design of PSI tree, we should change it
|
||||||
|
|
||||||
val newExpression =
|
val newExpression = expression.changeQualifiedName(
|
||||||
expression.project.disablePostprocessFormattingInside {
|
fqName.quoteIfNeeded().let {
|
||||||
expression.changeQualifiedName(
|
if (shorteningMode == ShorteningMode.NO_SHORTENING)
|
||||||
fqName.quoteIfNeeded().let {
|
it
|
||||||
if (shorteningMode == ShorteningMode.NO_SHORTENING)
|
else
|
||||||
it
|
it.withRootPrefixIfNeeded(expression)
|
||||||
else
|
},
|
||||||
it.withRootPrefixIfNeeded(expression)
|
targetElement
|
||||||
},
|
)
|
||||||
targetElement
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val newQualifiedElement = newExpression.getQualifiedElementOrCallableRef()
|
val newQualifiedElement = newExpression.getQualifiedElementOrCallableRef()
|
||||||
|
|
||||||
if (shorteningMode == ShorteningMode.NO_SHORTENING) return newExpression
|
if (shorteningMode == ShorteningMode.NO_SHORTENING) return newExpression
|
||||||
|
|||||||
@@ -15,5 +15,3 @@ text.loading.kotlin.script.definitions=Loading kotlin script definitions
|
|||||||
text.loading.kotlin.script.configuration=Loading script configuration
|
text.loading.kotlin.script.configuration=Loading script configuration
|
||||||
implement.members.handler.title=Implement Members
|
implement.members.handler.title=Implement Members
|
||||||
override.members.handler.title=Override 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
|
package org.jetbrains.kotlin.idea.core
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import com.intellij.openapi.application.runReadAction
|
||||||
import com.intellij.openapi.progress.ProgressManager
|
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.SmartPsiElementPointer
|
import com.intellij.psi.SmartPsiElementPointer
|
||||||
|
import com.intellij.psi.impl.source.PostprocessReformattingAspect
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement
|
import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread
|
import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
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.canBeReferencedViaImport
|
||||||
import org.jetbrains.kotlin.idea.imports.getImportableTargets
|
import org.jetbrains.kotlin.idea.imports.getImportableTargets
|
||||||
import org.jetbrains.kotlin.idea.references.mainReference
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
import org.jetbrains.kotlin.idea.references.resolveToDescriptors
|
import org.jetbrains.kotlin.idea.references.resolveToDescriptors
|
||||||
import org.jetbrains.kotlin.idea.util.*
|
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.incremental.components.NoLookupLocation
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
@@ -59,8 +54,6 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val LOG = Logger.getInstance(ShortenReferences::class.java)
|
|
||||||
|
|
||||||
@JvmField
|
@JvmField
|
||||||
val DEFAULT = ShortenReferences()
|
val DEFAULT = ShortenReferences()
|
||||||
|
|
||||||
@@ -197,8 +190,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
//TODO: that's not correct since we have options!
|
//TODO: that's not correct since we have options!
|
||||||
val elementsToUse = dropNestedElements(elements)
|
val elementsToUse = dropNestedElements(elements)
|
||||||
|
|
||||||
val project = file.project
|
val helper = ImportInsertHelper.getInstance(file.project)
|
||||||
val helper = ImportInsertHelper.getInstance(project)
|
|
||||||
|
|
||||||
val failedToImportDescriptors = LinkedHashSet<DeclarationDescriptor>()
|
val failedToImportDescriptors = LinkedHashSet<DeclarationDescriptor>()
|
||||||
|
|
||||||
@@ -210,9 +202,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var pass = 0
|
|
||||||
while (true) {
|
while (true) {
|
||||||
pass++
|
|
||||||
// Processors order is important here so that enclosing elements are not shortened before their children are, e.g.
|
// Processors order is important here so that enclosing elements are not shortened before their children are, e.g.
|
||||||
// test.foo(this@A) -> foo(this)
|
// test.foo(this@A) -> foo(this)
|
||||||
val processors: List<ShorteningProcessor<*>> = runReadAction {
|
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)
|
||||||
// step 1: collect qualified elements to analyze (no resolve at this step)
|
val visitors = processors.map { it.collectElementsVisitor }
|
||||||
val visitors = processors.map { it.collectElementsVisitor }
|
runReadAction {
|
||||||
runReadAction {
|
for (visitor in visitors) {
|
||||||
for (visitor in visitors) {
|
for (element in elementsToUse) {
|
||||||
for (element in elementsToUse) {
|
visitor.options = options(element)
|
||||||
visitor.options = options(element)
|
element.accept(visitor)
|
||||||
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")
|
// step 2: analyze collected elements with resolve and decide which can be shortened now and which need descriptors to be imported before shortening
|
||||||
step1And2.invoke()
|
val allElementsToAnalyze = visitors.flatMap { visitor -> visitor.getElementsToAnalyze().map { it.element } }.toSet()
|
||||||
} else {
|
val bindingContext = allowResolveInDispatchThread {
|
||||||
ProgressManager.getInstance().runProcessWithProgressSynchronously(
|
file.getResolutionFacade().analyze(allElementsToAnalyze, BodyResolveMode.PARTIAL_WITH_CFA)
|
||||||
step1And2, KotlinIdeaCoreBundle.message("shorten.references.pass.0", pass), false, project
|
}
|
||||||
)
|
|
||||||
|
processors.forEach { it.analyzeCollectedElements(bindingContext) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// step 3: shorten elements that can be shortened right now
|
// 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) {
|
for (descriptor in descriptorsToImport) {
|
||||||
assert(descriptor !in failedToImportDescriptors)
|
assert(descriptor !in failedToImportDescriptors)
|
||||||
|
|
||||||
val result =
|
val result = helper.importDescriptor(file, descriptor)
|
||||||
runWriteAction {
|
|
||||||
helper.importDescriptor(file, descriptor)
|
|
||||||
}
|
|
||||||
if (result != ImportDescriptorResult.ALREADY_IMPORTED) {
|
if (result != ImportDescriptorResult.ALREADY_IMPORTED) {
|
||||||
anyChange = true
|
anyChange = true
|
||||||
}
|
}
|
||||||
@@ -417,9 +387,10 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
val element = elementPointer.element ?: continue
|
val element = elementPointer.element ?: continue
|
||||||
if (!element.isValid) 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)
|
// we never want any reformatting to happen because sometimes it causes strange effects (see KT-11633)
|
||||||
val newElement = element.project.disablePostprocessFormattingInside {
|
PostprocessReformattingAspect.getInstance(element.project).disablePostprocessFormattingInside {
|
||||||
runWriteAction { shortenElement(element, options(element)) }
|
newElement = shortenElement(element, options(element))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (element in elementSetToUpdate && newElement != element) {
|
if (element in elementSetToUpdate && newElement != element) {
|
||||||
@@ -431,10 +402,8 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
|
|
||||||
fun removeRootPrefixes() {
|
fun removeRootPrefixes() {
|
||||||
for (pointer in collectElementsVisitor.getElementsWithRootPrefix()) {
|
for (pointer in collectElementsVisitor.getElementsWithRootPrefix()) {
|
||||||
val element = runReadAction { pointer.element } ?: continue
|
val element = pointer.element ?: continue
|
||||||
runWriteAction {
|
shortenElement(element, Options.DEFAULT)
|
||||||
shortenElement(element, Options.DEFAULT)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-13
@@ -11,8 +11,6 @@ import com.intellij.openapi.components.ComponentManager
|
|||||||
import com.intellij.openapi.progress.impl.CancellationCheck
|
import com.intellij.openapi.progress.impl.CancellationCheck
|
||||||
import com.intellij.openapi.project.DumbService
|
import com.intellij.openapi.project.DumbService
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.Computable
|
|
||||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect
|
|
||||||
|
|
||||||
fun <T> runReadAction(action: () -> T): T {
|
fun <T> runReadAction(action: () -> T): T {
|
||||||
return ApplicationManager.getApplication().runReadAction<T>(action)
|
return ApplicationManager.getApplication().runReadAction<T>(action)
|
||||||
@@ -55,15 +53,6 @@ inline fun invokeLater(crossinline action: () -> Unit) =
|
|||||||
ApplicationManager.getApplication().invokeLater { action() }
|
ApplicationManager.getApplication().invokeLater { action() }
|
||||||
|
|
||||||
inline fun isUnitTestMode(): Boolean = ApplicationManager.getApplication().isUnitTestMode
|
inline fun isUnitTestMode(): Boolean = ApplicationManager.getApplication().isUnitTestMode
|
||||||
inline fun isReadAccessAllowed() = ApplicationManager.getApplication().isReadAccessAllowed
|
|
||||||
inline fun isWriteAccessAllowed() = ApplicationManager.getApplication().isWriteAccessAllowed
|
|
||||||
inline fun isDispatchThread() = ApplicationManager.getApplication().isDispatchThread
|
|
||||||
inline fun assertWriteAccessAllowed() = ApplicationManager.getApplication().assertWriteAccessAllowed()
|
|
||||||
inline fun assertReadAccessAllowed() = ApplicationManager.getApplication().assertReadAccessAllowed()
|
|
||||||
inline fun assertIsDispatchThread() = ApplicationManager.getApplication().assertIsDispatchThread()
|
|
||||||
|
|
||||||
inline fun <reified T : Any> Project.disablePostprocessFormattingInside(action: Computable<T>): T =
|
|
||||||
PostprocessReformattingAspect.getInstance(this).disablePostprocessFormattingInside(action)
|
|
||||||
|
|
||||||
inline fun <reified T : Any> ComponentManager.getServiceSafe(): T =
|
inline fun <reified T : Any> ComponentManager.getServiceSafe(): T =
|
||||||
this.getService(T::class.java) ?: error("Unable to locate service ${T::class.java.name}")
|
this.getService(T::class.java) ?: error("Unable to locate service ${T::class.java.name}")
|
||||||
@@ -71,5 +60,4 @@ inline fun <reified T : Any> ComponentManager.getServiceSafe(): T =
|
|||||||
fun <T> Project.runReadActionInSmartMode(action: () -> T): T {
|
fun <T> Project.runReadActionInSmartMode(action: () -> T): T {
|
||||||
if (ApplicationManager.getApplication().isReadAccessAllowed) return action()
|
if (ApplicationManager.getApplication().isReadAccessAllowed) return action()
|
||||||
return DumbService.getInstance(this).runReadActionInSmartMode<T>(action)
|
return DumbService.getInstance(this).runReadActionInSmartMode<T>(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
-13
@@ -11,8 +11,6 @@ import com.intellij.openapi.components.ComponentManager
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.annotations.Nls
|
import org.jetbrains.annotations.Nls
|
||||||
import com.intellij.openapi.project.DumbService
|
import com.intellij.openapi.project.DumbService
|
||||||
import com.intellij.openapi.util.Computable
|
|
||||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect
|
|
||||||
|
|
||||||
fun <T> runReadAction(action: () -> T): T {
|
fun <T> runReadAction(action: () -> T): T {
|
||||||
return ApplicationManager.getApplication().runReadAction<T>(action)
|
return ApplicationManager.getApplication().runReadAction<T>(action)
|
||||||
@@ -46,17 +44,6 @@ inline fun invokeLater(crossinline action: () -> Unit) =
|
|||||||
ApplicationManager.getApplication().invokeLater { action() }
|
ApplicationManager.getApplication().invokeLater { action() }
|
||||||
|
|
||||||
inline fun isUnitTestMode(): Boolean = ApplicationManager.getApplication().isUnitTestMode
|
inline fun isUnitTestMode(): Boolean = ApplicationManager.getApplication().isUnitTestMode
|
||||||
inline fun isReadAccessAllowed() = ApplicationManager.getApplication().isReadAccessAllowed
|
|
||||||
inline fun isWriteAccessAllowed() = ApplicationManager.getApplication().isWriteAccessAllowed
|
|
||||||
inline fun isDispatchThread() = ApplicationManager.getApplication().isDispatchThread
|
|
||||||
inline fun assertWriteAccessAllowed() =
|
|
||||||
assert(isWriteAccessAllowed()) { "Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())" }
|
|
||||||
inline fun assertReadAccessAllowed() =
|
|
||||||
assert(isReadAccessAllowed()) { "Read access is allowed from event dispatch thread or inside read-action only (see com.intellij.openapi.application.Application.runReadAction())" }
|
|
||||||
inline fun assertIsDispatchThread() = ApplicationManager.getApplication().assertIsDispatchThread()
|
|
||||||
|
|
||||||
inline fun <reified T : Any> Project.disablePostprocessFormattingInside(action: Computable<T>): T =
|
|
||||||
PostprocessReformattingAspect.getInstance(this).disablePostprocessFormattingInside(action)
|
|
||||||
|
|
||||||
inline fun <reified T : Any> ComponentManager.getServiceSafe(): T =
|
inline fun <reified T : Any> ComponentManager.getServiceSafe(): T =
|
||||||
this.getService(T::class.java) ?: error("Unable to locate service ${T::class.java.name}")
|
this.getService(T::class.java) ?: error("Unable to locate service ${T::class.java.name}")
|
||||||
|
|||||||
-13
@@ -25,8 +25,6 @@ import com.intellij.openapi.progress.ProgressIndicatorProvider
|
|||||||
import com.intellij.openapi.progress.ProgressManager
|
import com.intellij.openapi.progress.ProgressManager
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.project.DumbService
|
import com.intellij.openapi.project.DumbService
|
||||||
import com.intellij.openapi.util.Computable
|
|
||||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect
|
|
||||||
|
|
||||||
fun <T> runReadAction(action: () -> T): T {
|
fun <T> runReadAction(action: () -> T): T {
|
||||||
return ApplicationManager.getApplication().runReadAction<T>(action)
|
return ApplicationManager.getApplication().runReadAction<T>(action)
|
||||||
@@ -60,17 +58,6 @@ inline fun invokeLater(crossinline action: () -> Unit) =
|
|||||||
ApplicationManager.getApplication().invokeLater { action() }
|
ApplicationManager.getApplication().invokeLater { action() }
|
||||||
|
|
||||||
inline fun isUnitTestMode(): Boolean = ApplicationManager.getApplication().isUnitTestMode
|
inline fun isUnitTestMode(): Boolean = ApplicationManager.getApplication().isUnitTestMode
|
||||||
inline fun isReadAccessAllowed() = ApplicationManager.getApplication().isReadAccessAllowed
|
|
||||||
inline fun isWriteAccessAllowed() = ApplicationManager.getApplication().isWriteAccessAllowed
|
|
||||||
inline fun isDispatchThread() = ApplicationManager.getApplication().isDispatchThread
|
|
||||||
inline fun assertWriteAccessAllowed() =
|
|
||||||
assert(isWriteAccessAllowed()) { "Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())" }
|
|
||||||
inline fun assertReadAccessAllowed() =
|
|
||||||
assert(isReadAccessAllowed()) { "Read access is allowed from event dispatch thread or inside read-action only (see com.intellij.openapi.application.Application.runReadAction())" }
|
|
||||||
inline fun assertIsDispatchThread() = ApplicationManager.getApplication().assertIsDispatchThread()
|
|
||||||
|
|
||||||
inline fun <reified T : Any> Project.disablePostprocessFormattingInside(action: Computable<T>): T =
|
|
||||||
PostprocessReformattingAspect.getInstance(this).disablePostprocessFormattingInside(action)
|
|
||||||
|
|
||||||
inline fun <reified T : Any> ComponentManager.getServiceSafe(): T =
|
inline fun <reified T : Any> ComponentManager.getServiceSafe(): T =
|
||||||
this.getService(T::class.java) ?: error("Unable to locate service ${T::class.java.name}")
|
this.getService(T::class.java) ?: error("Unable to locate service ${T::class.java.name}")
|
||||||
|
|||||||
+20
-22
@@ -9,7 +9,6 @@ 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.application.ApplicationManager
|
||||||
import com.intellij.openapi.application.ReadAction
|
import com.intellij.openapi.application.ReadAction
|
||||||
import com.intellij.openapi.command.CommandProcessor
|
|
||||||
import com.intellij.openapi.diagnostic.ControlFlowException
|
import com.intellij.openapi.diagnostic.ControlFlowException
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
@@ -44,8 +43,10 @@ import org.jetbrains.kotlin.idea.core.util.start
|
|||||||
import org.jetbrains.kotlin.idea.imports.importableFqName
|
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||||
import org.jetbrains.kotlin.idea.references.*
|
import org.jetbrains.kotlin.idea.references.*
|
||||||
import org.jetbrains.kotlin.idea.util.*
|
import org.jetbrains.kotlin.idea.util.*
|
||||||
import org.jetbrains.kotlin.idea.util.application.*
|
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
|
||||||
import org.jetbrains.kotlin.idea.util.runReadActionInSmartMode
|
import org.jetbrains.kotlin.idea.util.application.invokeLater
|
||||||
|
import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
|
||||||
|
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||||
import org.jetbrains.kotlin.kdoc.psi.api.KDocElement
|
import org.jetbrains.kotlin.kdoc.psi.api.KDocElement
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
@@ -375,10 +376,12 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<BasicKotlinRefe
|
|||||||
showRestoreReferencesDialog(project, referencesPossibleToRestore)
|
showRestoreReferencesDialog(project, referencesPossibleToRestore)
|
||||||
if (selectedReferencesToRestore.isEmpty()) return@invokeLater
|
if (selectedReferencesToRestore.isEmpty()) return@invokeLater
|
||||||
|
|
||||||
val imported = TreeSet<String>()
|
project.executeWriteCommand(KotlinBundle.message("resolve.pasted.references")) {
|
||||||
restoreReferences(selectedReferencesToRestore, file, imported)
|
val imported = TreeSet<String>()
|
||||||
|
restoreReferences(selectedReferencesToRestore, file, imported)
|
||||||
|
|
||||||
reviewAddedImports(project, editor, file, imported)
|
reviewAddedImports(project, editor, file, imported)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -686,9 +689,8 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<BasicKotlinRefe
|
|||||||
file: KtFile,
|
file: KtFile,
|
||||||
imported: MutableSet<String>
|
imported: MutableSet<String>
|
||||||
) {
|
) {
|
||||||
val project = file.project
|
val importHelper = ImportInsertHelper.getInstance(file.project)
|
||||||
val importHelper = ImportInsertHelper.getInstance(project)
|
val smartPointerManager = SmartPointerManager.getInstance(file.project)
|
||||||
val smartPointerManager = SmartPointerManager.getInstance(project)
|
|
||||||
|
|
||||||
data class BindingRequest(
|
data class BindingRequest(
|
||||||
val pointer: SmartPsiElementPointer<KtSimpleNameExpression>,
|
val pointer: SmartPsiElementPointer<KtSimpleNameExpression>,
|
||||||
@@ -715,22 +717,18 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor<BasicKotlinRefe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
project.executeWriteCommand(KotlinBundle.message("resolve.pasted.references")) {
|
for (descriptor in descriptorsToImport) {
|
||||||
for (descriptor in descriptorsToImport) {
|
importHelper.importDescriptor(file, descriptor)
|
||||||
importHelper.importDescriptor(file, descriptor)
|
descriptor.getImportableDescriptor().importableFqName?.let { imported.add(it.asString()) }
|
||||||
descriptor.getImportableDescriptor().importableFqName?.let { imported.add(it.asString()) }
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for ((pointer, fqName) in bindingRequests) {
|
for ((pointer, fqName) in bindingRequests) {
|
||||||
pointer.element?.mainReference?.let {
|
pointer.element?.mainReference?.let {
|
||||||
it.bindToFqName(fqName, KtSimpleNameReference.ShorteningMode.DELAYED_SHORTENING)
|
it.bindToFqName(fqName, KtSimpleNameReference.ShorteningMode.DELAYED_SHORTENING)
|
||||||
imported.add(fqName.asString())
|
imported.add(fqName.asString())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CommandProcessor.getInstance().runUndoTransparentAction {
|
performDelayedRefactoringRequests(file.project)
|
||||||
performDelayedRefactoringRequests(file.project)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findImportableDescriptors(fqName: FqName, file: KtFile): Collection<DeclarationDescriptor> {
|
private fun findImportableDescriptors(fqName: FqName, file: KtFile): Collection<DeclarationDescriptor> {
|
||||||
|
|||||||
Reference in New Issue
Block a user