Refactored ReplaceWithTraditionalAssignmentIntention
This commit is contained in:
@@ -290,8 +290,6 @@ add.name.to.argument.action=Add name to argument...
|
||||
add.name.to.parameter.name.chooser.title=Choose parameter name
|
||||
split.if=Split into 2 if's
|
||||
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.family=Simplify Boolean Expression
|
||||
remove.explicit.type.arguments=Remove explicit type arguments
|
||||
|
||||
+14
-28
@@ -16,43 +16,29 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import org.jetbrains.kotlin.psi.JetBinaryExpression
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.JetBinaryExpression
|
||||
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 {
|
||||
fun checkForNullSafety(element: JetBinaryExpression): Boolean = element.getLeft() != null && element.getRight() != null && element.getOperationToken() != null
|
||||
|
||||
fun checkValidity(element: JetBinaryExpression): Boolean {
|
||||
return element.getLeft() is JetSimpleNameExpression &&
|
||||
JetTokens.AUGMENTED_ASSIGNMENTS.contains(element.getOperationToken())
|
||||
}
|
||||
|
||||
return checkForNullSafety(element) && checkValidity(element)
|
||||
return element.getOperationToken() in JetTokens.AUGMENTED_ASSIGNMENTS
|
||||
&& element.getLeft() is JetSimpleNameExpression
|
||||
&& element.getRight() != null
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
|
||||
fun buildReplacement(element: JetBinaryExpression): String {
|
||||
val replacementStringBuilder = StringBuilder("${element.getLeft()!!.getText()} = ${element.getLeft()!!.getText()} ")
|
||||
val left = element.getLeft()!!
|
||||
val right = element.getRight()!!
|
||||
val factory = JetPsiFactory(element)
|
||||
|
||||
replacementStringBuilder.append(
|
||||
when {
|
||||
element.getOperationToken() == JetTokens.PLUSEQ -> "+"
|
||||
element.getOperationToken() == JetTokens.MINUSEQ -> "-"
|
||||
element.getOperationToken() == JetTokens.MULTEQ -> "*"
|
||||
element.getOperationToken() == JetTokens.DIVEQ -> "/"
|
||||
element.getOperationToken() == JetTokens.PERC -> "%"
|
||||
else -> ""
|
||||
}
|
||||
).append(" ${JetPsiUnparsingUtils.parenthesizeIfNeeded(element.getRight())}")
|
||||
val assignOpText = element.getOperationReference().getText()
|
||||
assert(assignOpText.endsWith("="))
|
||||
val operationText = assignOpText.substring(0, assignOpText.length() - 1)
|
||||
|
||||
return replacementStringBuilder.toString()
|
||||
}
|
||||
|
||||
element.replace(JetPsiFactory(element).createExpression(buildReplacement(element)))
|
||||
val expression = factory.createBinaryExpression(left, operationText, right)
|
||||
element.replace(factory.createBinaryExpression(left, "=", expression))
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,5 +2,5 @@ fun foo() {
|
||||
var x = 0
|
||||
val a = 1
|
||||
val b = 1
|
||||
x = x + (a / b)
|
||||
x = x + a / b
|
||||
}
|
||||
Reference in New Issue
Block a user