mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
WEB-43566 JSON: incorrect syntax after adding a single quote
GitOrigin-RevId: 46ca8af81c76fb562377070a8e001cb0bffe043e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
e1d5c85239
commit
b37bf2f554
@@ -4,8 +4,13 @@ package com.intellij.json;
|
||||
import com.intellij.codeInsight.editorActions.MultiCharQuoteHandler;
|
||||
import com.intellij.codeInsight.editorActions.SimpleTokenSetQuoteHandler;
|
||||
import com.intellij.json.editor.JsonTypedHandler;
|
||||
import com.intellij.json.psi.JsonStringLiteral;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.TokenType;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
@@ -35,6 +40,17 @@ public class JsonQuoteHandler extends SimpleTokenSetQuoteHandler implements Mult
|
||||
|
||||
@Override
|
||||
public void insertClosingQuote(@NotNull Editor editor, int offset, @NotNull PsiFile file, @NotNull CharSequence closingQuote) {
|
||||
PsiElement element = file.findElementAt(offset - 1);
|
||||
PsiElement parent = element == null ? null : element.getParent();
|
||||
if (parent instanceof JsonStringLiteral) {
|
||||
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
|
||||
TextRange range = parent.getTextRange();
|
||||
if (offset - 1 != range.getStartOffset() || !"\"".contentEquals(closingQuote)) {
|
||||
int endOffset = range.getEndOffset();
|
||||
if (offset < endOffset) return;
|
||||
if (offset == endOffset && !StringUtil.isEmpty(((JsonStringLiteral)parent).getValue())) return;
|
||||
}
|
||||
}
|
||||
editor.getDocument().insertString(offset, closingQuote);
|
||||
JsonTypedHandler.processPairedBracesComma(closingQuote.charAt(0), editor, file);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,9 @@ public class JsonTypingHandlingTest extends JsonTestCase {
|
||||
private void doTestQuote(@NotNull final String before, @NotNull final String expected) {
|
||||
doTypingTest('"', before, expected, "json");
|
||||
}
|
||||
private void doTestSingleQuote(@NotNull final String before, @NotNull final String expected) {
|
||||
doTypingTest('\'', before, expected, "json");
|
||||
}
|
||||
private void doTestColon(@NotNull final String before, @NotNull final String expected) {
|
||||
doTypingTest(':', before, expected, "json");
|
||||
}
|
||||
@@ -265,4 +268,12 @@ public class JsonTypingHandlingTest extends JsonTestCase {
|
||||
editorOptions.COMMA_MOVE_OUTSIDE_QUOTES = oldQuote;
|
||||
}
|
||||
}
|
||||
|
||||
public void testQuotePairingNotInsideString01() {
|
||||
doTestQuote("{\"MyKey\": \"This \\<caret>\"}", "{\"MyKey\": \"This \\\"<caret>\"}");
|
||||
}
|
||||
|
||||
public void testQuotePairingNotInsideString02() {
|
||||
doTestSingleQuote("{\"MyKey\": \"This <caret> \\\"is\\\" my value\"}", "{\"MyKey\": \"This ' \\\"is\\\" my value\"}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user