From 2b77fe2ff1929b576d418bb2548cf40b01dbbedc Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Mon, 11 May 2015 17:28:39 +0300 Subject: [PATCH] Refactored IfThenToDoubleBangIntention --- .../kotlin/idea/JetBundle.properties | 3 - .../intentions/IfThenToDoubleBangIntention.kt | 63 ++++++++----------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties index 62062cf7931..177da58d84a 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties @@ -220,9 +220,6 @@ unfold.call.to.if=Replace method call with 'if' expression unfold.call.to.if.family=Replace Method Call with 'if' Expression unfold.call.to.when=Replace method call with 'when' expression unfold.call.to.when.family=Replace Method Call with 'when' Expression -if.then.to.double.bang=Replace 'if' expression with '!!' expression -if.then.to.double.bang.replace.exception=Replace 'if' expression with '!!' expression (will remove Exception) -if.then.to.double.bang.family=Replace 'if' Expression with '!!' Expression safe.access.to.if.then=Replace safe access expression with 'if' expression safe.access.to.if.then.family=Replace Safe Access Expression with 'if' Expression merge.when=Merge 'when' expressions diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt index ad001c164eb..e089232aced 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt @@ -21,63 +21,50 @@ import org.jetbrains.kotlin.idea.JetBundle import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.* import org.jetbrains.kotlin.lexer.JetTokens -import org.jetbrains.kotlin.psi.JetBinaryExpression -import org.jetbrains.kotlin.psi.JetIfExpression -import org.jetbrains.kotlin.psi.JetPostfixExpression -import org.jetbrains.kotlin.psi.JetThrowExpression - -public class IfThenToDoubleBangIntention : JetSelfTargetingOffsetIndependentIntention("if.then.to.double.bang", javaClass()) { +import org.jetbrains.kotlin.psi.* +public class IfThenToDoubleBangIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'if' expression with '!!' expression") { override fun isApplicableTo(element: JetIfExpression): Boolean { val condition = element.getCondition() as? JetBinaryExpression ?: return false - val thenClause = element.getThen() + val thenClause = element.getThen() ?: return false val elseClause = element.getElse() val expression = condition.expressionComparedToNull() ?: return false val token = condition.getOperationToken() - val throwExpression = - when (token) { - JetTokens.EQEQ -> thenClause?.unwrapBlock() - JetTokens.EXCLEQ -> elseClause?.unwrapBlock() - else -> throw IllegalStateException("Token must be either '!=' or '==' ") - } as? JetThrowExpression ?: return false + val throwExpression: JetThrowExpression + val matchingClause: JetExpression? + when (token) { + JetTokens.EQEQ -> { + throwExpression = thenClause.unwrapBlock() as? JetThrowExpression ?: return false + matchingClause = elseClause + } - val matchingClause = - when (token) { - JetTokens.EQEQ -> elseClause - JetTokens.EXCLEQ -> thenClause - else -> throw IllegalStateException("Token must be either '!=' or '==' ") - } + JetTokens.EXCLEQ -> { + matchingClause = thenClause + throwExpression = elseClause?.unwrapBlock() as? JetThrowExpression ?: return false + } + + else -> throw IllegalStateException() + } val matchesAsStatement = element.isStatement() && (matchingClause?.isNullExpressionOrEmptyBlock() ?: true) - val matches = matchesAsStatement || (matchingClause?.evaluatesTo(expression) ?: false && expression.isStableVariable()) + if (!matchesAsStatement && !(matchingClause?.evaluatesTo(expression) ?: false && expression.isStableVariable())) return false - if (matches) { - val message = - if (throwExpression.throwsNullPointerExceptionWithNoArguments()) { - JetBundle.message("if.then.to.double.bang") - } - else { - // Warn that custom exception will be overwritten by intention action - JetBundle.message("if.then.to.double.bang.replace.exception") - } + var text = "Replace 'if' expression with '!!' expression" + if (!throwExpression.throwsNullPointerExceptionWithNoArguments()) { + text += " (will remove exception)" + } - setText(message) - return true - } - else { - return false - } + setText(text) + return true } override fun applyTo(element: JetIfExpression, editor: Editor) { val condition = element.getCondition() as JetBinaryExpression - val expression = condition.expressionComparedToNull()!! - val resultingExprString = expression.getText() + "!!" - val result = element.replace(resultingExprString) as JetPostfixExpression + val result = element.replace(JetPsiFactory(element).createExpressionByPattern("$0!!", expression)) as JetPostfixExpression result.inlineBaseExpressionIfApplicableWithPrompt(editor) }