From d72eba89a16888dca06facd87b3785f2f237a88c Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Mon, 9 Sep 2024 15:31:43 +0200 Subject: [PATCH] [java-completion] IDEA-357258 Java 23: .switch postfix completion is not supported for primitive data types GitOrigin-RevId: ae848584ee81611bde2e1201c79923c2e170f6e6 --- .../templates/SwitchStatementPostfixTemplate.java | 4 ++++ .../CompleteSwitchPrimitiveSelectorPostfix.java | 6 ++++++ ...tchPrimitiveSelectorPostfixLowerLanguageLevel.java | 6 ++++++ ...mitiveSelectorPostfixLowerLanguageLevel_after.java | 6 ++++++ .../CompleteSwitchPrimitiveSelectorPostfix_after.java | 8 ++++++++ .../completion/NormalSwitchCompletionTest.java | 11 +++++++++++ 6 files changed, 41 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel_after.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java index 084bd18d9f50..d7ae5480c4e6 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java @@ -19,6 +19,7 @@ import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; +import com.intellij.psi.util.TypeConversionUtil; import com.intellij.util.Function; import org.jetbrains.annotations.NotNull; @@ -48,6 +49,9 @@ public class SwitchStatementPostfixTemplate extends SurroundPostfixTemplateBase if (PsiUtil.isAvailable(JavaFeature.STRING_SWITCH, javaFile)) return true; } + if (PsiUtil.isAvailable(JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS, expression) && + TypeConversionUtil.isPrimitiveAndNotNull(type)) return true; + return false; }); }; diff --git a/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix.java b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix.java new file mode 100644 index 000000000000..abd8d7106976 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix.java @@ -0,0 +1,6 @@ + +class Main { + int g(long o) { + o.swit + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel.java b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel.java new file mode 100644 index 000000000000..abd8d7106976 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel.java @@ -0,0 +1,6 @@ + +class Main { + int g(long o) { + o.swit + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel_after.java b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel_after.java new file mode 100644 index 000000000000..82067a785ae0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel_after.java @@ -0,0 +1,6 @@ + +class Main { + int g(long o) { + o.swit + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix_after.java b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix_after.java new file mode 100644 index 000000000000..c2dc2d31c048 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/CompleteSwitchPrimitiveSelectorPostfix_after.java @@ -0,0 +1,8 @@ + +class Main { + int g(long o) { + switch (o) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java index e15096585f42..8f87dae958cf 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java @@ -3,6 +3,7 @@ package com.intellij.java.codeInsight.completion; import com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor; import com.intellij.openapi.application.impl.NonBlockingReadActionImpl; +import com.intellij.pom.java.JavaFeature; import com.intellij.pom.java.LanguageLevel; import com.intellij.testFramework.IdeaTestUtil; import com.intellij.testFramework.LightProjectDescriptor; @@ -69,6 +70,16 @@ public class NormalSwitchCompletionTest extends NormalCompletionTestCase { public void testCompleteSwitchObjectSelectorPostfix() { doTestPostfixCompletion(); } + public void testCompleteSwitchPrimitiveSelectorPostfix() { + IdeaTestUtil.withLevel(myFixture.getModule(), JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.getMinimumLevel(), + () -> doTestPostfixCompletion()); + } + + public void testCompleteSwitchPrimitiveSelectorPostfixLowerLanguageLevel() { + IdeaTestUtil.withLevel(myFixture.getModule(), LanguageLevel.JDK_11, + () -> doTestPostfixCompletion()); + } + public void testCompleteSwitchSealedSelectorPostfix() { doTestPostfixCompletion(); } @NeedsIndex.ForStandardLibrary