From ec5f02f0217a093808512d2915f88891ad96cb11 Mon Sep 17 00:00:00 2001 From: Sergey Simonchik Date: Mon, 8 Feb 2021 23:30:38 +0300 Subject: [PATCH] json: allow empty .jsonl files (WEB-49403) Also, allow empty .json files to make creating a new JSON file more pleasant. GitOrigin-RevId: 707361a11b025917c06758f5c058c144f18eee4f --- json/gen/com/intellij/json/JsonParser.java | 10 +++------- json/json.bnf | 3 ++- .../test/com/intellij/json/JsonHighlightingTest.java | 10 ++++++++++ json/tests/testData/highlighting/EmptyFile.json | 0 .../testData/highlighting/JsonLinesEmptyFile.jsonl | 0 5 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 json/tests/testData/highlighting/EmptyFile.json create mode 100644 json/tests/testData/highlighting/JsonLinesEmptyFile.jsonl diff --git a/json/gen/com/intellij/json/JsonParser.java b/json/gen/com/intellij/json/JsonParser.java index e67b8af540fc..9c2d1691860c 100644 --- a/json/gen/com/intellij/json/JsonParser.java +++ b/json/gen/com/intellij/json/JsonParser.java @@ -115,19 +115,15 @@ public class JsonParser implements PsiParser, LightPsiParser { } /* ********************************************************** */ - // value+ + // value* static boolean json(PsiBuilder b, int l) { if (!recursion_guard_(b, l, "json")) return false; - boolean r; - Marker m = enter_section_(b); - r = value(b, l + 1); - while (r) { + while (true) { int c = current_position_(b); if (!value(b, l + 1)) break; if (!empty_element_parsed_guard_(b, "json", c)) break; } - exit_section_(b, m, null, r); - return r; + return true; } /* ********************************************************** */ diff --git a/json/json.bnf b/json/json.bnf index 8036cf33d746..15f7e8e4b7aa 100644 --- a/json/json.bnf +++ b/json/json.bnf @@ -59,7 +59,8 @@ } // For compatibility we allow any value at root level (see JsonStandardComplianceAnnotator) -json ::= value+ +// Empty file is also allowed +json ::= value* object ::= '{' object_element* '}' { pin=1 diff --git a/json/tests/test/com/intellij/json/JsonHighlightingTest.java b/json/tests/test/com/intellij/json/JsonHighlightingTest.java index c1702d461d0b..39f31fdd9834 100644 --- a/json/tests/test/com/intellij/json/JsonHighlightingTest.java +++ b/json/tests/test/com/intellij/json/JsonHighlightingTest.java @@ -45,6 +45,16 @@ public class JsonHighlightingTest extends JsonHighlightingTestBase { doTestHighlightingForJsonLines(false, true, true); } + public void testJsonLinesEmptyFile() { + enableStandardComplianceInspection(true, true); + doTestHighlightingForJsonLines(false, true, true); + } + + public void testEmptyFile() { + enableStandardComplianceInspection(true, true); + doTestHighlighting(false, true, true); + } + public void testDuplicatePropertyKeys() { myFixture.enableInspections(JsonDuplicatePropertyKeysInspection.class); doTestHighlighting(false, true, true); diff --git a/json/tests/testData/highlighting/EmptyFile.json b/json/tests/testData/highlighting/EmptyFile.json new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/json/tests/testData/highlighting/JsonLinesEmptyFile.jsonl b/json/tests/testData/highlighting/JsonLinesEmptyFile.jsonl new file mode 100644 index 000000000000..e69de29bb2d1