negate comparison intention

This commit is contained in:
alexey.ivanov
2010-03-12 19:39:23 +03:00
parent d9c7bfc2af
commit 377f9fa25c
8 changed files with 115 additions and 1 deletions
@@ -95,6 +95,12 @@
<descriptionDirectoryName>PySplitIfIntention</descriptionDirectoryName>
</intentionAction>
<intentionAction>
<className>com.jetbrains.python.codeInsight.intentions.PyNegateComparisonIntention</className>
<category>Python</category>
<descriptionDirectoryName>PyNegateComparisonIntention</descriptionDirectoryName>
</intentionAction>
<stubElementTypeHolder class="com.jetbrains.python.PyElementTypes"/>
<inspectionToolProvider implementation="com.jetbrains.python.inspections.PythonInspectionToolProvider"/>
@@ -83,6 +83,9 @@ INTN.replace.method=Replace method which is not supported in current Python vers
INTN.split.if=Split if
INTN.split.if.text=Split into 2 if's
INTN.negate.comparison=Negate comparison
INTN.negate.$0.to.$1=Negate ''{0}'' to ''{1}''
# Conflict checker
CONFLICT.name.$0.obscured=Name ''{0}'' obscured by local definitions
CONFLICT.name.$0.obscured.cannot.convert=Name ''{0}'' obscured. Cannot convert.
@@ -0,0 +1,89 @@
package com.jetbrains.python.codeInsight.intentions;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.PythonLanguage;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* Author: Alexey.Ivanov
* Date: 12.03.2010
* Time: 17:58:56
*/
public class PyNegateComparisonIntention extends BaseIntentionAction {
private static final Map<PyElementType, String> comparisonStrings = new HashMap<PyElementType, String>(7);
private static final Map<PyElementType, PyElementType> invertedComparasions = new HashMap<PyElementType, PyElementType>(7);
static {
comparisonStrings.put(PyTokenTypes.LT, "<");
comparisonStrings.put(PyTokenTypes.GT, ">");
comparisonStrings.put(PyTokenTypes.EQEQ, "==");
comparisonStrings.put(PyTokenTypes.LE, "<=");
comparisonStrings.put(PyTokenTypes.GE, ">=");
comparisonStrings.put(PyTokenTypes.NE, "!=");
comparisonStrings.put(PyTokenTypes.NE_OLD, "<>");
invertedComparasions.put(PyTokenTypes.LT, PyTokenTypes.GE);
invertedComparasions.put(PyTokenTypes.GT, PyTokenTypes.LE);
invertedComparasions.put(PyTokenTypes.EQEQ, PyTokenTypes.NE);
invertedComparasions.put(PyTokenTypes.LE, PyTokenTypes.GT);
invertedComparasions.put(PyTokenTypes.GE, PyTokenTypes.LT);
invertedComparasions.put(PyTokenTypes.NE, PyTokenTypes.EQEQ);
invertedComparasions.put(PyTokenTypes.NE_OLD, PyTokenTypes.EQEQ);
}
@NotNull
public String getFamilyName() {
return PyBundle.message("INTN.negate.comparison");
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
PsiElement element = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class, false);
if (element == null) {
return false;
}
PyElementType operator = ((PyBinaryExpression)element).getOperator();
if (!comparisonStrings.containsKey(operator)) {
return false;
}
setText(PyBundle.message("INTN.negate.$0.to.$1",
comparisonStrings.get(operator),
comparisonStrings.get(invertedComparasions.get(operator))));
return true;
}
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiElement element = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class, false);
PsiElement parent = element.getParent();
while (parent instanceof PyParenthesizedExpression) {
parent = parent.getParent();
}
PyBinaryExpression binaryExpression = (PyBinaryExpression)element;
final PyElementType invertedOperator = invertedComparasions.get(binaryExpression.getOperator());
PyElementGenerator elementGenerator = PythonLanguage.getInstance().getElementGenerator();
final PyBinaryExpression newElement = elementGenerator.createBinaryExpression(project,
comparisonStrings.get(invertedOperator),
binaryExpression.getLeftExpression(),
binaryExpression.getRightExpression());
if (parent instanceof PyPrefixExpression && ((PyPrefixExpression)parent).getOperationSign() == PyTokenTypes.NOT_KEYWORD) {
parent.replace(newElement);
} else {
element.replace(elementGenerator.createExpressionFromText(project, "not " + newElement.getText()));
}
}
}
@@ -0,0 +1,2 @@
if b > 3 and a > b:
pass
@@ -0,0 +1,2 @@
if b < 3 or not a <= b:
pass
@@ -0,0 +1,2 @@
if b > 3 and not (a <= <caret> b):
pass
@@ -0,0 +1,2 @@
if b < 3 or a > <caret> b:
pass
@@ -82,7 +82,15 @@ public class PyIntentionTest extends PyLightFixtureTestCase {
doTest(PyBundle.message("INTN.replace.method"), LanguageLevel.PYTHON30);
}
public void testSplitIf()throws Exception {
public void testSplitIf() throws Exception {
doTest(PyBundle.message("INTN.split.if.text"));
}
public void testNegateComparison() throws Exception {
doTest(PyBundle.message("INTN.negate.$0.to.$1", "<=", ">"));
}
public void testNegateComparison2() throws Exception {
doTest(PyBundle.message("INTN.negate.$0.to.$1", ">", "<="));
}
}