diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaStatementsSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaStatementsSurrounder.java index 069ab3d1beab..836ddf42b5bc 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaStatementsSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaStatementsSurrounder.java @@ -16,15 +16,15 @@ */ package com.intellij.codeInsight.generation.surroundWith; -import com.intellij.psi.*; - -import com.intellij.openapi.project.Project; -import com.intellij.openapi.editor.Editor; - -import com.intellij.openapi.util.TextRange; -import com.intellij.util.IncorrectOperationException; import com.intellij.lang.surroundWith.Surrounder; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.util.IncorrectOperationException; import com.intellij.util.containers.ContainerUtil; +import com.siyeh.ig.psiutils.CommentTracker; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -44,4 +44,67 @@ abstract class JavaStatementsSurrounder implements Surrounder { } @Nullable protected abstract TextRange surroundStatements(final Project project, final Editor editor, final PsiElement container, final PsiElement[] statements) throws IncorrectOperationException; + + @NotNull + protected PsiStatement addAfter(final PsiStatement statement, final PsiElement container, final PsiElement[] statements) { + if (container instanceof PsiSwitchLabeledRuleStatement && !(statement instanceof PsiBlockStatement)) { + Project project = container.getProject(); + PsiManager manager = PsiManager.getInstance(project); + PsiElementFactory factory = JavaPsiFacade.getElementFactory(manager.getProject()); + CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); + + PsiBlockStatement blockStatement = (PsiBlockStatement)factory.createStatementFromText("{\n}", null); + blockStatement = (PsiBlockStatement)codeStyleManager.reformat(blockStatement); + blockStatement = (PsiBlockStatement)container.addAfter(blockStatement, statements[statements.length - 1]); + + return (PsiStatement)blockStatement.getCodeBlock().add(statement); + } + return (PsiStatement)container.addAfter(statement, statements[statements.length - 1]); + } + + protected static void addRangeWithinContainer(PsiCodeBlock codeBlock, PsiElement container, PsiElement[] statements, boolean canBreak) { + if (container instanceof PsiSwitchLabeledRuleStatement && statements.length == 1) { + PsiElement statement = statements[0]; + if (statement instanceof PsiExpressionStatement && canBreak) { + addBreakWithValue(codeBlock, (PsiExpressionStatement)statement); + return; + } + if (statement instanceof PsiBlockStatement) { + addCodeBlockContents(codeBlock, (PsiBlockStatement)statement); + return; + } + } + + codeBlock.addRange(statements[0], statements[statements.length - 1]); + } + + private static void addBreakWithValue(PsiCodeBlock codeBlock, PsiExpressionStatement statement) { + PsiExpressionStatement wrappedStatement = (PsiExpressionStatement)codeBlock.add(statement); + CommentTracker tracker = new CommentTracker(); + tracker.markUnchanged(wrappedStatement.getExpression()); + + PsiElementFactory factory = JavaPsiFacade.getElementFactory(codeBlock.getProject()); + PsiBreakStatement breakStatement = (PsiBreakStatement)factory.createStatementFromText("break 0;", null); + breakStatement = (PsiBreakStatement)tracker.replaceAndRestoreComments(wrappedStatement, breakStatement); + + PsiExpression breakExpression = breakStatement.getExpression(); + assert breakExpression != null : "breakExpression"; + breakExpression.replace(statement.getExpression()); + } + + protected static void addCodeBlockContents(PsiCodeBlock codeBlock, PsiBlockStatement statement) { + // could just replace one code block with the other but then we lose some comments and formatting + PsiBlockStatement tempStatement = (PsiBlockStatement)codeBlock.add(statement); + PsiCodeBlock tempBlock = tempStatement.getCodeBlock(); + PsiJavaToken lBrace = tempBlock.getLBrace(); + PsiJavaToken rBrace = tempBlock.getRBrace(); + if (lBrace != null && rBrace != null) { + CommentTracker tracker = new CommentTracker(); + for (PsiElement element = lBrace.getNextSibling(); element != null && element != rBrace; element = element.getNextSibling()) { + tracker.markUnchanged(element); + codeBlock.addBefore(element, tempStatement); + } + tracker.deleteAndRestoreComments(tempStatement); + } + } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java index bbb2ecf3117d..1b57cb0938c6 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java @@ -44,11 +44,11 @@ public class JavaWithBlockSurrounder extends JavaStatementsSurrounder{ PsiBlockStatement blockStatement = (PsiBlockStatement)factory.createStatementFromText(text, null); blockStatement = (PsiBlockStatement)codeStyleManager.reformat(blockStatement); - blockStatement = (PsiBlockStatement)container.addBefore(blockStatement, statements[0]); + blockStatement = (PsiBlockStatement)addAfter(blockStatement, container, statements); PsiCodeBlock body = blockStatement.getCodeBlock(); SurroundWithUtil.indentCommentIfNecessary(body, statements); - body.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(body, container, statements, true); container.deleteChildRange(statements[0], statements[statements.length - 1]); PsiElement firstChild = blockStatement.getFirstChild(); diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithDoWhileSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithDoWhileSurrounder.java index f2dbba79264c..95fc984af48b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithDoWhileSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithDoWhileSurrounder.java @@ -46,7 +46,7 @@ public class JavaWithDoWhileSurrounder extends JavaStatementsSurrounder{ PsiDoWhileStatement doWhileStatement = (PsiDoWhileStatement)factory.createStatementFromText(text, null); doWhileStatement = (PsiDoWhileStatement)codeStyleManager.reformat(doWhileStatement); - doWhileStatement = (PsiDoWhileStatement)container.addAfter(doWhileStatement, statements[statements.length - 1]); + doWhileStatement = (PsiDoWhileStatement)addAfter(doWhileStatement, container, statements); PsiStatement body = doWhileStatement.getBody(); if (!(body instanceof PsiBlockStatement)) { @@ -54,7 +54,7 @@ public class JavaWithDoWhileSurrounder extends JavaStatementsSurrounder{ } PsiCodeBlock bodyBlock = ((PsiBlockStatement)body).getCodeBlock(); SurroundWithUtil.indentCommentIfNecessary(bodyBlock, statements); - bodyBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(bodyBlock, container, statements, false); container.deleteChildRange(statements[0], statements[statements.length - 1]); PsiExpression condition = doWhileStatement.getCondition(); diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java index ec4d9a5ff1ab..9e130a857e75 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java @@ -47,7 +47,7 @@ public class JavaWithForSurrounder extends JavaStatementsSurrounder{ PsiForStatement forStatement = (PsiForStatement)factory.createStatementFromText(text, null); forStatement = (PsiForStatement)codeStyleManager.reformat(forStatement); - forStatement = (PsiForStatement)container.addAfter(forStatement, statements[statements.length - 1]); + forStatement = (PsiForStatement)addAfter(forStatement, container, statements); PsiStatement body = forStatement.getBody(); if (!(body instanceof PsiBlockStatement)) { @@ -55,7 +55,7 @@ public class JavaWithForSurrounder extends JavaStatementsSurrounder{ } PsiCodeBlock bodyBlock = ((PsiBlockStatement)body).getCodeBlock(); SurroundWithUtil.indentCommentIfNecessary(bodyBlock, statements); - bodyBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(bodyBlock, container, statements, false); container.deleteChildRange(statements[0], statements[statements.length - 1]); forStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(forStatement); diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseSurrounder.java index 8e2ccc303086..fe4407dce3da 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseSurrounder.java @@ -47,7 +47,7 @@ public class JavaWithIfElseSurrounder extends JavaStatementsSurrounder{ PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(text, null); ifStatement = (PsiIfStatement)codeStyleManager.reformat(ifStatement); - ifStatement = (PsiIfStatement)container.addAfter(ifStatement, statements[statements.length - 1]); + ifStatement = (PsiIfStatement)addAfter(ifStatement, container, statements); PsiStatement thenBranch = ifStatement.getThenBranch(); if (!(thenBranch instanceof PsiBlockStatement)) { @@ -55,7 +55,7 @@ public class JavaWithIfElseSurrounder extends JavaStatementsSurrounder{ } PsiCodeBlock thenBlock = ((PsiBlockStatement)thenBranch).getCodeBlock(); SurroundWithUtil.indentCommentIfNecessary(thenBlock, statements); - thenBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(thenBlock, container, statements, true); container.deleteChildRange(statements[0], statements[statements.length - 1]); ifStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(ifStatement); PsiExpression condition = ifStatement.getCondition(); diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfSurrounder.java index b6da7b7971ad..f019dbe4ec93 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfSurrounder.java @@ -47,13 +47,13 @@ public class JavaWithIfSurrounder extends JavaStatementsSurrounder{ PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(text, null); ifStatement = (PsiIfStatement)codeStyleManager.reformat(ifStatement); - ifStatement = (PsiIfStatement)container.addAfter(ifStatement, statements[statements.length - 1]); + ifStatement = (PsiIfStatement)addAfter(ifStatement, container, statements); final PsiStatement thenBranch = ifStatement.getThenBranch(); if (thenBranch != null) { PsiCodeBlock thenBlock = ((PsiBlockStatement)thenBranch).getCodeBlock(); SurroundWithUtil.indentCommentIfNecessary(thenBlock, statements); - thenBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(thenBlock, container, statements, true); container.deleteChildRange(statements[0], statements[statements.length - 1]); } diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithRunnableSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithRunnableSurrounder.java index 6f0451e3517b..55dd05af14e9 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithRunnableSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithRunnableSurrounder.java @@ -53,7 +53,7 @@ public class JavaWithRunnableSurrounder extends JavaStatementsSurrounder{ PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)factory.createStatementFromText(text, null); declarationStatement = (PsiDeclarationStatement)codeStyleManager.reformat(declarationStatement); - declarationStatement = (PsiDeclarationStatement)container.addAfter(declarationStatement, statements[statements.length - 1]); + declarationStatement = (PsiDeclarationStatement)addAfter(declarationStatement, container, statements); final PsiVariable variable = (PsiVariable)declarationStatement.getDeclaredElements()[0]; @@ -71,8 +71,8 @@ public class JavaWithRunnableSurrounder extends JavaStatementsSurrounder{ makeVariablesFinal(body, body); - final int textOffset = variable.getNameIdentifier().getTextOffset(); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); + final int textOffset = variable.getNameIdentifier().getTextOffset(); editor.getCaretModel().moveToOffset(textOffset); editor.getSelectionModel().removeSelection(); new VariableInplaceRenamer(variable, editor){ diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java index dea2777aaba9..b3e9867ec60c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java @@ -47,14 +47,14 @@ public class JavaWithSynchronizedSurrounder extends JavaStatementsSurrounder{ PsiSynchronizedStatement synchronizedStatement = (PsiSynchronizedStatement)factory.createStatementFromText(text, null); synchronizedStatement = (PsiSynchronizedStatement)codeStyleManager.reformat(synchronizedStatement); - synchronizedStatement = (PsiSynchronizedStatement)container.addAfter(synchronizedStatement, statements[statements.length - 1]); + synchronizedStatement = (PsiSynchronizedStatement)addAfter(synchronizedStatement, container, statements); PsiCodeBlock synchronizedBlock = synchronizedStatement.getBody(); if (synchronizedBlock == null) { return null; } SurroundWithUtil.indentCommentIfNecessary(synchronizedBlock, statements); - synchronizedBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(synchronizedBlock, container, statements, true); container.deleteChildRange(statements[0], statements[statements.length - 1]); synchronizedStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(synchronizedStatement); diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java index 471bf4694315..52f923f6f1b2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java @@ -72,11 +72,11 @@ public class JavaWithTryCatchSurrounder extends JavaStatementsSurrounder { PsiTryStatement tryStatement = (PsiTryStatement)factory.createStatementFromText(text, null); tryStatement = (PsiTryStatement)CodeStyleManager.getInstance(project).reformat(tryStatement); - tryStatement = (PsiTryStatement)container.addAfter(tryStatement, statements[statements.length - 1]); + tryStatement = (PsiTryStatement)addAfter(tryStatement, container, statements); PsiCodeBlock tryBlock = tryStatement.getTryBlock(); SurroundWithUtil.indentCommentIfNecessary(tryBlock, statements); - tryBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(tryBlock, container, statements, true); PsiCatchSection[] catchSections = tryStatement.getCatchSections(); diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java index 08dceb7cc6ee..33731fdd0b59 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java @@ -49,14 +49,14 @@ public class JavaWithTryFinallySurrounder extends JavaStatementsSurrounder{ PsiTryStatement tryStatement = (PsiTryStatement)factory.createStatementFromText(text, null); tryStatement = (PsiTryStatement)codeStyleManager.reformat(tryStatement); - tryStatement = (PsiTryStatement)container.addAfter(tryStatement, statements[statements.length - 1]); + tryStatement = (PsiTryStatement)addAfter(tryStatement, container, statements); PsiCodeBlock tryBlock = tryStatement.getTryBlock(); if (tryBlock == null) { return null; } SurroundWithUtil.indentCommentIfNecessary(tryBlock, statements); - tryBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(tryBlock, container, statements, true); container.deleteChildRange(statements[0], statements[statements.length - 1]); PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock(); diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithWhileSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithWhileSurrounder.java index cddeb5760131..1058cc31bda8 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithWhileSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithWhileSurrounder.java @@ -46,7 +46,7 @@ public class JavaWithWhileSurrounder extends JavaStatementsSurrounder{ PsiWhileStatement whileStatement = (PsiWhileStatement)factory.createStatementFromText(text, null); whileStatement = (PsiWhileStatement)codeStyleManager.reformat(whileStatement); - whileStatement = (PsiWhileStatement)container.addAfter(whileStatement, statements[statements.length - 1]); + whileStatement = (PsiWhileStatement)addAfter(whileStatement, container, statements); PsiStatement body = whileStatement.getBody(); if (!(body instanceof PsiBlockStatement)) { @@ -54,7 +54,7 @@ public class JavaWithWhileSurrounder extends JavaStatementsSurrounder{ } PsiCodeBlock bodyBlock = ((PsiBlockStatement)body).getCodeBlock(); SurroundWithUtil.indentCommentIfNecessary(bodyBlock, statements); - bodyBlock.addRange(statements[0], statements[statements.length - 1]); + addRangeWithinContainer(bodyBlock, container, statements, false); container.deleteChildRange(statements[0], statements[statements.length - 1]); PsiExpression condition = whileStatement.getCondition(); diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseBlockWithIf.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseBlockWithIf.java new file mode 100644 index 000000000000..d106a9eef3c3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseBlockWithIf.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { break "a"; } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseBlockWithIf_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseBlockWithIf_after.java new file mode 100644 index 000000000000..0fafb81c7da2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseBlockWithIf_after.java @@ -0,0 +1,12 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { + if () { + break "a"; + } + } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithIf.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithIf.java new file mode 100644 index 000000000000..f84e9dc7c340 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithIf.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> "a".substring(1); + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithIf_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithIf_after.java new file mode 100644 index 000000000000..3bdf88ec18ed --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithIf_after.java @@ -0,0 +1,12 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { + if () { + break "a".substring(1); + } + } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithSynchronized.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithSynchronized.java new file mode 100644 index 000000000000..2e4a2b57cc88 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithSynchronized.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> /*0*//*1*/"a" +/*2*/ "b"/*3*/;/*4*/ + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithSynchronized_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithSynchronized_after.java new file mode 100644 index 000000000000..0a98e5d51452 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseResultWithSynchronized_after.java @@ -0,0 +1,13 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> /*0*/{ + synchronized () { + /*1*/ + "a" +/*2*/ "b"/*3*/; + } + }/*4*/ + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithBlock.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithBlock.java new file mode 100644 index 000000000000..85c03bbbf57b --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithBlock.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> throw new RuntimeException(); + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithBlock_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithBlock_after.java new file mode 100644 index 000000000000..992d39ae197f --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithBlock_after.java @@ -0,0 +1,10 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { + throw new RuntimeException(); + } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithIf.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithIf.java new file mode 100644 index 000000000000..85c03bbbf57b --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithIf.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> throw new RuntimeException(); + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithIf_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithIf_after.java new file mode 100644 index 000000000000..1cf05b91ac62 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithIf_after.java @@ -0,0 +1,12 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { + if () { + throw new RuntimeException(); + } + } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithTryCatch.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithTryCatch.java new file mode 100644 index 000000000000..68bade42e560 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithTryCatch.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> /*0*//*1*/throw new /*2*/RuntimeException("a")/*3*/;/*4*/ + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithTryCatch_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithTryCatch_after.java new file mode 100644 index 000000000000..01a8b4f3beec --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CaseThrowWithTryCatch_after.java @@ -0,0 +1,15 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> /*0*/{ + try { + /*1*/ + throw new /*2*/RuntimeException("a")/*3*/;/*4*/ + } catch (RuntimeException e) { + e.printStackTrace(); + } + } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchBlockWithFor.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchBlockWithFor.java new file mode 100644 index 000000000000..d106a9eef3c3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchBlockWithFor.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { break "a"; } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchBlockWithFor_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchBlockWithFor_after.java new file mode 100644 index 000000000000..9f0747b006be --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchBlockWithFor_after.java @@ -0,0 +1,12 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { + for () { + break "a"; + } + } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchResultWithFor.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchResultWithFor.java new file mode 100644 index 000000000000..f84e9dc7c340 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchResultWithFor.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> "a".substring(1); + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchResultWithFor_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchResultWithFor_after.java new file mode 100644 index 000000000000..6c48e922d720 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/CatchResultWithFor_after.java @@ -0,0 +1,12 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> { + for () { + "a".substring(1); + } + } + default -> ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithDoWhile.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithDoWhile.java new file mode 100644 index 000000000000..f597c6bca22c --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithDoWhile.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> { break "a".substring(1); }; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithDoWhile_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithDoWhile_after.java new file mode 100644 index 000000000000..af9a40caebab --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithDoWhile_after.java @@ -0,0 +1,12 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> { + do { + break "a".substring(1); + } while (true); + }; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithTryFinally.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithTryFinally.java new file mode 100644 index 000000000000..35e76b64438f --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithTryFinally.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> /*0*/{/*1*/break /*2*/"a";/*3*/}/*4*/ + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithTryFinally_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithTryFinally_after.java new file mode 100644 index 000000000000..80864a0147b0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultBlockWithTryFinally_after.java @@ -0,0 +1,15 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> /*0*/{ + try { + /*1*/ + break /*2*/"a";/*3*/ + } finally { + + } + }/*4*/ + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithRunnable.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithRunnable.java new file mode 100644 index 000000000000..56709301573f --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithRunnable.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> /*0*//*1*/"a" + "b"/*2*/;/*3*/ + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithRunnable_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithRunnable_after.java new file mode 100644 index 000000000000..858fe067529a --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithRunnable_after.java @@ -0,0 +1,15 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> /*0*/{ + Runnable runnable = new Runnable() { + public void run() { + /*1*/ + "a" + "b"/*2*/; + } + }; + }/*3*/ + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithTryCatchFinally.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithTryCatchFinally.java new file mode 100644 index 000000000000..1c9a1d105285 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithTryCatchFinally.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> "a" + "b";; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithTryCatchFinally_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithTryCatchFinally_after.java new file mode 100644 index 000000000000..3b114de87be4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithTryCatchFinally_after.java @@ -0,0 +1,16 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> { + try { + break "a" + "b"; + } catch (Exception e) { + e.printStackTrace(); + } finally { + } + } + ; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithWhile.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithWhile.java new file mode 100644 index 000000000000..0b85fe2e3a1d --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithWhile.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> "a" + "b"; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithWhile_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithWhile_after.java new file mode 100644 index 000000000000..075260a5b95f --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultResultWithWhile_after.java @@ -0,0 +1,12 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> { + while (true) { + "a" + "b"; + } + } + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultThrowWithIfElse.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultThrowWithIfElse.java new file mode 100644 index 000000000000..85e8e988a0cf --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultThrowWithIfElse.java @@ -0,0 +1,8 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> throw new RuntimeException("a"); + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultThrowWithIfElse_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultThrowWithIfElse_after.java new file mode 100644 index 000000000000..4e8d3bd2651a --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java12/DefaultThrowWithIfElse_after.java @@ -0,0 +1,13 @@ +class C { + void test(int n) { + String s = switch (n) { + case 1 -> ""; + default -> { + if () { + throw new RuntimeException("a"); + } else { + } + } + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWith12Test.kt b/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWith12Test.kt new file mode 100644 index 000000000000..0a2a50e0e873 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWith12Test.kt @@ -0,0 +1,51 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInsight.generation.surroundWith + +import com.intellij.codeInsight.generation.surroundWith.* +import com.intellij.java.codeInsight.folding.JavaFoldingTestCase +import com.intellij.lang.LanguageSurrounders +import com.intellij.lang.java.JavaLanguage +import com.intellij.lang.surroundWith.Surrounder +import com.intellij.psi.PsiElement +import com.intellij.testFramework.LightCodeInsightTestCase +import com.intellij.testFramework.LightPlatformCodeInsightTestCase +import com.intellij.testFramework.LightPlatformTestCase +import com.intellij.testFramework.LightProjectDescriptor +import com.intellij.util.containers.ContainerUtil + +class JavaSurroundWith12Test : LightCodeInsightTestCase() { + + private val BASE_PATH = "/codeInsight/generation/surroundWith/java12/" + + override fun getProjectDescriptor(): LightProjectDescriptor = JavaFoldingTestCase.JAVA_12 + + fun testCaseBlockWithIf() = doTest(JavaWithIfSurrounder()) + fun testCaseResultWithIf() = doTest(JavaWithIfSurrounder()) + fun testCaseThrowWithIf() = doTest(JavaWithIfSurrounder()) + fun testCaseResultWithSynchronized() = doTest(JavaWithSynchronizedSurrounder()) + fun testDefaultBlockWithTryFinally() = doTest(JavaWithTryFinallySurrounder()) + fun testCaseThrowWithTryCatch() = doTest(JavaWithTryCatchSurrounder()) + fun testDefaultResultWithTryCatchFinally() = doTest(JavaWithTryCatchFinallySurrounder()) + fun testDefaultBlockWithDoWhile() = doTest(JavaWithDoWhileSurrounder()) + fun testCaseThrowWithBlock() = doTest(JavaWithBlockSurrounder()) + fun testDefaultResultWithRunnable() = doTest(JavaWithRunnableSurrounder()) + fun testCatchBlockWithFor() = doTest(JavaWithForSurrounder()) + fun testCatchResultWithFor() = doTest(JavaWithForSurrounder()) + fun testDefaultThrowWithIfElse() = doTest(JavaWithIfElseSurrounder()) + fun testDefaultResultWithWhile() = doTest(JavaWithWhileSurrounder()) + + private fun doTest(surrounder: Surrounder) = doTest(getTestName(false), surrounder) + + private fun doTest(fileName: String, surrounder: Surrounder) { + configureByFile("$BASE_PATH$fileName.java") + + val item = ContainerUtil.getFirstItem(LanguageSurrounders.INSTANCE.allForLanguage(JavaLanguage.INSTANCE))!! + val selectionModel = getEditor().selectionModel + val elements = item.getElementsToSurround(getFile(), selectionModel.selectionStart, selectionModel.selectionEnd) + assertTrue(surrounder.isApplicable(elements)) + + SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder) + + checkResultByFile(BASE_PATH + fileName + "_after.java") + } +}