JavaQuoteHandler: moved language level check for text blocks to hasNonClosedLiteral method (IDEA-CR-55318)

GitOrigin-RevId: d892b81916d12636c67118fdf240b661577c9bbb
This commit is contained in:
Artemiy Sartakov
2019-11-19 07:11:28 +00:00
committed by intellij-monorepo-bot
parent a81e4db6c6
commit 88fbc6522e
@@ -102,17 +102,18 @@ public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements Java
@Override
public boolean hasNonClosedLiteral(Editor editor, HighlighterIterator iterator, int offset) {
if (iterator.getTokenType() == JavaTokenType.TEXT_BLOCK_LITERAL) {
Document document = iterator.getDocument();
if (document != null) {
String text = document.getText();
boolean hasOpenQuotes = StringUtil.equals(text.substring(iterator.getStart(), offset + 1), "\"\"\"");
if (hasOpenQuotes) {
boolean hasCloseQuotes = StringUtil.contains(text.substring(offset + 1, iterator.getEnd()), "\"\"\"");
if (!hasCloseQuotes) return true;
// check if parser interpreted next text block start quotes as end quotes for the current one
int nTextBlockQuotes = StringUtil.getOccurrenceCount(text.substring(iterator.getEnd()), "\"\"\"");
return nTextBlockQuotes % 2 != 0;
}
Document document = editor.getDocument();
Project project = editor.getProject();
PsiFile file = project == null ? null : PsiDocumentManager.getInstance(project).getPsiFile(document);
if (file == null || !HighlightUtil.Feature.TEXT_BLOCKS.isAvailable(file)) return false;
String text = document.getText();
boolean hasOpenQuotes = StringUtil.equals(text.substring(iterator.getStart(), offset + 1), "\"\"\"");
if (hasOpenQuotes) {
boolean hasCloseQuotes = StringUtil.contains(text.substring(offset + 1, iterator.getEnd()), "\"\"\"");
if (!hasCloseQuotes) return true;
// check if parser interpreted next text block start quotes as end quotes for the current one
int nTextBlockQuotes = StringUtil.getOccurrenceCount(text.substring(iterator.getEnd()), "\"\"\"");
return nTextBlockQuotes % 2 != 0;
}
}
return super.hasNonClosedLiteral(editor, iterator, offset);
@@ -120,7 +121,6 @@ public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements Java
@Override
public void insertClosingQuote(@NotNull Editor editor, int offset, @NotNull PsiFile file, @NotNull CharSequence closingQuote) {
if (!HighlightUtil.Feature.TEXT_BLOCKS.isAvailable(file)) return;
editor.getDocument().insertString(offset, "\n\"\"\"");
Project project = file.getProject();
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());