diff --git a/json/src/com/intellij/json/JsonQuoteHandler.java b/json/src/com/intellij/json/JsonQuoteHandler.java index b830b4a17831..090eefe69b31 100644 --- a/json/src/com/intellij/json/JsonQuoteHandler.java +++ b/json/src/com/intellij/json/JsonQuoteHandler.java @@ -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); } diff --git a/json/tests/test/com/intellij/json/JsonTypingHandlingTest.java b/json/tests/test/com/intellij/json/JsonTypingHandlingTest.java index cb2fdb09e683..1c2e0f862318 100644 --- a/json/tests/test/com/intellij/json/JsonTypingHandlingTest.java +++ b/json/tests/test/com/intellij/json/JsonTypingHandlingTest.java @@ -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 \\\"}", "{\"MyKey\": \"This \\\"\"}"); + } + + public void testQuotePairingNotInsideString02() { + doTestSingleQuote("{\"MyKey\": \"This \\\"is\\\" my value\"}", "{\"MyKey\": \"This ' \\\"is\\\" my value\"}"); + } }