From db78755a84048386ab556891679e457c977116f5 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Thu, 19 Feb 2015 10:34:04 +0300 Subject: [PATCH] IDEA-136452 Can't surround with {} code starting from the comment under if statement --- .../intellij/codeInsight/CodeInsightUtil.java | 4 ++- ...urroundIfBranchWithNoBracesAndComment.java | 7 ++++ ...dIfBranchWithNoBracesAndComment_after.java | 8 +++++ .../surroundWith/JavaSurroundWithTest.java | 36 +++++++++++-------- 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment.java create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java b/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java index ca7c26dbc811..65c6777bcbbb 100644 --- a/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java @@ -106,7 +106,9 @@ public class CodeInsightUtil { if (parent == null) return PsiElement.EMPTY_ARRAY; while (true) { if (parent instanceof PsiStatement) { - parent = parent.getParent(); + if (!(element1 instanceof PsiComment || element2 instanceof PsiComment)) { + parent = parent.getParent(); + } break; } if (parent instanceof PsiCodeBlock) break; diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment.java new file mode 100644 index 000000000000..563c1795a3f6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment.java @@ -0,0 +1,7 @@ +class Test { + void m() { + if (true) + // comment + System.out.println(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment_after.java new file mode 100644 index 000000000000..801e09e42baa --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundIfBranchWithNoBracesAndComment_after.java @@ -0,0 +1,8 @@ +class Test { + void m() { + if (true) { + // comment + System.out.println(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java index c4dce5fd134b..dd8b42ffd98f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java @@ -94,11 +94,11 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { } public void testSurroundNonExpressionWithParenthesis() throws Exception { - doTest(getTestName(false), new JavaWithParenthesesSurrounder()); + doTest(new JavaWithParenthesesSurrounder()); } public void testSurroundNonExpressionWithCast() throws Exception { - doTest(getTestName(false), new JavaWithCastSurrounder()); + doTest(new JavaWithCastSurrounder()); } public void testSurroundExpressionWithCastEmptyLineAfter() throws Exception { @@ -110,52 +110,52 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { } public void testSurroundNonExpressionWithNot() throws Exception { - doTest(getTestName(false), new JavaWithNotSurrounder()); + doTest(new JavaWithNotSurrounder()); } public void testSurroundBinaryWithCast() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithCastSurrounder()); + doTest(new JavaWithCastSurrounder()); } public void testSurroundConditionalWithCast() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithCastSurrounder()); + doTest(new JavaWithCastSurrounder()); } public void testSurroundAssignmentWithCast() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithCastSurrounder()); + doTest(new JavaWithCastSurrounder()); } public void testSurroundWithNotNullCheck() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithNullCheckSurrounder()); + doTest(new JavaWithNullCheckSurrounder()); } public void testSurroundExpressionWithIf() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithIfExpressionSurrounder()); + doTest(new JavaWithIfExpressionSurrounder()); } public void testSurroundExpressionWithIfForBoxedBooleans() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithIfExpressionSurrounder()); + doTest(new JavaWithIfExpressionSurrounder()); } public void testSurroundExpressionWithNotForBoxedBooleans() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithNotSurrounder()); + doTest(new JavaWithNotSurrounder()); } public void testSurroundExpressionWithElseIf() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithIfExpressionSurrounder()); + doTest(new JavaWithIfExpressionSurrounder()); } public void testSurroundExpressionWithElseIfElse() { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - doTest(getTestName(false), new JavaWithIfElseExpressionSurrounder()); + doTest(new JavaWithIfElseExpressionSurrounder()); } public void testSurroundWithTryFinallyUsingIndents() { @@ -163,7 +163,7 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { boolean oldUseTabs = indentOptions.USE_TAB_CHARACTER; try { indentOptions.USE_TAB_CHARACTER = true; - doTest(getTestName(false), new JavaWithTryFinallySurrounder()); + doTest(new JavaWithTryFinallySurrounder()); } finally { indentOptions.USE_TAB_CHARACTER = oldUseTabs; @@ -171,9 +171,17 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { } public void testSurroundWithTryCatchFunctionalExpression() { - doTest(getTestName(false), new JavaWithTryCatchSurrounder()); + doTest(new JavaWithTryCatchSurrounder()); } + public void testSurroundIfBranchWithNoBracesAndComment() { + doTest(new JavaWithBlockSurrounder()); + } + + private void doTest(Surrounder surrounder) { + doTest(getTestName(false), surrounder); + } + private void doTest(@NotNull String fileName, final Surrounder surrounder) { configureByFile(BASE_PATH + fileName + ".java");