KT-4568: Created the ConvertNegatedBooleanSequence intention. This intention takes an expression of the form !a &&,|| !b &&,|| ... and converts it to the DeMorgan equivalent !(a &&,|| b ...).
This commit is contained in:
committed by
Mikhael Bogdanov
parent
4088f4a00e
commit
2147a88ed8
@@ -379,6 +379,7 @@ fun main(args: Array<String>) {
|
||||
model("intentions/attributeCallReplacements/replaceUnaryPrefixIntention", testMethod = "doTestReplaceUnaryPrefixIntention")
|
||||
model("intentions/attributeCallReplacements/replaceInvokeIntention", testMethod = "doTestReplaceInvokeIntention")
|
||||
model("intentions/simplifyNegatedBinaryExpressionIntention", testMethod = "doTestSimplifyNegatedBinaryExpressionIntention")
|
||||
model("intentions/convertNegatedBooleanSequence", testMethod="doTestConvertNegatedBooleanSequence")
|
||||
}
|
||||
|
||||
testClass(javaClass<AbstractHierarchyTest>()) {
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return <spot> !(a || b) </spot>
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return <spot> !a && !b</spot>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts a sequence of negated boolean expressions with the DeMorgan equivalent.
|
||||
</body>
|
||||
</html>
|
||||
@@ -611,9 +611,13 @@
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.AddBracesIntention</className>
|
||||
<category>Kotlin</category>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.ConvertNegatedBooleanSequenceIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
|
||||
|
||||
|
||||
@@ -279,6 +279,8 @@ remove.braces=Remove braces
|
||||
remove.braces.family=Remove Braces
|
||||
add.braces=Add braces
|
||||
add.braces.family=Add Braces
|
||||
convert.negated.boolean.sequence=Replace negated sequence with DeMorgan equivalent
|
||||
convert.negated.boolean.sequence.family=Replace negated sequence with DeMorgan equivalent
|
||||
|
||||
property.is.implemented.too.many=Has implementations
|
||||
property.is.overridden.too.many=Is overridden in subclasses
|
||||
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.JetPrefixExpression
|
||||
import org.jetbrains.jet.lexer.JetTokens
|
||||
import com.intellij.psi.impl.source.tree.PsiErrorElementImpl
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil
|
||||
|
||||
|
||||
public class ConvertNegatedBooleanSequenceIntention : JetSelfTargetingIntention<JetBinaryExpression>(
|
||||
"convert.negated.boolean.sequence", javaClass()) {
|
||||
|
||||
override fun isApplicableTo(element: JetBinaryExpression): Boolean {
|
||||
if (element.getParent() is JetBinaryExpression) return false // operate only on the longest sequence
|
||||
var binaryExpression : JetBinaryExpression? = element
|
||||
val originalOperator = element.getOperationToken()
|
||||
|
||||
if (!(originalOperator == JetTokens.ANDAND || originalOperator == JetTokens.OROR)) {
|
||||
return false
|
||||
}
|
||||
|
||||
do {
|
||||
val leftChild = binaryExpression?.getLeft()
|
||||
val rightChild = binaryExpression?.getRight()
|
||||
val operator = binaryExpression?.getOperationToken()
|
||||
when {
|
||||
rightChild !is JetPrefixExpression,
|
||||
operator != originalOperator,
|
||||
!(leftChild is JetPrefixExpression || leftChild is JetBinaryExpression) -> return false
|
||||
else -> binaryExpression = leftChild as? JetBinaryExpression
|
||||
}
|
||||
} while (binaryExpression != null)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
|
||||
var binaryExpression = element : JetBinaryExpression?
|
||||
var expressionText = ""
|
||||
val operator = binaryExpression!!.getOperationToken()
|
||||
val operatorText = when(binaryExpression!!.getOperationToken()) {
|
||||
JetTokens.ANDAND -> JetTokens.OROR.getValue()
|
||||
JetTokens.OROR -> JetTokens.ANDAND.getValue()
|
||||
else -> throw IllegalArgumentException("Invalid operator: '$operator'. Only expressions using '&&' or '||' can be converted.")
|
||||
}
|
||||
|
||||
while (binaryExpression != null) {
|
||||
val leftChild = binaryExpression!!.getLeft()
|
||||
val rightChild = binaryExpression!!.getRight() as JetPrefixExpression
|
||||
expressionText = " $operatorText ${rightChild.getBaseExpression()!!.getText()}$expressionText"
|
||||
if (leftChild is JetPrefixExpression) {
|
||||
val leftChildText = (leftChild as JetPrefixExpression).getBaseExpression()!!.getText()
|
||||
expressionText = "$leftChildText$expressionText"
|
||||
}
|
||||
binaryExpression = leftChild as? JetBinaryExpression
|
||||
}
|
||||
|
||||
val newExpression = JetPsiFactory.createExpression(element.getProject(),"!($expressionText)")
|
||||
|
||||
val insertedElement = element.replace(newExpression)
|
||||
val insertedElementParent = insertedElement.getParent() as? JetParenthesizedExpression ?: return
|
||||
|
||||
if (JetPsiUtil.areParenthesesUseless(insertedElementParent)) {
|
||||
insertedElementParent.replace(insertedElement)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return <caret>!a && !b && !c
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return !(a || b || c)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return <caret>!a && !b
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return !(a || b)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return <caret>!a || !b
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return !(a && b)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean, c: Boolean) {
|
||||
return <caret>!(a && b) || !(a || c)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean, c: Boolean) {
|
||||
return !((a && b) && (a || c))
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(a: Boolean, b: Boolean, c: Boolean) {
|
||||
return <caret>!a && !b || !c
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(a: Boolean, b: boolean) {
|
||||
return <caret> !a && b
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(a: Boolean) {
|
||||
return !a
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun bar1() {
|
||||
return true
|
||||
}
|
||||
|
||||
fun bar2() {
|
||||
return false
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
return <caret>!bar1() && !bar2()
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun bar1() {
|
||||
return true
|
||||
}
|
||||
|
||||
fun bar2() {
|
||||
return false
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
return !(bar1() || bar2())
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return (<caret>!a && !b)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(a: Boolean, b: Boolean) {
|
||||
return !(a || b)
|
||||
}
|
||||
@@ -175,6 +175,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
|
||||
doTestIntention(path, new AddBracesIntention());
|
||||
}
|
||||
|
||||
public void doTestConvertNegatedBooleanSequence(@NotNull String path) throws Exception {
|
||||
doTestIntention(path, new ConvertNegatedBooleanSequenceIntention());
|
||||
}
|
||||
|
||||
public void doTestReplaceGetIntention(@NotNull String path) throws Exception {
|
||||
doTestIntention(path, new TestableReplaceGetIntention());
|
||||
}
|
||||
|
||||
+56
-2
@@ -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})
|
||||
@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})
|
||||
public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest {
|
||||
@TestMetadata("idea/testData/intentions/branched/elvisToIfThen")
|
||||
public static class ElvisToIfThen extends AbstractCodeTransformationTest {
|
||||
@@ -2602,7 +2602,60 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/simplifyNegatedBinaryExpressionIntention")
|
||||
@TestMetadata("idea/testData/intentions/convertNegatedBooleanSequence")
|
||||
public static class ConvertNegatedBooleanSequence extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInConvertNegatedBooleanSequence() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/convertNegatedBooleanSequence"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("conjunctionOfThreeNegations.kt")
|
||||
public void testConjunctionOfThreeNegations() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/conjunctionOfThreeNegations.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("conjunctionOfTwoNegations.kt")
|
||||
public void testConjunctionOfTwoNegations() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/conjunctionOfTwoNegations.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("disjunctionOfTwoNegations.kt")
|
||||
public void testDisjunctionOfTwoNegations() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/disjunctionOfTwoNegations.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("doubleParenthesizedExpression.kt")
|
||||
public void testDoubleParenthesizedExpression() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/doubleParenthesizedExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inapplicableMixedOperators.kt")
|
||||
public void testInapplicableMixedOperators() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/inapplicableMixedOperators.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inapplicableMixedSequence.kt")
|
||||
public void testInapplicableMixedSequence() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/inapplicableMixedSequence.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inapplicableSingleExpression.kt")
|
||||
public void testInapplicableSingleExpression() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/inapplicableSingleExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("negatedFunction.kt")
|
||||
public void testNegatedFunction() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/negatedFunction.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("parenthesizedConjunctionOfTwoNegations.kt")
|
||||
public void testParenthesizedConjunctionOfTwoNegations() throws Exception {
|
||||
doTestConvertNegatedBooleanSequence("idea/testData/intentions/convertNegatedBooleanSequence/parenthesizedConjunctionOfTwoNegations.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/simplifyNegatedBinaryExpressionIntention")
|
||||
public static class SimplifyNegatedBinaryExpressionIntention extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInSimplifyNegatedBinaryExpressionIntention() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/simplifyNegatedBinaryExpressionIntention"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
@@ -2713,6 +2766,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
suite.addTestSuite(ReplaceUnaryPrefixIntention.class);
|
||||
suite.addTestSuite(ReplaceInvokeIntention.class);
|
||||
suite.addTestSuite(SimplifyNegatedBinaryExpressionIntention.class);
|
||||
suite.addTestSuite(ConvertNegatedBooleanSequence.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user