mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Attempt to make ChainedComparisonsQuickFix more readable
This commit is contained in:
+36
-28
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 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.
|
||||
@@ -39,21 +39,21 @@ import static com.jetbrains.python.psi.PyUtil.as;
|
||||
*/
|
||||
public class ChainedComparisonsQuickFix implements LocalQuickFix {
|
||||
|
||||
boolean myIsLeftLeft;
|
||||
boolean myIsRightLeft;
|
||||
boolean getInnerRight;
|
||||
private final boolean myCommonIsInLeftLeft;
|
||||
private final boolean myCommonIsInRightLeft;
|
||||
private final boolean myUseRightChildOfLeft;
|
||||
|
||||
/**
|
||||
* @param isLeft true if common expression is on the left hand side of the left comparison
|
||||
* @param isRight true if common expression is on the left hand side of the right comparison
|
||||
* @param getInner whether left comparison is deeper in PSI tree than the right comparison.
|
||||
* E.g. in {@code foo and x > 1 and x < 3} expressions {@code x > 1} and {@code x < 3} are targets for simplification but
|
||||
* because of associativity of {@code and} operator they are not siblings: {@code (foo and x > 1) and x < 3}
|
||||
* @param commonIsInLeftLeft true if common expression is on the left hand side of the left comparison
|
||||
* @param commonIsInRightLeft true if common expression is on the left hand side of the right comparison
|
||||
* @param useRightChildOfLeft whether left comparison is deeper in PSI tree than the right comparison.
|
||||
* E.g. in {@code foo and x > 1 and x < 3} expressions {@code x > 1} and {@code x < 3} are targets for simplification but
|
||||
* because of associativity of {@code and} operator they are not siblings: {@code (foo and x > 1) and x < 3}
|
||||
*/
|
||||
public ChainedComparisonsQuickFix(boolean isLeft, boolean isRight, boolean getInner) {
|
||||
myIsLeftLeft = isLeft;
|
||||
myIsRightLeft = isRight;
|
||||
getInnerRight = getInner;
|
||||
public ChainedComparisonsQuickFix(boolean commonIsInLeftLeft, boolean commonIsInRightLeft, boolean useRightChildOfLeft) {
|
||||
myCommonIsInLeftLeft = commonIsInLeftLeft;
|
||||
myCommonIsInRightLeft = commonIsInRightLeft;
|
||||
myUseRightChildOfLeft = useRightChildOfLeft;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -70,24 +70,32 @@ public class ChainedComparisonsQuickFix implements LocalQuickFix {
|
||||
final PyBinaryExpression expression = as(descriptor.getPsiElement(), PyBinaryExpression.class);
|
||||
if (expression != null && expression.isWritable()) {
|
||||
final PyBinaryExpression rightExpression = as(expression.getRightExpression(), PyBinaryExpression.class);
|
||||
PyBinaryExpression leftExpression = as(expression.getLeftExpression(), PyBinaryExpression.class);
|
||||
final PyBinaryExpression leftExpression = as(expression.getLeftExpression(), PyBinaryExpression.class);
|
||||
|
||||
if (rightExpression != null && leftExpression != null && isLogicalAndExpression(expression)) {
|
||||
if (getInnerRight && leftExpression.getRightExpression() instanceof PyBinaryExpression && isLogicalAndExpression(leftExpression)) {
|
||||
leftExpression = (PyBinaryExpression)leftExpression.getRightExpression();
|
||||
}
|
||||
checkOperator(assertNotNull(leftExpression), rightExpression, project);
|
||||
applyFix(goDownIfNeeded(leftExpression), rightExpression, project);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkOperator(@NotNull PyBinaryExpression leftExpression,
|
||||
@NotNull PyBinaryExpression rightExpression,
|
||||
@NotNull Project project) {
|
||||
@NotNull
|
||||
private PyBinaryExpression goDownIfNeeded(@NotNull PyBinaryExpression expression) {
|
||||
final PyExpression rightExpression = expression.getRightExpression();
|
||||
|
||||
if (myUseRightChildOfLeft && rightExpression instanceof PyBinaryExpression && isLogicalAndExpression(expression)) {
|
||||
return (PyBinaryExpression)rightExpression;
|
||||
}
|
||||
else {
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
|
||||
private void applyFix(@NotNull PyBinaryExpression leftExpression, @NotNull PyBinaryExpression rightExpression, @NotNull Project project) {
|
||||
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
|
||||
if (myIsLeftLeft) {
|
||||
if (myCommonIsInLeftLeft) {
|
||||
final PyExpression newLeftExpression = invertExpression(leftExpression, elementGenerator);
|
||||
|
||||
if (myIsRightLeft) {
|
||||
if (myCommonIsInRightLeft) {
|
||||
final PsiElement operator = getLeftestOperator(rightExpression);
|
||||
final PyBinaryExpression binaryExpression = elementGenerator.createBinaryExpression(
|
||||
operator.getText(), newLeftExpression, getLargeRightExpression(rightExpression, project));
|
||||
@@ -103,7 +111,7 @@ public class ChainedComparisonsQuickFix implements LocalQuickFix {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (myIsRightLeft) {
|
||||
if (myCommonIsInRightLeft) {
|
||||
final PsiElement operator = getLeftestOperator(rightExpression);
|
||||
final PyBinaryExpression binaryExpression = elementGenerator.createBinaryExpression(
|
||||
operator.getText(), leftExpression, getLargeRightExpression(rightExpression, project));
|
||||
@@ -136,10 +144,10 @@ public class ChainedComparisonsQuickFix implements LocalQuickFix {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PyExpression invertExpression(@NotNull PyBinaryExpression leftExpression, @NotNull PyElementGenerator elementGenerator) {
|
||||
final PsiElement operator = leftExpression.getPsiOperator();
|
||||
final PyExpression right = leftExpression.getRightExpression();
|
||||
PyExpression left = leftExpression.getLeftExpression();
|
||||
private static PyExpression invertExpression(@NotNull PyBinaryExpression expression, @NotNull PyElementGenerator elementGenerator) {
|
||||
final PsiElement operator = expression.getPsiOperator();
|
||||
final PyExpression right = expression.getRightExpression();
|
||||
PyExpression left = expression.getLeftExpression();
|
||||
if (isComparisonExpression(left)) {
|
||||
left = invertExpression((PyBinaryExpression)left, elementGenerator);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user