KT-9729 Invert If Condition intention should run Simplify Negated Expression when applicable

#KT-9729 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-10-27 22:11:06 +03:00
parent 87f68f3332
commit ce50a27af0
3 changed files with 12 additions and 6 deletions
@@ -40,6 +40,10 @@ public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetin
}
override fun applyTo(element: KtPrefixExpression, editor: Editor) {
applyTo(element)
}
fun applyTo(element: KtPrefixExpression) {
val parenthesizedExpression = element.getBaseExpression() as KtParenthesizedExpression
val baseExpression = parenthesizedExpression.getExpression() as KtBinaryExpression
@@ -18,18 +18,14 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.quickfix.moveCaret
import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
@@ -45,6 +41,12 @@ public class InvertIfConditionIntention : JetSelfTargetingIntention<KtIfExpressi
val newIf = handleSpecialCases(element, newCondition)
?: handleStandardCase(element, newCondition)
val newIfCondition = newIf.condition
val simplifyIntention = ConvertNegatedExpressionWithDemorgansLawIntention()
if (newIfCondition is KtPrefixExpression && simplifyIntention.isApplicableTo(newIfCondition)) {
simplifyIntention.applyTo(newIfCondition)
}
PsiDocumentManager.getInstance(newIf.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument())
editor.moveCaret(newIf.getTextOffset())
}
@@ -3,7 +3,7 @@ fun foo(): Boolean {
}
fun main() {
if (!(foo() && foo())) {
if (!foo() || !foo()) {
foo()
} else {
foo()