From 4f4012fecf4a18f4a1ec348e9236cf9fa59a0801 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Wed, 10 Oct 2018 15:55:15 +0200 Subject: [PATCH] preserve comments: invert if in case when else branch become empty --- .../intention/impl/InvertIfConditionAction.java | 2 +- .../invertIfCondition/afterEmptyElseWithComments.java | 10 ++++++++++ .../invertIfCondition/beforeEmptyElseWithComments.java | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/invertIfCondition/afterEmptyElseWithComments.java create mode 100644 java/java-tests/testData/codeInsight/invertIfCondition/beforeEmptyElseWithComments.java 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 26f0f70ba2ae..1c1b426a6afc 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 @@ -142,7 +142,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction { elseBranch = Objects.requireNonNull(ifStatement.getElseBranch()).replace(elseBranch); if (emptyBlock(((PsiBlockStatement)elseBranch).getCodeBlock())) { - ifStatement.getElseBranch().delete(); + new CommentTracker().deleteAndRestoreComments(ifStatement.getElseBranch()); } } } diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterEmptyElseWithComments.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterEmptyElseWithComments.java new file mode 100644 index 000000000000..6c5174cb9726 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterEmptyElseWithComments.java @@ -0,0 +1,10 @@ +// "Invert 'if' condition" "true" +class A { + public void foo() { + if (c) { + } + //c1 + + System.out.println(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeEmptyElseWithComments.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeEmptyElseWithComments.java new file mode 100644 index 000000000000..2c2a21ccbe78 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeEmptyElseWithComments.java @@ -0,0 +1,9 @@ +// "Invert 'if' condition" "true" +class A { + public void foo() { + if (!c) { + continue; + }//c1 + System.out.println(); + } +} \ No newline at end of file