KT-16714 related refactoring of tryRunWriteAction (now runWriteAction is not used if intention is called from J2K)
This commit is contained in:
+1
-1
@@ -78,7 +78,7 @@ abstract class IntentionBasedInspection<TElement : PsiElement>(
|
|||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||||
|
|
||||||
val intentionsAndCheckers = intentionInfos.map {
|
val intentionsAndCheckers = intentionInfos.map {
|
||||||
val instance = it.intention.constructors.single().call()
|
val instance = it.intention.constructors.single { it.parameters.isEmpty() } .call()
|
||||||
instance.inspection = this
|
instance.inspection = this
|
||||||
instance to it.additionalChecker
|
instance to it.additionalChecker
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -58,8 +58,8 @@ abstract class SelfTargetingIntention<TElement : PsiElement>(
|
|||||||
|
|
||||||
abstract fun applyTo(element: TElement, editor: Editor?)
|
abstract fun applyTo(element: TElement, editor: Editor?)
|
||||||
|
|
||||||
protected fun <R> tryRunWriteAction(action: () -> R): R =
|
protected fun <R> runInWriteActionOrHere(inWriteAction: Boolean = true, action: () -> R): R =
|
||||||
if (ApplicationManager.getApplication().isDispatchThread) runWriteAction(action)
|
if (inWriteAction) runWriteAction(action)
|
||||||
else run(action)
|
else run(action)
|
||||||
|
|
||||||
private fun getTarget(editor: Editor, file: PsiFile): TElement? {
|
private fun getTarget(editor: Editor, file: PsiFile): TElement? {
|
||||||
|
|||||||
+4
-2
@@ -41,10 +41,12 @@ class IfThenToElvisInspection : IntentionBasedInspection<KtIfExpression>(
|
|||||||
{ it -> it.isUsedAsExpression(it.analyze(BodyResolveMode.PARTIAL)) }
|
{ it -> it.isUsedAsExpression(it.analyze(BodyResolveMode.PARTIAL)) }
|
||||||
)
|
)
|
||||||
|
|
||||||
class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpression>(
|
class IfThenToElvisIntention(private val fromJ2K: Boolean) : SelfTargetingOffsetIndependentIntention<KtIfExpression>(
|
||||||
KtIfExpression::class.java,
|
KtIfExpression::class.java,
|
||||||
"Replace 'if' expression with elvis expression"
|
"Replace 'if' expression with elvis expression"
|
||||||
) {
|
) {
|
||||||
|
@Suppress("unused")
|
||||||
|
constructor(): this(fromJ2K = false)
|
||||||
|
|
||||||
private fun KtExpression.clausesReplaceableByElvis(firstClause: KtExpression, secondClause: KtExpression, context: BindingContext) =
|
private fun KtExpression.clausesReplaceableByElvis(firstClause: KtExpression, secondClause: KtExpression, context: BindingContext) =
|
||||||
!firstClause.isNullOrBlockExpression() &&
|
!firstClause.isNullOrBlockExpression() &&
|
||||||
@@ -145,7 +147,7 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
it.typeReference!!)
|
it.typeReference!!)
|
||||||
}
|
}
|
||||||
val checkedExpression = condition.checkedExpression()!!
|
val checkedExpression = condition.checkedExpression()!!
|
||||||
val elvis = tryRunWriteAction {
|
val elvis = runInWriteActionOrHere(inWriteAction = !fromJ2K) {
|
||||||
val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
|
val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
|
||||||
if (condition is KtIsExpression) newReceiver!! else left
|
if (condition is KtIsExpression) newReceiver!! else left
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-3
@@ -21,13 +21,16 @@ import org.jetbrains.kotlin.idea.core.replaced
|
|||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingOffsetIndependentIntention
|
import org.jetbrains.kotlin.idea.intentions.SelfTargetingOffsetIndependentIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
|
||||||
class IfThenToSafeAccessInspection : IntentionBasedInspection<KtIfExpression>(IfThenToSafeAccessIntention::class)
|
class IfThenToSafeAccessInspection : IntentionBasedInspection<KtIfExpression>(IfThenToSafeAccessIntention::class)
|
||||||
|
|
||||||
class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIfExpression>(KtIfExpression::class.java, "Replace 'if' expression with safe access expression") {
|
class IfThenToSafeAccessIntention(private val fromJ2K: Boolean) : SelfTargetingOffsetIndependentIntention<KtIfExpression>(
|
||||||
|
KtIfExpression::class.java, "Replace 'if' expression with safe access expression"
|
||||||
|
) {
|
||||||
|
@Suppress("unused")
|
||||||
|
constructor(): this(fromJ2K = false)
|
||||||
|
|
||||||
override fun isApplicableTo(element: KtIfExpression): Boolean {
|
override fun isApplicableTo(element: KtIfExpression): Boolean {
|
||||||
val condition = element.condition as? KtBinaryExpression ?: return false
|
val condition = element.condition as? KtBinaryExpression ?: return false
|
||||||
@@ -67,7 +70,7 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
|
|||||||
}
|
}
|
||||||
|
|
||||||
val newExpr = KtPsiFactory(element).createExpressionByPattern("$0?.$1", receiverExpression, selectorExpression) as KtSafeQualifiedExpression
|
val newExpr = KtPsiFactory(element).createExpressionByPattern("$0?.$1", receiverExpression, selectorExpression) as KtSafeQualifiedExpression
|
||||||
val safeAccessExpr = tryRunWriteAction {
|
val safeAccessExpr = runInWriteActionOrHere(inWriteAction = !fromJ2K) {
|
||||||
element.replaced(newExpr)
|
element.replaced(newExpr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ object J2KPostProcessingRegistrar {
|
|||||||
_processings.add(RemoveRedundantCastToNullableProcessing())
|
_processings.add(RemoveRedundantCastToNullableProcessing())
|
||||||
|
|
||||||
registerIntentionBasedProcessing(ConvertToExpressionBodyIntention(convertEmptyToUnit = false)) { it is KtPropertyAccessor }
|
registerIntentionBasedProcessing(ConvertToExpressionBodyIntention(convertEmptyToUnit = false)) { it is KtPropertyAccessor }
|
||||||
registerIntentionBasedProcessing(IfThenToSafeAccessIntention())
|
registerIntentionBasedProcessing(IfThenToSafeAccessIntention(fromJ2K = true))
|
||||||
registerIntentionBasedProcessing(IfThenToElvisIntention())
|
registerIntentionBasedProcessing(IfThenToElvisIntention(fromJ2K = true))
|
||||||
registerIntentionBasedProcessing(FoldInitializerAndIfToElvisIntention())
|
registerIntentionBasedProcessing(FoldInitializerAndIfToElvisIntention())
|
||||||
registerIntentionBasedProcessing(SimplifyNegatedBinaryExpressionIntention())
|
registerIntentionBasedProcessing(SimplifyNegatedBinaryExpressionIntention())
|
||||||
registerIntentionBasedProcessing(ReplaceGetOrSetIntention(), additionalChecker = ReplaceGetOrSetInspection.additionalChecker)
|
registerIntentionBasedProcessing(ReplaceGetOrSetIntention(), additionalChecker = ReplaceGetOrSetInspection.additionalChecker)
|
||||||
|
|||||||
Reference in New Issue
Block a user