[java-completion] IDEA-305420 Complete statement does not work for switch expressions

GitOrigin-RevId: 0cd50d0ef1168b9bf637c8dcb0972f27d7ce2156
This commit is contained in:
Tagir Valeev
2022-12-02 17:29:19 +00:00
committed by intellij-monorepo-bot
parent 4d1c5305ab
commit b0b19ea5b5
4 changed files with 30 additions and 7 deletions
@@ -18,26 +18,32 @@ package com.intellij.codeInsight.editorActions.smartEnter;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLambdaExpression;
import com.intellij.psi.PsiSwitchLabeledRuleStatement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nullable;
public class MissingLambdaBodyFixer implements Fixer {
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
if (!(psiElement instanceof PsiLambdaExpression lambda)) return;
PsiElement body = lambda.getBody();
PsiElement body;
if (psiElement instanceof PsiLambdaExpression lambda) {
body = lambda.getBody();
} else if (psiElement instanceof PsiSwitchLabeledRuleStatement rule) {
body = rule.getBody();
} else return;
if (body != null) return;
Document doc = editor.getDocument();
PsiElement arrow = PsiTreeUtil.getDeepestVisibleLast(lambda);
PsiElement arrow = PsiTreeUtil.getDeepestVisibleLast(psiElement);
if (!PsiUtil.isJavaToken(arrow, JavaTokenType.ARROW)) return;
int offset = arrow.getTextRange().getEndOffset();
doc.insertString(offset, "{\n}");
editor.getCaretModel().moveToOffset(offset + 1);
processor.commit(editor);
processor.reformat(lambda);
processor.setSkipEnter(true);
processor.reformat(psiElement);
processor.setSkipEnter(psiElement instanceof PsiLambdaExpression);
}
}
@@ -0,0 +1,7 @@
public class A {
public static void main(String[] args) {
switch (args[0]) {
case String s -> <caret>
}
}
}
@@ -0,0 +1,9 @@
public class A {
public static void main(String[] args) {
switch (args[0]) {
case String s -> {
<caret>
}
}
}
}
@@ -217,6 +217,7 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testSwitchAtTheEndOfClass() { doTest(); }
public void testAddMissingLambdaBody() { doTest(); }
public void testAddMissingLambdaBody2() { doTest(); }
public void testBlockInSwitchRule() { doTest(); }
private void doTestBracesNextLineStyle() {
myJavaSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;