From 5a4f85ff35f36db299f8ecb451a76768423bd958 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 15 Mar 2018 12:38:58 +0700 Subject: [PATCH] CommentTracker#makeRangeUnchanged; more comment fixes for InvertIfCondition --- .../intention/impl/InvertIfConditionAction.java | 4 ++-- .../afterConditionWithComment.java | 1 + .../afterContinueWithCommentNoBlock.java | 4 ++-- .../beforeConditionWithComment.java | 2 +- .../src/com/siyeh/ig/psiutils/CommentTracker.java | 15 +++++++++++++++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java index c7e830af8b22..42a92f261baa 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java @@ -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]); diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterConditionWithComment.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterConditionWithComment.java index 368df8b96ab7..3436a02a4042 100644 --- a/java/java-tests/testData/codeInsight/invertIfCondition/afterConditionWithComment.java +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterConditionWithComment.java @@ -4,6 +4,7 @@ class A { public void foo() { String value ="not-null"; + // Another comment if (value == null) { return; } diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterContinueWithCommentNoBlock.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterContinueWithCommentNoBlock.java index c758a140372a..f6e8e28b4404 100644 --- a/java/java-tests/testData/codeInsight/invertIfCondition/afterContinueWithCommentNoBlock.java +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterContinueWithCommentNoBlock.java @@ -2,8 +2,8 @@ class A { void f(){ while (true) { - if (false) //comment - { + //comment + if (false) { System.out.println(); } } diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeConditionWithComment.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeConditionWithComment.java index 3629feb8bcfc..a686e9fe0e98 100644 --- a/java/java-tests/testData/codeInsight/invertIfCondition/beforeConditionWithComment.java +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeConditionWithComment.java @@ -7,6 +7,6 @@ class A { if (value != null) { System.out.println(value); // Comment gets deleted. - } + } // Another comment } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CommentTracker.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CommentTracker.java index fe8024f51627..595e17466dd5 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CommentTracker.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CommentTracker.java @@ -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;