From dabef9b7b1c6fa7f4833137c4da7a94b22ff73fe Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 18 Jun 2018 15:03:31 +0300 Subject: [PATCH] keep comments (IDEA-140861) --- .../quickfix/SimplifyBooleanExpressionFix.java | 2 +- .../inspection/dataFlow/fixture/KeepComments.java | 15 +++++++++++++++ .../dataFlow/fixture/KeepComments_after.java | 15 +++++++++++++++ .../codeInspection/DataFlowInspectionTest.java | 5 +++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/KeepComments.java create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/KeepComments_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index 3cefc651db1a..ab4b1caf5776 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -292,7 +292,7 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { public static void simplifyExpression(PsiExpression expression) throws IncorrectOperationException { final PsiExpression result = createSimplifiedReplacement(expression); - PsiExpression newExpression = (PsiExpression)expression.replace(result); + PsiExpression newExpression = (PsiExpression)new CommentTracker().replaceAndRestoreComments(expression, result); if (newExpression instanceof PsiLiteralExpression) { final PsiElement parent = newExpression.getParent(); if (parent instanceof PsiAssertStatement && ((PsiLiteralExpression)newExpression).getValue() == Boolean.TRUE) { diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/KeepComments.java b/java/java-tests/testData/inspection/dataFlow/fixture/KeepComments.java new file mode 100644 index 000000000000..0e50d0316b9d --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/KeepComments.java @@ -0,0 +1,15 @@ +// "Simplify boolean expression" "true" +class A { + public static void m(boolean fullSearch, boolean partialSearch) { + + + if (!partialSearch) { + return; + } + + String str + = fullSearch ? "str1" + : partialSearch ? "str2 " + "str3" // comment + : null; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/KeepComments_after.java b/java/java-tests/testData/inspection/dataFlow/fixture/KeepComments_after.java new file mode 100644 index 000000000000..e3fb4813f294 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/KeepComments_after.java @@ -0,0 +1,15 @@ +// "Simplify boolean expression" "true" +class A { + public static void m(boolean fullSearch, boolean partialSearch) { + + + if (!partialSearch) { + return; + } + + // comment + String str + = fullSearch ? "str1" + : "str2 " + "str3"; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java index e84d12503901..0a9665af2521 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java @@ -280,6 +280,11 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase { public void testFinalGetter() { doTest(); } public void testGetterResultsNotSame() { doTest(); } public void testIntersectionTypeInstanceof() { doTest(); } + + public void testKeepComments() { + doTest(); + checkIntentionResult("Simplify"); + } public void testImmutableClassNonGetterMethod() { myFixture.addClass("package javax.annotation.concurrent; public @interface Immutable {}");