IDEA-155309 Complete statement does not work for `default` in `switch`
authorpeter <peter@jetbrains.com>
Sun, 1 May 2016 14:37:31 +0000 (16:37 +0200)
committerpeter <peter@jetbrains.com>
Mon, 2 May 2016 06:58:54 +0000 (08:58 +0200)
java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java
java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java [moved from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/CaseColonFixer.java with 71% similarity]
java/java-tests/testData/codeInsight/completeStatement/DefaultColon.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/completeStatement/DefaultColon_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java

index 9fd7ed8c5222408a76316e5b8b910954e0f1369f..42e45c19bda259e3205b01e9f1a5d9ea65dda822 100644 (file)
@@ -74,7 +74,7 @@ public class JavaSmartEnterProcessor extends SmartEnterProcessor {
     fixers.add(new WhileConditionFixer());
     fixers.add(new CatchDeclarationFixer());
     fixers.add(new SwitchExpressionFixer());
-    fixers.add(new CaseColonFixer());
+    fixers.add(new SwitchLabelColonFixer());
     fixers.add(new DoWhileConditionFixer());
     fixers.add(new BlockBraceFixer());
     fixers.add(new MissingIfBranchesFixer());
similarity index 71%
rename from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/CaseColonFixer.java
rename to java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java
index de32027c4616aac047d107bb5e815397e456d928..939a7c051222dc15039c9f0182138b79b7a29171 100644 (file)
@@ -23,13 +23,14 @@ import com.intellij.util.IncorrectOperationException;
 /**
  * @author peter
  */
-public class CaseColonFixer implements Fixer {
+public class SwitchLabelColonFixer implements Fixer {
   @Override
   public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
-    if (psiElement instanceof PsiSwitchLabelStatement &&
-        ((PsiSwitchLabelStatement)psiElement).getCaseValue() != null &&
-        !psiElement.getText().endsWith(":")) {
-      editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), ":");
+    if (psiElement instanceof PsiSwitchLabelStatement && !psiElement.getText().endsWith(":")) {
+      PsiSwitchLabelStatement statement = (PsiSwitchLabelStatement)psiElement;
+      if (statement.getCaseValue() != null || statement.isDefaultCase()) {
+        editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), ":");
+      }
     }
   }
 }
diff --git a/java/java-tests/testData/codeInsight/completeStatement/DefaultColon.java b/java/java-tests/testData/codeInsight/completeStatement/DefaultColon.java
new file mode 100644 (file)
index 0000000..c1e4e58
--- /dev/null
@@ -0,0 +1,8 @@
+
+class Foo {
+    void foo(String a) {
+        switch (a) {
+            default<caret>
+        }
+    }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/completeStatement/DefaultColon_after.java b/java/java-tests/testData/codeInsight/completeStatement/DefaultColon_after.java
new file mode 100644 (file)
index 0000000..3e898b1
--- /dev/null
@@ -0,0 +1,9 @@
+
+class Foo {
+    void foo(String a) {
+        switch (a) {
+            default:
+                <caret>
+        }
+    }
+}
\ No newline at end of file
index 00efb8bb80cb81a69997bfdee269123975a2b8b3..576e3e00a1a4b027e47282477b2dcd3db8ff17bb 100644 (file)
@@ -226,6 +226,7 @@ public class CompleteStatementTest extends EditorActionTestCase {
   public void testSwitchKeywordWithCondition() throws Exception { doTest(); }
   public void testSwitchBraces() { doTest(); }
   public void testCaseColon() { doTest(); }
+  public void testDefaultColon() { doTest(); }
 
   public void testNewInParentheses() throws Exception { doTest(); }