Remove ConvertNegatedBooleanSequenceIntention

Relates to #KT-31502
This commit is contained in:
Dmitry Gridin
2019-05-27 17:27:21 +07:00
parent dd03a81309
commit 91e9ed25f7
24 changed files with 0 additions and 231 deletions
@@ -1093,11 +1093,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertNegatedBooleanSequenceIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertBinaryExpressionWithDemorgansLawIntention</className>
<category>Kotlin</category>
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean) {
return <spot> !(a || b) </spot>
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean) {
return <spot> !a && !b</spot>
}
@@ -1,5 +0,0 @@
<html>
<body>
This intention converts a sequence of negated boolean expressions with the De Morgan equivalent.
</body>
</html>
@@ -1,81 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import java.util.*
class ConvertNegatedBooleanSequenceIntention : SelfTargetingOffsetIndependentIntention<KtBinaryExpression>(
KtBinaryExpression::class.java, "Replace negated sequence with DeMorgan equivalent"
) {
override fun isApplicableTo(element: KtBinaryExpression): Boolean {
if (element.parent is KtBinaryExpression) return false // operate only on the longest sequence
val opToken = element.operationToken
if (opToken != KtTokens.ANDAND && opToken != KtTokens.OROR) return false
return splitBooleanSequence(element) != null
}
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
val operatorText = when(element.operationToken) {
KtTokens.ANDAND -> KtTokens.OROR.value
KtTokens.OROR -> KtTokens.ANDAND.value
else -> throw IllegalArgumentException() // checked in isApplicableTo
}
val elements = splitBooleanSequence(element)!!
val bareExpressions = elements.map { prefixExpression -> prefixExpression.baseExpression!!.text }
val negatedExpression = bareExpressions.subList(0, bareExpressions.lastIndex).foldRight(
"!(${bareExpressions.last()}", { negated, expression -> "$expression $operatorText $negated" }
)
val newExpression = KtPsiFactory(element).createExpression("$negatedExpression)")
val insertedElement = element.replace(newExpression)
val insertedElementParent = insertedElement.parent as? KtParenthesizedExpression ?: return
if (KtPsiUtil.areParenthesesUseless(insertedElementParent)) {
insertedElementParent.replace(insertedElement)
}
}
private fun splitBooleanSequence(expression: KtBinaryExpression): List<KtPrefixExpression>? {
val itemList = LinkedList<KtPrefixExpression>()
val firstOperator = expression.operationToken
var currentItem: KtBinaryExpression? = expression
while (currentItem != null) {
if (currentItem.operationToken != firstOperator) return null //Boolean sequence must be homogeneous
val rightChild = currentItem.right as? KtPrefixExpression ?: return null
itemList.add(rightChild)
val leftChild = currentItem.left
when (leftChild) {
is KtPrefixExpression -> itemList.add(leftChild)
!is KtBinaryExpression -> return null
}
currentItem = leftChild as? KtBinaryExpression
}
return itemList
}
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ConvertNegatedBooleanSequenceIntention
@@ -1,3 +0,0 @@
fun foo(a: Int, b: Int, c: Int, d: Int) : Boolean {
return !(a == b) && !(b <caret>== c) && !(a < d)
}
@@ -1,3 +0,0 @@
fun foo(a: Int, b: Int, c: Int, d: Int) : Boolean {
return !((a == b) || (b == c) || (a < d))
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean, c: Boolean): Boolean {
return <caret>!a && !b && !c
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean, c: Boolean): Boolean {
return !(a || b || c)
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean): Boolean {
return <caret>!a && !b
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean): Boolean {
return !(a || b)
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean): Boolean {
return <caret>!a || !b
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean): Boolean {
return !(a && b)
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean, c: Boolean): Boolean {
return <caret>!(a && b) || !(a || c)
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean, c: Boolean): Boolean {
return !((a && b) && (a || c))
}
@@ -1,4 +0,0 @@
// IS_APPLICABLE: false
fun foo(a: Boolean, b: Boolean, c: Boolean): Boolean {
return <caret>!a && !b || !c
}
@@ -1,4 +0,0 @@
// IS_APPLICABLE: false
fun foo(a: Boolean, b: Boolean): Boolean {
return <caret> !a && b
}
@@ -1,4 +0,0 @@
// IS_APPLICABLE: false
fun foo(a: Boolean): Boolean {
return <caret>!a
}
@@ -1,11 +0,0 @@
fun bar1(): Boolean {
return true
}
fun bar2(): Boolean {
return false
}
fun foo(): Boolean {
return <caret>!bar1() && !bar2()
}
@@ -1,11 +0,0 @@
fun bar1(): Boolean {
return true
}
fun bar2(): Boolean {
return false
}
fun foo(): Boolean {
return !(bar1() || bar2())
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean): Boolean {
return (<caret>!a && !b)
}
@@ -1,3 +0,0 @@
fun foo(a: Boolean, b: Boolean): Boolean {
return !(a || b)
}
@@ -5830,69 +5830,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertNegatedBooleanSequence")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertNegatedBooleanSequence extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInConvertNegatedBooleanSequence() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertNegatedBooleanSequence"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("complexNegatedSequence.kt")
public void testComplexNegatedSequence() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/complexNegatedSequence.kt");
}
@TestMetadata("conjunctionOfThreeNegations.kt")
public void testConjunctionOfThreeNegations() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/conjunctionOfThreeNegations.kt");
}
@TestMetadata("conjunctionOfTwoNegations.kt")
public void testConjunctionOfTwoNegations() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/conjunctionOfTwoNegations.kt");
}
@TestMetadata("disjunctionOfTwoNegations.kt")
public void testDisjunctionOfTwoNegations() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/disjunctionOfTwoNegations.kt");
}
@TestMetadata("doubleParenthesizedExpression.kt")
public void testDoubleParenthesizedExpression() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/doubleParenthesizedExpression.kt");
}
@TestMetadata("inapplicableMixedOperators.kt")
public void testInapplicableMixedOperators() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/inapplicableMixedOperators.kt");
}
@TestMetadata("inapplicableMixedSequence.kt")
public void testInapplicableMixedSequence() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/inapplicableMixedSequence.kt");
}
@TestMetadata("inapplicableSingleExpression.kt")
public void testInapplicableSingleExpression() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/inapplicableSingleExpression.kt");
}
@TestMetadata("negatedFunction.kt")
public void testNegatedFunction() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/negatedFunction.kt");
}
@TestMetadata("parenthesizedConjunctionOfTwoNegations.kt")
public void testParenthesizedConjunctionOfTwoNegations() throws Exception {
runTest("idea/testData/intentions/convertNegatedBooleanSequence/parenthesizedConjunctionOfTwoNegations.kt");
}
}
@TestMetadata("idea/testData/intentions/convertNullablePropertyToLateinit")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)