[java-rd] IDEA-322563 Improve editing experience in Remote Dev for Java

- fix while completion

GitOrigin-RevId: 40827f77037557eab988d33d8170bd4446b8478a
This commit is contained in:
Mikhail Pyltsin
2023-09-18 16:13:41 +02:00
committed by intellij-monorepo-bot
parent de67570ff3
commit 76d10b31f3
6 changed files with 17 additions and 76 deletions

View File

@@ -553,7 +553,7 @@ public final class BasicJavaAstTreeUtil {
@Nullable
public static ASTNode getWhileCondition(@Nullable ASTNode element) {
if (!is(element, BASIC_DO_WHILE_STATEMENT) ||
if (!is(element, BASIC_DO_WHILE_STATEMENT) &&
!is(element, BASIC_WHILE_STATEMENT)) {
return null;
}

View File

@@ -412,6 +412,8 @@ public abstract class AbstractBasicCompleteStatementTest extends LightPlatformCo
public void testYieldSemicolon() { doTest(); }
public void testCommentSmartEnter() { doTest(false); }
public void testWhileStatementWithCondition() { doTest(); }
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return new BasicDefaultLightProjectDescriptor();

View File

@@ -1,34 +0,0 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.editorActions.smartEnter;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiThrowStatement;
import com.intellij.psi.impl.source.BasicJavaAstTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public class MissingThrowExpressionFixer implements Fixer {
@Override
public void apply(Editor editor, AbstractBasicJavaSmartEnterProcessor processor, @NotNull ASTNode astNode)
throws IncorrectOperationException {
PsiElement psiElement = BasicJavaAstTreeUtil.toPsi(astNode);
if (psiElement instanceof PsiThrowStatement throwStatement) {
if (throwStatement.getException() != null &&
startLine(editor, throwStatement) == startLine(editor, throwStatement.getException())) {
return;
}
final int startOffset = throwStatement.getTextRange().getStartOffset();
if (throwStatement.getException() != null) {
editor.getDocument().insertString(startOffset + "throw".length(), ";");
}
processor.registerUnresolvedError(startOffset + "throw".length());
}
}
private static int startLine(Editor editor, PsiElement psiElement) {
return editor.getDocument().getLineNumber(psiElement.getTextRange().getStartOffset());
}
}

View File

@@ -1,41 +0,0 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.editorActions.smartEnter;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.BasicJavaAstTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public class WhileConditionFixer implements Fixer {
@Override
public void apply(Editor editor, AbstractBasicJavaSmartEnterProcessor processor, @NotNull ASTNode astNode) throws IncorrectOperationException {
PsiElement psiElement = BasicJavaAstTreeUtil.toPsi(astNode);
if (psiElement instanceof PsiWhileStatement whileStatement) {
final Document doc = editor.getDocument();
final PsiJavaToken rParenth = whileStatement.getRParenth();
final PsiJavaToken lParenth = whileStatement.getLParenth();
final PsiExpression condition = whileStatement.getCondition();
if (condition == null) {
if (lParenth == null || rParenth == null) {
int stopOffset = doc.getLineEndOffset(doc.getLineNumber(whileStatement.getTextRange().getStartOffset()));
final PsiStatement block = whileStatement.getBody();
if (block != null) {
stopOffset = Math.min(stopOffset, block.getTextRange().getStartOffset());
}
stopOffset = Math.min(stopOffset, whileStatement.getTextRange().getEndOffset());
doc.replaceString(whileStatement.getTextRange().getStartOffset(), stopOffset, "while ()");
processor.registerUnresolvedError(whileStatement.getTextRange().getStartOffset() + "while (".length());
} else {
processor.registerUnresolvedError(lParenth.getTextRange().getEndOffset());
}
} else if (rParenth == null) {
doc.insertString(condition.getTextRange().getEndOffset(), ")");
}
}
}
}

View File

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

View File

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