mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
preserve comments: invert if (IDEA-205835)
This commit is contained in:
+10
-7
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// "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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "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");
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,6 @@ class T {
|
||||
throw new RuntimeException("When true");
|
||||
<caret>else {
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
}//c1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user