mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-completion] IDEA-305420 Complete statement does not work for switch expressions
GitOrigin-RevId: 0cd50d0ef1168b9bf637c8dcb0972f27d7ce2156
This commit is contained in:
committed by
intellij-monorepo-bot
parent
4d1c5305ab
commit
b0b19ea5b5
+13
-7
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user