keep comments: don't restore comments which repeat in replacement

IDEA-CR-33818
This commit is contained in:
Anna Kozlova
2018-06-28 11:12:16 +03:00
parent f8263bc48f
commit 17fe0f503a
3 changed files with 12 additions and 7 deletions
@@ -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);
}
}
@@ -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);
}
@@ -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" +