[java-completion] Complete statement: use arrow instead of colon in rule switch (IDEA-309553)

GitOrigin-RevId: 74a10e28de1dfb1667bb4a29688ab4c710b73bf4
This commit is contained in:
Tagir Valeev
2023-01-11 15:56:33 +01:00
committed by intellij-monorepo-bot
parent 5263b8ca9e
commit 01db701bec
6 changed files with 42 additions and 5 deletions

View File

@@ -18,17 +18,23 @@ package com.intellij.codeInsight.editorActions.smartEnter;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiCaseLabelElementList;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiSwitchBlock;
import com.intellij.psi.PsiSwitchLabelStatement;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.SwitchUtils;
public class SwitchLabelColonFixer implements Fixer {
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
if (psiElement instanceof PsiSwitchLabelStatement && !psiElement.getText().endsWith(":")) {
PsiSwitchLabelStatement statement = (PsiSwitchLabelStatement)psiElement;
PsiCaseLabelElementList labelElementList = statement.getCaseLabelElementList();
if ((labelElementList != null && labelElementList.getElementCount() != 0) || statement.isDefaultCase()) {
editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), ":");
if (psiElement instanceof PsiSwitchLabelStatement statement) {
PsiSwitchBlock block = statement.getEnclosingSwitchBlock();
if (block == null) return;
String token = SwitchUtils.isRuleFormatSwitch(block) ? "->" : ":";
if (!psiElement.getText().endsWith(token)) {
PsiCaseLabelElementList labelElementList = statement.getCaseLabelElementList();
if ((labelElementList != null && labelElementList.getElementCount() != 0) || statement.isDefaultCase()) {
editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), token);
}
}
}
}

View File

@@ -2,6 +2,7 @@
class Foo {
void foo(String a) {
switch (a) {
case "x":break;
case "asdf<caret>"
}
}

View File

@@ -2,6 +2,7 @@
class Foo {
void foo(String a) {
switch (a) {
case "x":break;
case "asdf":
<caret>
}

View File

@@ -0,0 +1,13 @@
class X {
sealed interface I0 {}
sealed interface I1 extends I0 {}
sealed interface I2 extends I1 {}
record A() implements I2 {}
record B() implements I0 {}
static void main(I0 i1) {
switch (i1) {
case A a -> System.out.println("A");
case B b<caret>
}
}
}

View File

@@ -0,0 +1,15 @@
class X {
sealed interface I0 {}
sealed interface I1 extends I0 {}
sealed interface I2 extends I1 {}
record A() implements I2 {}
record B() implements I0 {}
static void main(I0 i1) {
switch (i1) {
case A a -> System.out.println("A");
case B b -> {
<caret>
}
}
}
}

View File

@@ -218,6 +218,7 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testAddMissingLambdaBody() { doTest(); }
public void testAddMissingLambdaBody2() { doTest(); }
public void testBlockInSwitchRule() { doTest(); }
public void testSwitchAddArrow() { doTest(); }
private void doTestBracesNextLineStyle() {
myJavaSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;