JavaEnterInTextBlockHandler: commit document on enter only for java files, do not add semicolon after text block (IDEA-CR-53311)

GitOrigin-RevId: 256c7400b9a22bad92833b4576a30b6075676694
This commit is contained in:
Artemiy Sartakov
2019-10-10 12:03:40 +00:00
committed by intellij-monorepo-bot
parent 3ad31d8a27
commit 524e6f6664
7 changed files with 1 additions and 42 deletions
@@ -19,6 +19,7 @@ public class JavaEnterInTextBlockHandler extends EnterInStringLiteralHandler {
@Override
public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
if (!(file instanceof PsiJavaFile)) return super.postProcessEnter(file, editor, dataContext);
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
PsiLiteralExpressionImpl textBlock = getTextBlock(file, editor.getCaretModel().getOffset());
if (textBlock == null) return super.postProcessEnter(file, editor, dataContext);
@@ -83,7 +84,6 @@ public class JavaEnterInTextBlockHandler extends EnterInStringLiteralHandler {
@Nullable
private static PsiLiteralExpressionImpl getTextBlock(@NotNull PsiFile file, int offset) {
if (!(file instanceof PsiJavaFile)) return null;
PsiJavaToken token = ObjectUtils.tryCast(file.findElementAt(offset), PsiJavaToken.class);
if (token == null || token.getTokenType() != JavaTokenType.TEXT_BLOCK_LITERAL) return null;
return ObjectUtils.tryCast(token.getParent(), PsiLiteralExpressionImpl.class);
@@ -7,10 +7,8 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -116,20 +114,6 @@ public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements Java
return super.hasNonClosedLiteral(editor, iterator, offset);
}
@Override
public boolean needSemicolonAfter(@NotNull PsiElement element) {
PsiLiteralExpressionImpl literal = ObjectUtils.tryCast(element.getParent(), PsiLiteralExpressionImpl.class);
if (literal == null || literal.getLiteralElementType() != JavaTokenType.TEXT_BLOCK_LITERAL) return false;
PsiJavaToken token = ObjectUtils.tryCast(literal.getNextSibling(), PsiJavaToken.class);
if (token != null && token.getTokenType() == JavaTokenType.SEMICOLON) return false;
String text = literal.getText();
if (text.indexOf('\n') != -1) return false;
PsiVariable variable = ObjectUtils.tryCast(literal.getParent(), PsiVariable.class);
if (variable != null && variable.getInitializer() == literal) return true;
PsiAssignmentExpression assignment = ObjectUtils.tryCast(literal.getParent(), PsiAssignmentExpression.class);
return assignment != null && assignment.getRExpression() == literal;
}
@Override
public void insertClosingQuote(@NotNull Editor editor, int offset, @NotNull PsiFile file, @NotNull CharSequence closingQuote) {
editor.getDocument().insertString(offset, "\"\"\"");
@@ -1,7 +0,0 @@
class Test {
void test() {
String block = """<caret>"""
}
}
@@ -1,8 +0,0 @@
class Test {
void test() {
String block = """
<caret>""";
}
}
@@ -47,10 +47,6 @@ public class JavaEnterInStringLiteralTest extends LightJavaCodeInsightTestCase {
doTest();
}
public void testNoSemicolon() {
doTest();
}
public void testEnter() {
doTest();
}
@@ -24,7 +24,4 @@ public interface JavaLikeQuoteHandler extends QuoteHandler {
boolean needParenthesesAroundConcatenation(PsiElement element);
default boolean needSemicolonAfter(@NotNull PsiElement element) {
return false;
}
}
@@ -83,9 +83,6 @@ public class EnterInStringLiteralHandler extends EnterHandlerDelegateAdapter {
caretAdvanceRef.set(caretAdvance);
return Result.DefaultForceIndent;
}
if (quoteHandler.needSemicolonAfter(psiAtOffset)) {
document.insertString(psiAtOffset.getTextRange().getEndOffset(), ";");
}
}
return Result.Continue;
}