Commit cc6b79a3 authored by Anna.Kozlova's avatar Anna.Kozlova
Browse files

preserve comments: invert if (IDEA-205835)

parent c518058b
Branches unavailable Tags unavailable
Showing with 44 additions and 10 deletions
+44 -10
......@@ -227,7 +227,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
}
} else {
if (!(thenBranch instanceof PsiReturnStatement)) {
ifStatement = addAfterWithinCodeBlock(ifStatement, ct.markUnchanged(thenBranch));
ifStatement = addAfterWithinCodeBlock(ifStatement, ct.markUnchanged(thenBranch), ct);
}
}
ct.replaceAndRestoreComments(Objects.requireNonNull(ifStatement.getThenBranch()), statement);
......@@ -241,14 +241,14 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
element instanceof PsiForeachStatement && flow.getStartOffset(element) + 1 == endOffset) {
PsiStatement statement = factory.createStatementFromText("continue;", ifStatement);
statement = (PsiStatement)codeStyle.reformat(statement);
ifStatement = addAfterWithinCodeBlock(ifStatement, ct.markUnchanged(thenBranch));
ifStatement = addAfterWithinCodeBlock(ifStatement, ct.markUnchanged(thenBranch), ct);
Objects.requireNonNull(ifStatement.getThenBranch()).replace(statement);
return ifStatement;
}
if (element instanceof PsiReturnStatement) {
PsiReturnStatement returnStatement = (PsiReturnStatement) element;
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch, ct);
ct.replaceAndRestoreComments(Objects.requireNonNull(ifStatement.getThenBranch()), ct.markUnchanged(returnStatement).copy());
ControlFlow flow2 = buildControlFlow(findCodeBlock(ifStatement));
......@@ -356,27 +356,30 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
return stmt.getStatements()[0];
}
private static PsiIfStatement addAfterWithinCodeBlock(@NotNull PsiIfStatement ifStatement, @NotNull PsiStatement branch) {
private static PsiIfStatement addAfterWithinCodeBlock(@NotNull PsiIfStatement ifStatement,
@NotNull PsiStatement branch,
CommentTracker ct) {
final PsiElement parent = ifStatement.getParent();
if (parent != null && !(parent instanceof PsiCodeBlock)) {
branch = (PsiStatement)branch.copy();
ifStatement = (PsiIfStatement)wrapWithCodeBlock(ifStatement);
}
addAfter(ifStatement, branch);
addAfter(ifStatement, branch, ct);
return ifStatement;
}
static void addAfter(PsiIfStatement ifStatement, PsiStatement branch) throws IncorrectOperationException {
static void addAfter(PsiIfStatement ifStatement, PsiStatement branch, CommentTracker ct) throws IncorrectOperationException {
if (branch instanceof PsiBlockStatement) {
PsiBlockStatement blockStatement = (PsiBlockStatement) branch;
final PsiCodeBlock block = blockStatement.getCodeBlock();
final PsiElement firstBodyElement = block.getFirstBodyElement();
final PsiElement lastBodyElement = block.getLastBodyElement();
if (firstBodyElement != null && lastBodyElement != null) {
ct.markRangeUnchanged(firstBodyElement, lastBodyElement);
ifStatement.getParent().addRangeAfter(firstBodyElement, lastBodyElement, ifStatement);
}
} else {
ifStatement.getParent().addAfter(branch, ifStatement);
ifStatement.getParent().addAfter(ct.markUnchanged(branch), ifStatement);
}
}
......
......@@ -24,6 +24,7 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
......@@ -51,8 +52,9 @@ public class UnwrapElseBranchAction extends PsiElementBaseIntentionAction {
elseBranch = ifStatement.getElseBranch();
LOG.assertTrue(elseBranch != null);
}
InvertIfConditionAction.addAfter(ifStatement, elseBranch);
elseBranch.delete();
CommentTracker ct = new CommentTracker();
InvertIfConditionAction.addAfter(ifStatement, elseBranch, ct);
ct.deleteAndRestoreComments(elseBranch);
}
}
}
......
// "Invert 'if' condition" "true"
public class C {
public boolean isAcceptable(Object element) {
if (element == null) {
return false;
}
System.out.println();
//c1
if (true) {
//c2
}
return false;
}
}
// "Invert 'if' condition" "true"
public class C {
public boolean isAcceptable(Object element) {
i<caret>f (element != null) {
System.out.println();
//c1
if (true) {
//c2
}
}
return false;
}
}
......@@ -4,6 +4,8 @@ class T {
void f(boolean b) {
if (b)
throw new RuntimeException("When true");
//c1
System.out.println("Otherwise");
}
}
\ No newline at end of file
......@@ -6,6 +6,6 @@ class T {
throw new RuntimeException("When true");
<caret>else {
System.out.println("Otherwise");
}
}//c1
}
}
\ No newline at end of file
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