[java-highlighting] IDEA-324445 offer when after TypePattern

GitOrigin-RevId: 5ef95cd17be1a7c76e82438d84f46f96b515be2f
This commit is contained in:
Mikhail Pyltsin
2023-07-06 11:52:57 +02:00
committed by intellij-monorepo-bot
parent 06a30a413b
commit 13774e83ac
6 changed files with 95 additions and 4 deletions

View File

@@ -267,7 +267,7 @@ public class JavaKeywordCompletion {
}
addFinal();
addWhen();
boolean statementPosition = isStatementPosition(myPosition);
if (statementPosition) {
addCaseDefault();
@@ -317,6 +317,35 @@ public class JavaKeywordCompletion {
addCaseNullToSwitch();
}
private void addWhen() {
LanguageLevel level = PsiUtil.getLanguageLevel(myPosition);
if (!(level == LanguageLevel.JDK_20_PREVIEW || level.isAtLeast(LanguageLevel.JDK_21))) {
return;
}
PsiElement element = PsiTreeUtil.skipWhitespacesAndCommentsForward(myPrevLeaf);
if (element instanceof PsiErrorElement) {
return;
}
element = PsiTreeUtil.skipWhitespacesAndCommentsBackward(PsiTreeUtil.prevLeaf(myPosition));
if (element instanceof PsiErrorElement) {
return;
}
PsiPattern psiPattern = PsiTreeUtil.getParentOfType(myPrevLeaf, PsiPattern.class, true, PsiStatement.class, PsiMember.class, PsiClass.class);
if (psiPattern == null ||
(psiPattern instanceof PsiTypeTestPattern testPattern &&
testPattern.getPatternVariable() != null &&
testPattern.getPatternVariable().getNameIdentifier() == myPosition)) {
return;
}
PsiElement parentOfPattern = PsiTreeUtil.skipParentsOfType(psiPattern, PsiPattern.class, PsiDeconstructionList.class);
if (!(parentOfPattern instanceof PsiCaseLabelElementList)) {
return;
}
addKeyword(new OverridableSpace(createKeyword(PsiKeyword.WHEN), TailType.INSERT_SPACE));
}
private void addSwitchRuleKeywords(@NotNull PsiSwitchLabeledRuleStatement rule) {
addKeyword(new OverridableSpace(createKeyword(PsiKeyword.THROW), TailType.INSERT_SPACE));
addKeyword(wrapRuleIntoBlock(new OverridableSpace(createKeyword(PsiKeyword.ASSERT), TailType.INSERT_SPACE)));

View File

@@ -0,0 +1,10 @@
class Main {
record I(int x)
void f(Object o) {
switch (o) {
case Record(int x) <caret>
}
}
}

View File

@@ -0,0 +1,10 @@
class Main {
record I(int x)
void f(Object o) {
switch (o) {
case Record(int x <caret>
}
}
}

View File

@@ -0,0 +1,8 @@
class Main {
void f(Object o) {
switch (o) {
case String <caret>
}
}
}

View File

@@ -0,0 +1,8 @@
class Main {
void f(Object o) {
switch (o) {
case String s <caret>
}
}
}

View File

@@ -23,13 +23,36 @@ public class NormalSwitchCompletionVariantsTest extends LightFixtureCompletionTe
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_20;
return JAVA_21;
}
public void testCompletionPrimitiveTypeExpr() { doTest(COMMON_VARIANTS); }
public void testCompletionPrimitiveTypeStmt() { doTest(COMMON_VARIANTS); }
public void testCompletionVariantsInStmt() { doTest(COMMON_OBJECT_VARIANTS); }
public void testCompletionVariantsInExpr() { doTest(COMMON_OBJECT_VARIANTS); }
public void testCompletionWhenAfterTypeTest() {
List<String> lookup = doTestAndGetLookup();
assertContainsElements(lookup, "when");
}
public void testCompletionWhenAfterDeconstruction() {
List<String> lookup = doTestAndGetLookup();
assertContainsElements(lookup, "when");
}
public void testCompletionWhenAfterPartDeconstruction() {
List<String> lookup = doTestAndGetLookup();
if (lookup != null) {
assertDoesntContain(lookup, "when");
}
//if null - it is ok
}
public void testCompletionWhenAfterPartTypeTest() {
List<String> lookup = doTestAndGetLookup();
if (lookup != null) {
assertDoesntContain(lookup, "when");
}
//if null - it is ok
}
@NeedsIndex.Full
public void testCompletionSealedHierarchyStmt() {
@@ -42,10 +65,13 @@ public class NormalSwitchCompletionVariantsTest extends LightFixtureCompletionTe
}
private void doTest(String[] variants) {
final List<String> lookupElementStrings =doTestAndGetLookup();
assertSameElements(lookupElementStrings, variants);
}
private List<String> doTestAndGetLookup() {
myFixture.configureByFile(getTestName(false) + ".java");
myFixture.complete(CompletionType.BASIC);
final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
assertSameElements(lookupElementStrings, variants);
return myFixture.getLookupElementStrings();
}
}