mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fixed PY-4697 "join 2 if's" inspection suggestion is invalid
This commit is contained in:
@@ -9,6 +9,7 @@ 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.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -65,17 +66,24 @@ public class PyJoinIfIntention extends BaseIntentionAction {
|
||||
PyIfStatement ifStatement = getIfStatement(expression);
|
||||
|
||||
PyStatement firstStatement = getFirstStatement(ifStatement);
|
||||
|
||||
if (ifStatement == null) return;
|
||||
if (firstStatement != null && firstStatement instanceof PyIfStatement) {
|
||||
PyExpression condition = ((PyIfStatement)firstStatement).getIfPart().getCondition();
|
||||
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
|
||||
PyExpression newCondition = elementGenerator.createExpressionFromText(
|
||||
ifStatement.getIfPart().getCondition().getText() + " and " + condition.getText());
|
||||
ifStatement.getIfPart().getCondition().replace(newCondition);
|
||||
PyExpression ifCondition = ifStatement.getIfPart().getCondition();
|
||||
if (ifCondition == null || condition == null) return;
|
||||
StringBuilder replacementText = new StringBuilder(ifCondition.getText() + " and ");
|
||||
if (condition instanceof PyBinaryExpression && ((PyBinaryExpression)condition).getOperator() == PyTokenTypes.OR_KEYWORD) {
|
||||
replacementText.append("(").append(condition.getText()).append(")");
|
||||
} else
|
||||
replacementText.append(condition.getText());
|
||||
|
||||
PyExpression newCondition = elementGenerator.createExpressionFromText(replacementText.toString());
|
||||
ifCondition.replace(newCondition);
|
||||
|
||||
PyStatementList stList = ((PyIfStatement)firstStatement).getIfPart().getStatementList();
|
||||
PyStatementList ifStatementList = ifStatement.getIfPart().getStatementList();
|
||||
|
||||
if (ifStatementList == null || stList == null) return;
|
||||
List<PsiComment> comments = PsiTreeUtil.getChildrenOfTypeAsList(ifStatement.getIfPart(), PsiComment.class);
|
||||
comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(((PyIfStatement)firstStatement).getIfPart(), PsiComment.class));
|
||||
comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(ifStatementList, PsiComment.class));
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
if value is not None and (not validate_uint(value) or value <= self.begin):
|
||||
print value
|
||||
@@ -0,0 +1,3 @@
|
||||
if value is not<caret> None:
|
||||
if not validate_uint(value) or value <= self.begin:
|
||||
print value
|
||||
@@ -134,6 +134,10 @@ public class PyIntentionTest extends PyTestCase {
|
||||
doNegativeTest(PyBundle.message("INTN.join.if.text"));
|
||||
}
|
||||
|
||||
public void testJoinIfBinary() { //PY-4697
|
||||
doTest(PyBundle.message("INTN.join.if.text"));
|
||||
}
|
||||
|
||||
public void testJoinIfMultiStatements() { //PY-2970
|
||||
doNegativeTest(PyBundle.message("INTN.join.if.text"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user