CommentTracker#makeRangeUnchanged; more comment fixes for InvertIfCondition

This commit is contained in:
Tagir Valeev
2018-03-15 12:38:58 +07:00
parent 628b803c27
commit 5a4f85ff35
5 changed files with 21 additions and 5 deletions
@@ -197,8 +197,8 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
PsiElement lastElement = codeBlock.getLastBodyElement();
if (firstElement != null && lastElement != null) {
ifStatement.getParent().addRangeAfter(firstElement, lastElement, ifStatement);
ct.markRangeUnchanged(firstElement, lastElement);
}
ct.markUnchanged(thenBranch);
} else {
if (!(thenBranch instanceof PsiReturnStatement)) {
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
@@ -302,7 +302,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
}
return;
}
else if (thenBranch instanceof PsiBlockStatement) {
if (thenBranch instanceof PsiBlockStatement) {
PsiStatement[] statements = ((PsiBlockStatement) thenBranch).getCodeBlock().getStatements();
if (statements.length > 0 && statements[statements.length - 1] instanceof PsiContinueStatement) {
new CommentTracker().deleteAndRestoreComments(statements[statements.length - 1]);
@@ -4,6 +4,7 @@ class A {
public void foo() {
String value ="not-null";
// Another comment
if (value == null) {
return;
}
@@ -2,8 +2,8 @@
class A {
void f(){
while (true) {
if (false) //comment
{
//comment
if (false) {
System.out.println();
}
}
@@ -7,6 +7,6 @@ class A {
<caret>if (value != null) {
System.out.println(value);
// Comment gets deleted.
}
} // Another comment
}
}
@@ -79,6 +79,18 @@ public class CommentTracker {
return element;
}
public void markRangeUnchanged(@NotNull PsiElement firstElement, @NotNull PsiElement lastElement) {
checkState();
PsiElement e;
for (e = firstElement; e != null && e != lastElement; e = e.getNextSibling()) {
addIgnored(e);
}
if (e == null) {
throw new IllegalArgumentException("Elements must be siblings: " + firstElement + " and " + lastElement);
}
addIgnored(lastElement);
}
/**
* Deletes given PsiElement collecting all the comments inside it.
*
@@ -168,6 +180,9 @@ public class CommentTracker {
if (anchor instanceof PsiVariable && anchor.getParent() instanceof PsiDeclarationStatement) {
anchor = anchor.getParent();
}
if (anchor instanceof PsiStatement && (anchor.getParent() instanceof PsiIfStatement || anchor.getParent() instanceof PsiLoopStatement)) {
anchor = anchor.getParent();
}
if (anchor == null) anchor = result;
insertCommentsBefore(anchor);
return result;