Commit d2fbc49a authored by Anna Kozlova's avatar Anna Kozlova
Browse files

simplify if: preserve comments (IDEA-197511)

parent de4e4520
Showing with 11 additions and 6 deletions
+11 -6
......@@ -243,19 +243,23 @@ public class TrivialIfInspection extends BaseInspection implements CleanupLocalI
if (condition == null) {
return;
}
final String conditionText = BoolUtils.getNegatedExpressionText(condition);
CommentTracker commentTracker = new CommentTracker();
final String conditionText = BoolUtils.getNegatedExpressionText(condition, commentTracker);
final PsiReturnStatement nextStatement = ControlFlowUtils.getNextReturnStatement(statement);
if (nextStatement == null) {
return;
}
final PsiElement nextSibling = statement.getNextSibling();
PsiElement nextSibling = statement.getNextSibling();
if (nextSibling != nextStatement && nextStatement.getParent() == statement.getParent()) {
statement.getParent().deleteChildRange(nextSibling, nextStatement.getPrevSibling());
while (nextSibling != nextStatement && nextSibling != null) {
commentTracker.delete(nextSibling);
nextSibling = nextSibling.getNextSibling();
}
}
@NonNls final String newStatement = "return " + conditionText + ';';
PsiReplacementUtil.replaceStatement(statement, newStatement);
PsiReplacementUtil.replaceStatement(statement, newStatement, commentTracker);
if (!ControlFlowUtils.isReachable(nextStatement)) {
nextStatement.delete();
new CommentTracker().deleteAndRestoreComments(nextStatement);
}
}
......
......@@ -8,6 +8,7 @@ class NegatedConditional {
final NegatedConditional that = (NegatedConditional)object;
//comment
return text != null ? text.equals(that.text) : that.text == null;
}
......
......@@ -8,7 +8,7 @@ class NegatedConditional {
final NegatedConditional that = (NegatedConditional)object;
<caret>if (text != null ? !text.equals(that.text) : that.text != null) return false;
<caret>if (text != null ? !text.equals(that.text) : that.text != null) return false; //comment
return true;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment