From 0ee0388653fbafeea5b4675f6a84e91d30c49295 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 18 Feb 2021 11:15:32 +0700 Subject: [PATCH] [java-impl] IDEA-262315 Complete current statement doesn't work for switch expression GitOrigin-RevId: c755b2eb3a30a8011ea665f13effc7224594818b --- .../smartEnter/JavaSmartEnterProcessor.java | 12 +++++++----- .../SemicolonAfterSwitchExpression.java | 9 +++++++++ .../SemicolonAfterSwitchExpression_after.java | 9 +++++++++ .../java/codeInsight/CompleteStatementTest.java | 1 + 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression.java create mode 100644 java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java index 1275aa949d5f..61c96a2ab5c7 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java @@ -281,11 +281,13 @@ public class JavaSmartEnterProcessor extends SmartEnterProcessor { if (atCaret instanceof PsiWhiteSpace) return null; if (atCaret instanceof PsiJavaToken && "}".equals(atCaret.getText())) { atCaret = atCaret.getParent(); - if (!(atCaret instanceof PsiAnonymousClass || - atCaret instanceof PsiArrayInitializerExpression || - psiElement(PsiCodeBlock.class).withParent(PsiLambdaExpression.class).accepts(atCaret))) { - return null; - } + boolean expressionEndingWithBrace = atCaret instanceof PsiAnonymousClass || + atCaret instanceof PsiArrayInitializerExpression || + atCaret instanceof PsiCodeBlock && ( + atCaret.getParent() instanceof PsiLambdaExpression || + atCaret.getParent() instanceof PsiSwitchExpression + ); + if (!expressionEndingWithBrace) return null; } for (PsiElement each : SyntaxTraverser.psiApi().parents(atCaret).skip(1)) { diff --git a/java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression.java b/java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression.java new file mode 100644 index 000000000000..8ee2a808d88d --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression.java @@ -0,0 +1,9 @@ +public class SemicolonAfterSwitchExpression { + void test() { + int x = switch(0) { + case 0 -> 1; + case 1 -> 2; + default -> 3; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression_after.java b/java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression_after.java new file mode 100644 index 000000000000..71b2ad0b5cbb --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/SemicolonAfterSwitchExpression_after.java @@ -0,0 +1,9 @@ +public class SemicolonAfterSwitchExpression { + void test() { + int x = switch (0) { + case 0 -> 1; + case 1 -> 2; + default -> 3; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/CompleteStatementTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/CompleteStatementTest.java index 479c8e0e0098..270e1acbe383 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/CompleteStatementTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/CompleteStatementTest.java @@ -182,6 +182,7 @@ public class CompleteStatementTest extends EditorActionTestCase { public void testRecordWithComponent() { doTest(); } public void testRecordWithComponentNoBody() { doTest(); } public void testVarargMethod() { doTest(); } + public void testSemicolonAfterSwitchExpression() { doTest(); } private void doTestBracesNextLineStyle() { myJavaSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;