mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
[java-completion] MissingArrayInitializerBraceFixer: use EnterAfterUnmatchedBraceHandler (IDEA-282469)
GitOrigin-RevId: 29a477bf9a7b726ef2d56c7e0ad7f82c8621d8b7
This commit is contained in:
committed by
intellij-monorepo-bot
parent
6775b54e18
commit
2d19324846
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.editorActions.smartEnter;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.enter.EnterAfterUnmatchedBraceHandler;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
@@ -26,10 +27,21 @@ public class MissingArrayInitializerBraceFixer implements Fixer {
|
||||
@Override
|
||||
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
if (!(psiElement instanceof PsiArrayInitializerExpression || psiElement instanceof PsiArrayInitializerMemberValue)) return;
|
||||
PsiElement deepestVisibleLast = PsiTreeUtil.getDeepestVisibleLast(psiElement);
|
||||
if (PsiUtil.isJavaToken(deepestVisibleLast, JavaTokenType.RBRACE) && deepestVisibleLast.getParent() == psiElement) return;
|
||||
PsiErrorElement err = ContainerUtil.findInstance(psiElement.getChildren(), PsiErrorElement.class);
|
||||
int endOffset = (err != null ? err : psiElement).getTextRange().getEndOffset();
|
||||
PsiElement child = psiElement.getFirstChild();
|
||||
if (!PsiUtil.isJavaToken(child, JavaTokenType.LBRACE)) return;
|
||||
if (!EnterAfterUnmatchedBraceHandler.isAfterUnmatchedLBrace(editor, child.getTextRange().getEndOffset(),
|
||||
psiElement.getContainingFile().getFileType())) return;
|
||||
PsiElement anchor = ContainerUtil.findInstance(psiElement.getChildren(), PsiErrorElement.class);
|
||||
if (anchor == null) {
|
||||
PsiElement last = PsiTreeUtil.getDeepestVisibleLast(psiElement);
|
||||
while (PsiUtil.isJavaToken(last, JavaTokenType.RBRACE)) {
|
||||
last = PsiTreeUtil.prevCodeLeaf(last);
|
||||
}
|
||||
if (last != null && PsiTreeUtil.isAncestor(psiElement, last, true)) {
|
||||
anchor = last;
|
||||
}
|
||||
}
|
||||
int endOffset = (anchor != null ? anchor : psiElement).getTextRange().getEndOffset();
|
||||
editor.getDocument().insertString(endOffset, "}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public class Foo {
|
||||
public void fails() {
|
||||
final String ORIGIN = "";
|
||||
final String[] expected = {ORIGIN<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public class Foo {
|
||||
public void fails() {
|
||||
final String ORIGIN = "";
|
||||
final String[] expected = {ORIGIN};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
String[][] foo = {{"a", "b", "c"<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
String[][] foo = {{"a", "b", "c"}};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
switch(args[0]) {<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
switch(args[0]) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,7 +166,9 @@ public class CompleteStatementTest extends EditorActionTestCase {
|
||||
public void testAddMethodBodyFromInsideAnnotation() { doTest(); }
|
||||
public void testArrayInitializerRBracket() { doTest(); }
|
||||
public void testArrayInitializerRBrace() { doTest(); }
|
||||
public void testArrayInitializerRBrace2() { doTest(); }
|
||||
public void testMultiArrayInitializerRBrace() { doTest(); }
|
||||
public void testMultiArrayInitializerRBrace2() { doTest(); }
|
||||
public void testArrayInitializerSeveralLines() { doTest(); }
|
||||
public void testReturnInLambda() { doTest(); }
|
||||
public void testSemicolonAfterLambda() { doTest(); }
|
||||
@@ -202,6 +204,7 @@ public class CompleteStatementTest extends EditorActionTestCase {
|
||||
public void testReturnSwitchExpression3() {
|
||||
doTest();
|
||||
}
|
||||
public void testSwitchAtTheEndOfClass() { doTest(); }
|
||||
|
||||
private void doTestBracesNextLineStyle() {
|
||||
myJavaSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
|
||||
|
||||
Reference in New Issue
Block a user