mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
keep comments: don't restore comments which repeat in replacement
IDEA-CR-33818
This commit is contained in:
+7
-5
@@ -86,16 +86,18 @@ public class ConditionalCanBePushedInsideExpressionInspection extends BaseInspec
|
||||
if (!match.isPartialMatch()) {
|
||||
return;
|
||||
}
|
||||
CommentTracker commentTracker = new CommentTracker();
|
||||
final PsiElement leftDiff = match.getLeftDiff();
|
||||
final PsiElement rightDiff = match.getRightDiff();
|
||||
final String expression = "(" + commentTracker.text(conditionalExpression.getCondition()) + " ? " +
|
||||
commentTracker.text(leftDiff) + " : " + commentTracker.text(rightDiff) + ")";
|
||||
final String expression = "(" + conditionalExpression.getCondition().getText() + " ? " +
|
||||
leftDiff.getText() + " : " + rightDiff.getText() + ")";
|
||||
final PsiExpression newConditionalExpression =
|
||||
JavaPsiFacade.getElementFactory(project).createExpressionFromText(expression, conditionalExpression);
|
||||
final PsiElement replacedConditionalExpression = commentTracker.replaceAndRestoreComments(leftDiff, newConditionalExpression);
|
||||
final PsiElement replacedConditionalExpression = leftDiff.replace(newConditionalExpression);
|
||||
ParenthesesUtils.removeParentheses((PsiExpression)replacedConditionalExpression, false);
|
||||
new CommentTracker().replaceAndRestoreComments(conditionalExpression, thenExpression);
|
||||
CommentTracker commentTracker = new CommentTracker();
|
||||
commentTracker.markUnchanged(conditionalExpression.getCondition());
|
||||
commentTracker.markUnchanged(thenExpression);
|
||||
commentTracker.replaceAndRestoreComments(conditionalExpression, thenExpression);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -185,7 +185,9 @@ public class ParenthesesUtils {
|
||||
if (!(parent instanceof PsiExpression) || !areParenthesesNeeded(body, (PsiExpression)parent, ignoreClarifyingParentheses)) {
|
||||
PsiExpression newExpression = ExpressionUtils.replacePolyadicWithParent(parenthesizedExpression, body);
|
||||
if (newExpression == null){
|
||||
newExpression = (PsiExpression)new CommentTracker().replaceAndRestoreComments(parenthesizedExpression, body);
|
||||
CommentTracker commentTracker = new CommentTracker();
|
||||
commentTracker.markUnchanged(body);
|
||||
newExpression = (PsiExpression)commentTracker.replaceAndRestoreComments(parenthesizedExpression, body);
|
||||
}
|
||||
removeParentheses(newExpression, ignoreClarifyingParentheses);
|
||||
}
|
||||
|
||||
+2
-1
@@ -13,13 +13,14 @@ public class ConditionalCanBePushedInsideExpressionFixTest extends IGQuickFixesT
|
||||
doTest(InspectionGadgetsBundle.message("conditional.can.be.pushed.inside.expression.quickfix"),
|
||||
"class X {\n" +
|
||||
" String foo(boolean b) {\n" +
|
||||
" return b ? String.v<caret>alueOf(new Double(0)) //c1\n" +
|
||||
" return b/*c0*/ ? String.v<caret>alueOf(new Double(0)) //c1\n" +
|
||||
" : String.valueOf(new Double(1.2))//c2\n" +
|
||||
" ;\n" +
|
||||
" }\n" +
|
||||
"}",
|
||||
"class X {\n" +
|
||||
" String foo(boolean b) {\n" +
|
||||
" /*c0*/\n" +
|
||||
" //c1\n" +
|
||||
" return String.valueOf(new Double(b ? 0 : 1.2))//c2\n" +
|
||||
" ;\n" +
|
||||
|
||||
Reference in New Issue
Block a user