diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java index 0cc0c90d10b9..b4ef03182d71 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java @@ -769,15 +769,7 @@ public final class JavaCompletionContributor extends CompletionContributor imple } boolean inSwitchLabel = IN_SWITCH_LABEL.accepts(position); - TailType forcedTail = null; - if (!smart) { - if (inSwitchLabel) { - forcedTail = TailTypes.forSwitchLabel(Objects.requireNonNull(PsiTreeUtil.getParentOfType(position, PsiSwitchBlock.class))); - } - else if (shouldInsertSemicolon(position)) { - forcedTail = TailType.SEMICOLON; - } - } + TailType forcedTail = getTailType(smart, inSwitchLabel, position); List items = new ArrayList<>(); if (INSIDE_CONSTRUCTOR.accepts(position) && @@ -805,7 +797,15 @@ public final class JavaCompletionContributor extends CompletionContributor imple LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY); if (forcedTail != null && !(element instanceof JavaPsiClassReferenceElement)) { - element = TailTypeDecorator.withTail(element, forcedTail); + element = new TailTypeDecorator<>(element) { + @Override + protected TailType computeTailType(InsertionContext context) { + if (context.getCompletionChar() == ':' && forcedTail == TailTypes.CASE_ARROW) { + return TailType.CASE_COLON; + } + return forcedTail; + } + }; } if (inSwitchLabel && !smart) { @@ -837,6 +837,17 @@ public final class JavaCompletionContributor extends CompletionContributor imple return items; } + @Nullable + private static TailType getTailType(boolean smart, boolean inSwitchLabel, PsiElement position) { + if (!smart && inSwitchLabel) { + return TailTypes.forSwitchLabel(Objects.requireNonNull(PsiTreeUtil.getParentOfType(position, PsiSwitchBlock.class))); + } + if (!smart && shouldInsertSemicolon(position)) { + return TailType.SEMICOLON; + } + return null; + } + private static Collection getInnerScopeVariables(CompletionParameters parameters, PsiElement position) { PsiElement container = BringVariableIntoScopeFix.getContainer(position); if (container == null) return Collections.emptyList(); diff --git a/java/java-tests/testData/codeInsight/completion/normal/StaticallyImportedFieldsTwiceSwitch_after.java b/java/java-tests/testData/codeInsight/completion/normal/StaticallyImportedFieldsTwiceSwitch_after.java index 11831a43106a..aeefb60b9de3 100644 --- a/java/java-tests/testData/codeInsight/completion/normal/StaticallyImportedFieldsTwiceSwitch_after.java +++ b/java/java-tests/testData/codeInsight/completion/normal/StaticallyImportedFieldsTwiceSwitch_after.java @@ -4,7 +4,7 @@ class C { public static void main(A a) { switch (a) { - case Abc: + case Abc -> } } diff --git a/java/java-tests/testData/codeInsight/completion/normal/SwitchCaseWithEnumConstant_after.java b/java/java-tests/testData/codeInsight/completion/normal/SwitchCaseWithEnumConstant_after.java index c6ed04eeda4e..43f8fcb29338 100644 --- a/java/java-tests/testData/codeInsight/completion/normal/SwitchCaseWithEnumConstant_after.java +++ b/java/java-tests/testData/codeInsight/completion/normal/SwitchCaseWithEnumConstant_after.java @@ -2,7 +2,7 @@ class A extends Base{ void foo(){ B x = null; switch(x){ - case GOO: + case GOO -> } } } diff --git a/java/java-tests/testData/codeInsight/completion/normal/UnboxedConstantsInCase_after.java b/java/java-tests/testData/codeInsight/completion/normal/UnboxedConstantsInCase_after.java index 31623a47b29a..522831859a4f 100644 --- a/java/java-tests/testData/codeInsight/completion/normal/UnboxedConstantsInCase_after.java +++ b/java/java-tests/testData/codeInsight/completion/normal/UnboxedConstantsInCase_after.java @@ -1,7 +1,7 @@ public class Demo { void foo(Integer i){ switch(i) { - case Types.CHAR: + case Types.CHAR -> } } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.java index 21cd658d2923..093dfb7b82c4 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.java @@ -16,16 +16,14 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.impl.NonBlockingReadActionImpl; import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; import com.intellij.openapi.util.registry.Registry; +import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.augment.PsiAugmentProvider; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.psi.codeStyle.JavaCodeStyleSettings; import com.intellij.psi.impl.light.LightMethodBuilder; import com.intellij.psi.impl.source.PsiExtensibleClass; -import com.intellij.testFramework.LightProjectDescriptor; -import com.intellij.testFramework.NeedsIndex; -import com.intellij.testFramework.PlatformTestUtil; -import com.intellij.testFramework.ServiceContainerUtil; +import com.intellij.testFramework.*; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.UIUtil; import com.siyeh.ig.style.UnqualifiedFieldAccessInspection; @@ -46,7 +44,7 @@ public class NormalCompletionTest extends NormalCompletionTestCase { @NotNull @Override protected LightProjectDescriptor getProjectDescriptor() { - return JAVA_9; + return JAVA_21; } public void testSimple() throws Exception { @@ -306,7 +304,7 @@ public class NormalCompletionTest extends NormalCompletionTestCase { public void testSwitchEnumLabel() { configureByFile("SwitchEnumLabel.java"); - assertEquals(3, myItems.length); + assertEquals("[A, B, C, null]", ContainerUtil.map(myItems, LookupElement::getLookupString).toString()); } public void testSwitchCaseWithEnumConstant() { doTest(); } @@ -976,7 +974,9 @@ public class NormalCompletionTest extends NormalCompletionTestCase { checkResult(); } - public void testCaseTailType() { doTest(); } + public void testCaseTailType() { + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_11, this::doTest); + } private void doPrimitiveTypeTest() { configure(); @@ -992,7 +992,7 @@ public class NormalCompletionTest extends NormalCompletionTestCase { @NeedsIndex.ForStandardLibrary public void testFinalInForLoop2() { configure(); - myFixture.assertPreferredCompletionItems(0, "finalize", "final"); + myFixture.assertPreferredCompletionItems(1, "finalize", "final"); } public void testOnlyClassesInExtends() { @@ -2263,7 +2263,9 @@ public class NormalCompletionTest extends NormalCompletionTestCase { checkResult(); } - public void testCaseColonAfterStringConstant() { doTest(); } + public void testCaseColonAfterStringConstant() { + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_11, this::doTest); + } public void testOneElementArray() { configureByTestName(); @@ -2678,7 +2680,8 @@ public class NormalCompletionTest extends NormalCompletionTestCase { }"""); myFixture.completeBasic(); assertEquals("return", myFixture.getLookupElements()[0].getLookupString()); - var element = myFixture.getLookupElements()[1]; + assertEquals("record", myFixture.getLookupElements()[1].getLookupString()); + var element = myFixture.getLookupElements()[2]; assertEquals("result", element.getLookupString()); LookupElementPresentation presentation = renderElement(element); assertEquals(" (from if-then block)", presentation.getTailText());