Refactored ReplaceWithTraditionalAssignmentIntention

This commit is contained in:
Valentin Kipyatkov
2015-04-29 19:32:05 +03:00
parent 475a361b32
commit 8fc799322b
3 changed files with 15 additions and 31 deletions
@@ -290,8 +290,6 @@ add.name.to.argument.action=Add name to argument...
add.name.to.parameter.name.chooser.title=Choose parameter name add.name.to.parameter.name.chooser.title=Choose parameter name
split.if=Split into 2 if's split.if=Split into 2 if's
split.if.family=Split If split.if.family=Split If
replace.with.traditional.assignment.intention=Replace with traditional assignment
replace.with.traditional.assignment.intention.family=Replace with Traditional Assignment
simplify.boolean.with.constants=Simplify boolean expression simplify.boolean.with.constants=Simplify boolean expression
simplify.boolean.with.constants.family=Simplify Boolean Expression simplify.boolean.with.constants.family=Simplify Boolean Expression
remove.explicit.type.arguments=Remove explicit type arguments remove.explicit.type.arguments=Remove explicit type arguments
@@ -16,43 +16,29 @@
package org.jetbrains.kotlin.idea.intentions package org.jetbrains.kotlin.idea.intentions
import org.jetbrains.kotlin.psi.JetBinaryExpression
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetBinaryExpression
import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetPsiUnparsingUtils import org.jetbrains.kotlin.psi.JetSimpleNameExpression
public class ReplaceWithTraditionalAssignmentIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>("replace.with.traditional.assignment.intention", javaClass()) { public class ReplaceWithTraditionalAssignmentIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Replace with traditional assignment") {
override fun isApplicableTo(element: JetBinaryExpression): Boolean { override fun isApplicableTo(element: JetBinaryExpression): Boolean {
fun checkForNullSafety(element: JetBinaryExpression): Boolean = element.getLeft() != null && element.getRight() != null && element.getOperationToken() != null return element.getOperationToken() in JetTokens.AUGMENTED_ASSIGNMENTS
&& element.getLeft() is JetSimpleNameExpression
fun checkValidity(element: JetBinaryExpression): Boolean { && element.getRight() != null
return element.getLeft() is JetSimpleNameExpression &&
JetTokens.AUGMENTED_ASSIGNMENTS.contains(element.getOperationToken())
}
return checkForNullSafety(element) && checkValidity(element)
} }
override fun applyTo(element: JetBinaryExpression, editor: Editor) { override fun applyTo(element: JetBinaryExpression, editor: Editor) {
fun buildReplacement(element: JetBinaryExpression): String { val left = element.getLeft()!!
val replacementStringBuilder = StringBuilder("${element.getLeft()!!.getText()} = ${element.getLeft()!!.getText()} ") val right = element.getRight()!!
val factory = JetPsiFactory(element)
replacementStringBuilder.append( val assignOpText = element.getOperationReference().getText()
when { assert(assignOpText.endsWith("="))
element.getOperationToken() == JetTokens.PLUSEQ -> "+" val operationText = assignOpText.substring(0, assignOpText.length() - 1)
element.getOperationToken() == JetTokens.MINUSEQ -> "-"
element.getOperationToken() == JetTokens.MULTEQ -> "*"
element.getOperationToken() == JetTokens.DIVEQ -> "/"
element.getOperationToken() == JetTokens.PERC -> "%"
else -> ""
}
).append(" ${JetPsiUnparsingUtils.parenthesizeIfNeeded(element.getRight())}")
return replacementStringBuilder.toString() val expression = factory.createBinaryExpression(left, operationText, right)
} element.replace(factory.createBinaryExpression(left, "=", expression))
element.replace(JetPsiFactory(element).createExpression(buildReplacement(element)))
} }
} }
@@ -2,5 +2,5 @@ fun foo() {
var x = 0 var x = 0
val a = 1 val a = 1
val b = 1 val b = 1
x = x + (a / b) x = x + a / b
} }