WEB-40927 Editor is breaking JSON syntax when inserting new line after key-value pair

GitOrigin-RevId: a4c3c12a0b3ac07b2ffa2bbc4972768fc0f82bcf
This commit is contained in:
Anton Lobov
2019-08-28 08:02:56 +00:00
committed by intellij-monorepo-bot
parent a367440722
commit f60afaffd3
2 changed files with 12 additions and 3 deletions
@@ -59,7 +59,9 @@ public class JsonEnterHandler extends EnterHandlerDelegateAdapter {
private static boolean handleComma(@NotNull Ref<Integer> caretOffsetRef, @NotNull PsiElement psiAtOffset, @NotNull Editor editor) {
PsiElement nextSibling = psiAtOffset;
boolean hasNewlineBefore = false;
while (nextSibling instanceof PsiWhiteSpace) {
hasNewlineBefore = nextSibling.getText().contains("\n");
nextSibling = nextSibling.getNextSibling();
}
@@ -74,7 +76,7 @@ public class JsonEnterHandler extends EnterHandlerDelegateAdapter {
if (prevSibling instanceof JsonProperty && ((JsonProperty)prevSibling).getValue() != null) {
int offset = elementType == JsonElementTypes.COMMA ? nextSibling.getTextRange().getEndOffset() : prevSibling.getTextRange().getEndOffset();
if (offset < editor.getDocument().getTextLength()) {
if (elementType == JsonElementTypes.R_CURLY) {
if (elementType == JsonElementTypes.R_CURLY && hasNewlineBefore) {
editor.getDocument().insertString(offset, ",");
offset++;
}
@@ -43,7 +43,7 @@ public class JsonTypingHandlingTest extends JsonTestCase {
// JsonEnterHandler
public void testEnterAfterProperty() {
doTestEnter("{\"a\": true<caret>}", "{\"a\": true,\n}");
doTestEnter("{\"a\": true<caret>\n}", "{\"a\": true,\n <caret>\n}");
}
public void testEnterMidProperty() {
doTestEnter("{\"a\": tr<caret>ue}", "{\"a\": true,\n}");
@@ -55,7 +55,7 @@ public class JsonTypingHandlingTest extends JsonTestCase {
doTestEnter("{\"a\": {<caret>} \"b\": 5}", "{\"a\": {\n \n}, \"b\": 5}");
}
public void testEnterAfterObject() {
doTestEnter("{\"a\": {}<caret>}", "{\"a\": {},\n}");
doTestEnter("{\"a\": {}<caret>\n}", "{\"a\": {},\n <caret>\n}");
}
// JsonTypedHandler
@@ -158,4 +158,11 @@ public class JsonTypingHandlingTest extends JsonTestCase {
public void testAddCommaWithPairedQuotes() {
testWithPairQuotes(true, () -> doTypingTest("\"ccc\": \"", "{<caret>\"aaa\": \"bbb\"}", "{\"ccc\": \"<caret>\",\"aaa\": \"bbb\"}", "json"));
}
public void testNoCommaIfRBraceAndNoNewline() {
doTestEnter("{\n" +
" \"x\": 5<caret>}\n", "{\n" +
" \"x\": 5\n" +
"}\n");
}
}