Minor code improvements + more correct code
This commit is contained in:
+11
-13
@@ -21,7 +21,7 @@ import com.intellij.psi.impl.source.tree.PsiErrorElementImpl
|
|||||||
import org.jetbrains.kotlin.lexer.JetTokens
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||||
import java.util.LinkedList
|
import java.util.*
|
||||||
|
|
||||||
public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetingOffsetIndependentIntention<JetPrefixExpression>(javaClass(), "DeMorgan Law") {
|
public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetingOffsetIndependentIntention<JetPrefixExpression>(javaClass(), "DeMorgan Law") {
|
||||||
|
|
||||||
@@ -38,8 +38,7 @@ public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetin
|
|||||||
else -> return false
|
else -> return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val elements = splitBooleanSequence(baseExpression) ?: return false
|
return splitBooleanSequence(baseExpression) != null
|
||||||
return elements.none { it is PsiErrorElementImpl }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetPrefixExpression, editor: Editor) {
|
override fun applyTo(element: JetPrefixExpression, editor: Editor) {
|
||||||
@@ -67,24 +66,23 @@ public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetin
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun splitBooleanSequence(expression: JetBinaryExpression): List<JetExpression>? {
|
private fun splitBooleanSequence(expression: JetBinaryExpression): List<JetExpression>? {
|
||||||
val result = LinkedList<JetExpression>()
|
val result = ArrayList<JetExpression>()
|
||||||
val firstOperator = expression.getOperationToken()
|
val firstOperator = expression.getOperationToken()
|
||||||
|
|
||||||
var currentItem: JetExpression? = expression
|
var remainingExpression: JetExpression = expression
|
||||||
while (currentItem as? JetBinaryExpression != null) {
|
while (true) {
|
||||||
val remainingExpression = currentItem as JetBinaryExpression
|
if (remainingExpression !is JetBinaryExpression) break
|
||||||
|
|
||||||
val operation = remainingExpression.getOperationToken()
|
val operation = remainingExpression.getOperationToken()
|
||||||
if (!(operation == JetTokens.ANDAND || operation == JetTokens.OROR)) break
|
if (operation != JetTokens.ANDAND && operation != JetTokens.OROR) break
|
||||||
|
|
||||||
if (operation != firstOperator) return null //Boolean sequence must be homogenous
|
if (operation != firstOperator) return null //Boolean sequence must be homogenous
|
||||||
|
|
||||||
val leftChild = remainingExpression.getLeft()
|
result.add(remainingExpression.getRight() ?: return null)
|
||||||
val rightChild = remainingExpression.getRight()
|
remainingExpression = remainingExpression.getLeft() ?: return null
|
||||||
result.add(rightChild)
|
|
||||||
currentItem = leftChild
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.addIfNotNull(currentItem)
|
result.add(remainingExpression)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user