mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
preserve comments: deep demorgan comments retrieval
This commit is contained in:
@@ -50,15 +50,12 @@ public class DemorgansIntention extends MutablyNamedIntention {
|
||||
@Override
|
||||
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
|
||||
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)element;
|
||||
final String newExpression = convertConjunctionExpression(polyadicExpression);
|
||||
CommentTracker tracker = new CommentTracker();
|
||||
for (PsiExpression expression : polyadicExpression.getOperands()) {
|
||||
tracker.markUnchanged(expression);
|
||||
}
|
||||
final String newExpression = convertConjunctionExpression(polyadicExpression, tracker);
|
||||
replaceExpressionWithNegatedExpressionString(newExpression, polyadicExpression, tracker);
|
||||
}
|
||||
|
||||
private static String convertConjunctionExpression(PsiPolyadicExpression polyadicExpression) {
|
||||
private static String convertConjunctionExpression(PsiPolyadicExpression polyadicExpression, CommentTracker tracker) {
|
||||
final IElementType tokenType = polyadicExpression.getOperationTokenType();
|
||||
final boolean tokenTypeAndAnd = tokenType.equals(JavaTokenType.ANDAND);
|
||||
final String flippedConjunction = tokenTypeAndAnd ? "||" : "&&";
|
||||
@@ -67,12 +64,14 @@ public class DemorgansIntention extends MutablyNamedIntention {
|
||||
if (result.length() != 0) {
|
||||
result.append(flippedConjunction);
|
||||
}
|
||||
result.append(convertLeafExpression(operand, tokenTypeAndAnd));
|
||||
result.append(convertLeafExpression(operand, tokenTypeAndAnd, tracker));
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static String convertLeafExpression(PsiExpression expression, boolean tokenTypeAndAnd) {
|
||||
private static String convertLeafExpression(PsiExpression expression,
|
||||
boolean tokenTypeAndAnd,
|
||||
CommentTracker tracker) {
|
||||
if (BoolUtils.isNegation(expression)) {
|
||||
final PsiExpression negatedExpression = BoolUtils.getNegated(expression);
|
||||
if (negatedExpression == null) {
|
||||
@@ -80,12 +79,12 @@ public class DemorgansIntention extends MutablyNamedIntention {
|
||||
}
|
||||
if (tokenTypeAndAnd) {
|
||||
if (ParenthesesUtils.getPrecedence(negatedExpression) > ParenthesesUtils.OR_PRECEDENCE) {
|
||||
return '(' + negatedExpression.getText() + ')';
|
||||
return '(' + tracker.markUnchanged(negatedExpression).getText() + ')';
|
||||
}
|
||||
} else if (ParenthesesUtils.getPrecedence(negatedExpression) > ParenthesesUtils.AND_PRECEDENCE) {
|
||||
return '(' + negatedExpression.getText() + ')';
|
||||
return '(' + tracker.markUnchanged(negatedExpression).getText() + ')';
|
||||
}
|
||||
return negatedExpression.getText();
|
||||
return tracker.markUnchanged(negatedExpression).getText();
|
||||
}
|
||||
else if (ComparisonUtils.isComparison(expression)) {
|
||||
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)expression;
|
||||
@@ -93,13 +92,13 @@ public class DemorgansIntention extends MutablyNamedIntention {
|
||||
final PsiExpression lhs = binaryExpression.getLOperand();
|
||||
final PsiExpression rhs = binaryExpression.getROperand();
|
||||
assert rhs != null;
|
||||
return lhs.getText() + negatedComparison + rhs.getText();
|
||||
return tracker.markUnchanged(lhs).getText() + negatedComparison + tracker.markUnchanged(rhs).getText();
|
||||
}
|
||||
else if (ParenthesesUtils.getPrecedence(expression) > ParenthesesUtils.PREFIX_PRECEDENCE) {
|
||||
return "!(" + expression.getText() + ')';
|
||||
return "!(" + tracker.markUnchanged(expression).getText() + ')';
|
||||
}
|
||||
else {
|
||||
return '!' + expression.getText();
|
||||
return '!' + tracker.markUnchanged(expression).getText();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -2,6 +2,7 @@ package com.siyeh.ipp.bool.demorgans;
|
||||
|
||||
class NotTooManyParentheses {
|
||||
void foo(boolean a, boolean b, boolean c) {
|
||||
if (a && (b ||<caret> c)) {}
|
||||
if (a && (b ||<caret> c//cooment inside expr
|
||||
== c)) {}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -2,6 +2,7 @@ package com.siyeh.ipp.bool.demorgans;
|
||||
|
||||
class NotTooManyParentheses {
|
||||
void foo(boolean a, boolean b, boolean c) {
|
||||
if (a && !(!b && !c)) {}
|
||||
//cooment inside expr
|
||||
if (a && !(!b && c != c)) {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user