[java-completion] IDEA-331975 'Complete Current Statement' invoked on the 'while' keyword does not insert opening parenthesis

GitOrigin-RevId: 3d61667b9af08123dddef7f74e346473187266cd
This commit is contained in:
Tagir Valeev
2023-09-15 15:57:40 +02:00
committed by intellij-monorepo-bot
parent 291026bc68
commit ce7ea33a0a
4 changed files with 25 additions and 6 deletions

View File

@@ -54,8 +54,7 @@ public class MissingLoopBodyFixer implements Fixer {
if (BasicJavaAstTreeUtil.is(body, BASIC_BLOCK_STATEMENT)) return;
if (body != null && startLine(doc, body) == startLine(doc, loopStatement)) return;
ASTNode eltToInsertAfter = BasicJavaAstTreeUtil.getRParenth(loopStatement);
fixLoopBody(editor, processor, loopStatement, doc, body, eltToInsertAfter);
fixLoopBody(editor, processor, loopStatement, doc, body);
}
private static ASTNode getLoopParent(@NotNull ASTNode element) {
@@ -115,8 +114,8 @@ public class MissingLoopBodyFixer implements Fixer {
@NotNull AbstractBasicJavaSmartEnterProcessor processor,
@NotNull ASTNode loop,
@NotNull Document doc,
@Nullable ASTNode body,
@Nullable ASTNode eltToInsertAfter) {
@Nullable ASTNode body) {
ASTNode eltToInsertAfter = BasicJavaAstTreeUtil.getRParenth(loop);
PsiElement loopElement = BasicJavaAstTreeUtil.toPsi(loop);
if (body != null && eltToInsertAfter != null) {
PsiElement bodyElement = BasicJavaAstTreeUtil.toPsi(body);
@@ -139,8 +138,13 @@ public class MissingLoopBodyFixer implements Fixer {
}
int offset = eltToInsertAfter.getTextRange().getEndOffset();
if (needToClose) {
doc.insertString(offset, ")");
offset++;
if (BasicJavaAstTreeUtil.getLParenth(loop) == null) {
doc.insertString(offset, "()");
offset += 2;
} else {
doc.insertString(offset, ")");
offset++;
}
}
processor.insertBraces(editor, offset);
editor.getCaretModel().moveToOffset(offset);

View File

@@ -205,6 +205,8 @@ public abstract class AbstractBasicCompleteStatementTest extends LightPlatformCo
public void testParenthesized() { doTest(); }
public void testCompleteBreak() { doTest(); }
public void testNakedWhile() { doTest(); }
public void testCompleteIfNextLineBraceStyle() {
myJavaSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;

View File

@@ -0,0 +1,6 @@
class Foo {
{
while<caret>
}
}

View File

@@ -0,0 +1,7 @@
class Foo {
{
while (<caret>) {
}
}
}