New Intention Action: Replace with traditional assign.

This takes an expression of the form a += b and converts it to an
expression form a = a + b.
This commit is contained in:
Pradyoth Kukkapalli
2014-04-05 01:13:49 -04:00
parent 249af96d3c
commit eb3ddf50bf
15 changed files with 145 additions and 1 deletions
@@ -387,6 +387,7 @@ fun main(args: Array<String>) {
model("intentions/swapBinaryExpression", testMethod = "doTestSwapBinaryExpression")
model("intentions/splitIf", testMethod = "doTestSplitIf")
model("intentions/replaceWithOperatorAssign", testMethod = "doTestReplaceWithOperatorAssign")
model("intentions/replaceWithTraditionalAssignment", testMethod = "doTestReplaceWithTraditionalAssignment")
}
testClass(javaClass<AbstractJetInspectionTest>()) {
@@ -0,0 +1,4 @@
fun foo() {
var x = 0
x = x + y
}
@@ -0,0 +1,4 @@
fun foo() {
var x = 0
<spot>x += y</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention modifies an expression with an augment assignment to an expression that separates the assignment and operator.
</body>
</html>
+5
View File
@@ -650,6 +650,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.ReplaceWithTraditionalAssignmentIntention</className>
<category>Kotlin</category>
</intentionAction>
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
</extensions>
@@ -289,6 +289,8 @@ split.if=Split into 2 if's
split.if.family=Split If
replace.with.operator.assign.intention=Replace with an Operator-Assign Expression
replace.with.operator.assign.intention.family=Replace with an Operator-Assign Expression
replace.with.traditional.assignment.intention=Replace with Traditional Assignment
replace.with.traditional.assignment.intention.family=Replace with Traditional Assignment
property.is.implemented.too.many=Has implementations
property.is.overridden.too.many=Is overridden in subclasses
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.intentions
import org.jetbrains.jet.lang.psi.JetBinaryExpression
import com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
import org.jetbrains.jet.lexer.JetTokens
import org.jetbrains.jet.lang.psi.JetPsiFactory
import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils
public class ReplaceWithTraditionalAssignmentIntention : JetSelfTargetingIntention<JetBinaryExpression>("replace.with.traditional.assignment.intention", javaClass()) {
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)
}
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
fun buildReplacement(element: JetBinaryExpression): String {
val replacementStringBuilder = StringBuilder("${element.getLeft()!!.getText()} = ${element.getLeft()!!.getText()} ")
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())}")
return replacementStringBuilder.toString()
}
element.replace(JetPsiFactory.createExpression(element.getProject(), buildReplacement(element)))
}
}
@@ -0,0 +1,6 @@
fun foo() {
var x = 0
val a = 1
val b = 1
<caret>x += a / b
}
@@ -0,0 +1,6 @@
fun foo() {
var x = 0
val a = 1
val b = 1
x = x + (a / b)
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
fun foo() {
var x = 0
<caret>x + 1
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
fun foo() {
var x = 0
val a = 1
val b = 1
<caret>x = a / b
}
@@ -0,0 +1,4 @@
fun foo() {
var x = 0
<caret>x += 1
}
@@ -0,0 +1,4 @@
fun foo() {
var x = 0
<caret>x = x + 1
}
@@ -223,6 +223,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
doTestIntention(path, new ReplaceWithOperatorAssignIntention());
}
public void doTestReplaceWithTraditionalAssignment(@NotNull String path) throws Exception {
doTestIntention(path, new ReplaceWithTraditionalAssignmentIntention());
}
private void doTestIntention(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
configureByFile(path);
@@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@InnerTestClasses({CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class, CodeTransformationTestGenerated.SplitIf.class, CodeTransformationTestGenerated.ReplaceWithOperatorAssign.class})
@InnerTestClasses({CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class, CodeTransformationTestGenerated.SplitIf.class, CodeTransformationTestGenerated.ReplaceWithOperatorAssign.class, CodeTransformationTestGenerated.ReplaceWithTraditionalAssignment.class})
public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest {
@TestMetadata("idea/testData/intentions/branched/elvisToIfThen")
public static class ElvisToIfThen extends AbstractCodeTransformationTest {
@@ -3145,6 +3145,34 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
}
@TestMetadata("idea/testData/intentions/replaceWithTraditionalAssignment")
public static class ReplaceWithTraditionalAssignment extends AbstractCodeTransformationTest {
public void testAllFilesPresentInReplaceWithTraditionalAssignment() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/replaceWithTraditionalAssignment"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("complexRightExpression.kt")
public void testComplexRightExpression() throws Exception {
doTestReplaceWithTraditionalAssignment("idea/testData/intentions/replaceWithTraditionalAssignment/complexRightExpression.kt");
}
@TestMetadata("nonAssignmentExpression.kt")
public void testNonAssignmentExpression() throws Exception {
doTestReplaceWithTraditionalAssignment("idea/testData/intentions/replaceWithTraditionalAssignment/nonAssignmentExpression.kt");
}
@TestMetadata("nonAugmentedAssign.kt")
public void testNonAugmentedAssign() throws Exception {
doTestReplaceWithTraditionalAssignment("idea/testData/intentions/replaceWithTraditionalAssignment/nonAugmentedAssign.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
doTestReplaceWithTraditionalAssignment("idea/testData/intentions/replaceWithTraditionalAssignment/simple.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("CodeTransformationTestGenerated");
suite.addTestSuite(ElvisToIfThen.class);
@@ -3193,6 +3221,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
suite.addTestSuite(SwapBinaryExpression.class);
suite.addTestSuite(SplitIf.class);
suite.addTestSuite(ReplaceWithOperatorAssign.class);
suite.addTestSuite(ReplaceWithTraditionalAssignment.class);
return suite;
}
}