From a5d749d8ea2bb19ab73281c33c091266b0ba9e63 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Tue, 26 Sep 2023 13:34:23 +0200 Subject: [PATCH] [java-completions] IDEA-332716 IJ-CR-115136 SmartEnter invoked on the 'switch' expression does not move caret inside - added formatting for switch expression -simplify MissingSwitchBodyFixer GitOrigin-RevId: 4c77e10ce5dd83d9c4f5d08c0c9fe94eeca0d411 --- .../AbstractBasicJavaSmartEnterProcessor.java | 3 +++ .../smartEnter/MissingSwitchBodyFixer.java | 22 +++---------------- .../ReturnSwitchExpression2_after.java | 2 +- .../ReturnSwitchExpression_after.java | 2 +- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/AbstractBasicJavaSmartEnterProcessor.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/AbstractBasicJavaSmartEnterProcessor.java index f6906893a0b1..471e1139c5a5 100644 --- a/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/AbstractBasicJavaSmartEnterProcessor.java +++ b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/AbstractBasicJavaSmartEnterProcessor.java @@ -371,6 +371,9 @@ public abstract class AbstractBasicJavaSmartEnterProcessor extends SmartEnterPro BasicJavaAstTreeUtil.is(block.getTreeParent().getTreeParent(), BASIC_FOR_STATEMENT)) { reformat(block.getTreeParent().getTreeParent().getPsi()); } + if (block != null && BasicJavaAstTreeUtil.is(block.getTreeParent(), BASIC_SWITCH_EXPRESSION)) { + reformat(block.getTreeParent().getPsi()); + } } public void registerUnresolvedError(int offset) { diff --git a/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingSwitchBodyFixer.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingSwitchBodyFixer.java index ce1122349692..9a862a204d57 100644 --- a/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingSwitchBodyFixer.java +++ b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingSwitchBodyFixer.java @@ -4,10 +4,6 @@ package com.intellij.codeInsight.editorActions.smartEnter; import com.intellij.lang.ASTNode; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; -import com.intellij.psi.SmartPointerManager; -import com.intellij.psi.SmartPsiElementPointer; import com.intellij.psi.impl.source.BasicJavaAstTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; @@ -17,7 +13,8 @@ import static com.intellij.psi.impl.source.BasicJavaElementType.BASIC_SWITCH_STA public class MissingSwitchBodyFixer implements Fixer { @Override - public void apply(Editor editor, AbstractBasicJavaSmartEnterProcessor processor, @NotNull ASTNode astNode) throws IncorrectOperationException { + public void apply(Editor editor, AbstractBasicJavaSmartEnterProcessor processor, @NotNull ASTNode astNode) + throws IncorrectOperationException { if (!(BasicJavaAstTreeUtil.is(astNode, BASIC_SWITCH_EXPRESSION, BASIC_SWITCH_STATEMENT))) return; final ASTNode body = BasicJavaAstTreeUtil.getCodeBlock(astNode); @@ -28,19 +25,6 @@ public class MissingSwitchBodyFixer implements Fixer { int offset = rParenth.getTextRange().getEndOffset(); processor.insertBracesWithNewLine(editor, offset); - if (BasicJavaAstTreeUtil.is(astNode, BASIC_SWITCH_EXPRESSION)) { - Project project = editor.getProject(); - PsiElement psiElement = BasicJavaAstTreeUtil.toPsi(astNode); - if (project != null && psiElement != null) { - SmartPsiElementPointer pointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(psiElement); - processor.commit(editor); - PsiElement reparsedSwitch = pointer.getElement(); - if (reparsedSwitch != null) { - editor.getCaretModel().moveToOffset(offset + 1); - processor.reformat(editor, pointer.getElement()); - processor.setSkipEnter(true); - } - } - } + editor.getCaretModel().moveToOffset(offset); } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression2_after.java b/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression2_after.java index 09791798e55e..c546cdcadb9a 100644 --- a/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression2_after.java +++ b/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression2_after.java @@ -2,6 +2,6 @@ class Test { int aMethod(int i) { return switch (i) { - }; + } } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression_after.java b/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression_after.java index 09791798e55e..c546cdcadb9a 100644 --- a/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression_after.java +++ b/java/java-tests/testData/codeInsight/completeStatement/ReturnSwitchExpression_after.java @@ -2,6 +2,6 @@ class Test { int aMethod(int i) { return switch (i) { - }; + } } } \ No newline at end of file