Refactored IfThenToDoubleBangIntention
This commit is contained in:
@@ -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.if.family=Replace Method Call with 'if' Expression
|
||||||
unfold.call.to.when=Replace method call with 'when' expression
|
unfold.call.to.when=Replace method call with 'when' expression
|
||||||
unfold.call.to.when.family=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=Replace safe access expression with 'if' expression
|
||||||
safe.access.to.if.then.family=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
|
merge.when=Merge 'when' expressions
|
||||||
|
|||||||
+25
-38
@@ -21,63 +21,50 @@ import org.jetbrains.kotlin.idea.JetBundle
|
|||||||
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
|
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
||||||
import org.jetbrains.kotlin.lexer.JetTokens
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.JetBinaryExpression
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.JetIfExpression
|
|
||||||
import org.jetbrains.kotlin.psi.JetPostfixExpression
|
|
||||||
import org.jetbrains.kotlin.psi.JetThrowExpression
|
|
||||||
|
|
||||||
public class IfThenToDoubleBangIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>("if.then.to.double.bang", javaClass()) {
|
|
||||||
|
|
||||||
|
public class IfThenToDoubleBangIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with '!!' expression") {
|
||||||
override fun isApplicableTo(element: JetIfExpression): Boolean {
|
override fun isApplicableTo(element: JetIfExpression): Boolean {
|
||||||
val condition = element.getCondition() as? JetBinaryExpression ?: return false
|
val condition = element.getCondition() as? JetBinaryExpression ?: return false
|
||||||
val thenClause = element.getThen()
|
val thenClause = element.getThen() ?: return false
|
||||||
val elseClause = element.getElse()
|
val elseClause = element.getElse()
|
||||||
|
|
||||||
val expression = condition.expressionComparedToNull() ?: return false
|
val expression = condition.expressionComparedToNull() ?: return false
|
||||||
|
|
||||||
val token = condition.getOperationToken()
|
val token = condition.getOperationToken()
|
||||||
|
|
||||||
val throwExpression =
|
val throwExpression: JetThrowExpression
|
||||||
when (token) {
|
val matchingClause: JetExpression?
|
||||||
JetTokens.EQEQ -> thenClause?.unwrapBlock()
|
when (token) {
|
||||||
JetTokens.EXCLEQ -> elseClause?.unwrapBlock()
|
JetTokens.EQEQ -> {
|
||||||
else -> throw IllegalStateException("Token must be either '!=' or '==' ")
|
throwExpression = thenClause.unwrapBlock() as? JetThrowExpression ?: return false
|
||||||
} as? JetThrowExpression ?: return false
|
matchingClause = elseClause
|
||||||
|
}
|
||||||
|
|
||||||
val matchingClause =
|
JetTokens.EXCLEQ -> {
|
||||||
when (token) {
|
matchingClause = thenClause
|
||||||
JetTokens.EQEQ -> elseClause
|
throwExpression = elseClause?.unwrapBlock() as? JetThrowExpression ?: return false
|
||||||
JetTokens.EXCLEQ -> thenClause
|
}
|
||||||
else -> throw IllegalStateException("Token must be either '!=' or '==' ")
|
|
||||||
}
|
else -> throw IllegalStateException()
|
||||||
|
}
|
||||||
|
|
||||||
val matchesAsStatement = element.isStatement() && (matchingClause?.isNullExpressionOrEmptyBlock() ?: true)
|
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) {
|
var text = "Replace 'if' expression with '!!' expression"
|
||||||
val message =
|
if (!throwExpression.throwsNullPointerExceptionWithNoArguments()) {
|
||||||
if (throwExpression.throwsNullPointerExceptionWithNoArguments()) {
|
text += " (will remove exception)"
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
setText(message)
|
setText(text)
|
||||||
return true
|
return true
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetIfExpression, editor: Editor) {
|
override fun applyTo(element: JetIfExpression, editor: Editor) {
|
||||||
val condition = element.getCondition() as JetBinaryExpression
|
val condition = element.getCondition() as JetBinaryExpression
|
||||||
|
|
||||||
val expression = condition.expressionComparedToNull()!!
|
val expression = condition.expressionComparedToNull()!!
|
||||||
val resultingExprString = expression.getText() + "!!"
|
val result = element.replace(JetPsiFactory(element).createExpressionByPattern("$0!!", expression)) as JetPostfixExpression
|
||||||
val result = element.replace(resultingExprString) as JetPostfixExpression
|
|
||||||
|
|
||||||
result.inlineBaseExpressionIfApplicableWithPrompt(editor)
|
result.inlineBaseExpressionIfApplicableWithPrompt(editor)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user