Skip to content

Commit 7ddeece

Browse files
committed
[JSON] Rewrite syntax
1 parent 258ace9 commit 7ddeece

18 files changed

+1203
-288
lines changed

JSON/Comments.tmPreferences renamed to JSON/Comments - JSONC.tmPreferences

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<plist version="1.0">
33
<dict>
44
<key>scope</key>
5-
<string>source.json</string>
5+
<string>source.json.jsonc</string>
66
<key>settings</key>
77
<dict>
88
<key>shellVariables</key>

JSON/Default.sublime-keymap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "context":
44
[
55
{ "key": "setting.auto_match_enabled" },
6-
{ "key": "selector", "operand": "source.json" },
6+
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
77
{ "key": "selection_empty", "match_all": true },
88
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "['\\w]$", "match_all": true },
99
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
@@ -14,7 +14,7 @@
1414
{ "keys": ["\""], "command": "insert_snippet", "args": {"contents": "\"$0\""}, "context":
1515
[
1616
{ "key": "setting.auto_match_enabled" },
17-
{ "key": "selector", "operand": "source.json" },
17+
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
1818
{ "key": "selection_empty", "match_all": true },
1919
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\"\\w]$", "match_all": true },
2020
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
@@ -25,7 +25,7 @@
2525
{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{$0}"}, "context":
2626
[
2727
{ "key": "setting.auto_match_enabled" },
28-
{ "key": "selector", "operand": "source.json" },
28+
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
2929
{ "key": "selection_empty", "match_all": true },
3030
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
3131
]
@@ -35,7 +35,7 @@
3535
{ "keys": ["["], "command": "insert_snippet", "args": {"contents": "[$0]"}, "context":
3636
[
3737
{ "key": "setting.auto_match_enabled" },
38-
{ "key": "selector", "operand": "source.json" },
38+
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
3939
{ "key": "selection_empty", "match_all": true },
4040
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
4141
]
@@ -45,7 +45,7 @@
4545
{ "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
4646
[
4747
{ "key": "setting.auto_indent" },
48-
{ "key": "selector", "operand": "source.json" },
48+
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
4949
{ "key": "selection_empty", "match_all": true },
5050
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
5151
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
@@ -54,10 +54,10 @@
5454
{ "keys": ["shift+enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
5555
[
5656
{ "key": "setting.auto_indent" },
57-
{ "key": "selector", "operand": "source.json" },
57+
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
5858
{ "key": "selection_empty", "match_all": true },
5959
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
6060
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
6161
]
6262
},
63-
]
63+
]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<plist version="1.0">
3+
<dict>
4+
<key>scope</key>
5+
<string>source.json.jsonc</string>
6+
<key>settings</key>
7+
<dict>
8+
<key>decreaseIndentPattern</key>
9+
<string>(?x)
10+
# When an object is closed, but not opened
11+
(
12+
^
13+
(
14+
# Consume strings
15+
"(?:[^"\\]|\\.)*"
16+
|
17+
# Consume all chars that don't start a string, comment or
18+
# open an object on this line
19+
[^"/{\n]
20+
)*
21+
\}.*$
22+
)
23+
|
24+
# When an array is closed by itself on a line (interacts with indentSquareBrackets)
25+
(
26+
^(.*\*/)?\s*\].*$
27+
)
28+
</string>
29+
<key>increaseIndentPattern</key>
30+
<string>(?x)
31+
# When an object is opened, but not closed
32+
(
33+
^.*\{
34+
(
35+
# Consume strings
36+
"(?:[^"\\]|\\.)*"
37+
|
38+
# Consume all chars that don't start a string, comment or
39+
# end the object that was opened on this line
40+
[^"/}]
41+
)*
42+
# Stop matching at the end of the line, or once we hit a comment
43+
($|/[/*])
44+
)
45+
|
46+
# When an array is opened, but not closed
47+
(
48+
^.*\[
49+
(
50+
# Consume strings
51+
"(?:[^"\\]|\\.)*"
52+
|
53+
# Consume all chars that don't start a string, comment or
54+
# end the array that was opened on this line
55+
[^"/\]]
56+
)*
57+
# Stop matching at the end of the line, or once we hit a comment
58+
($|/[/*])
59+
)
60+
</string>
61+
<key>indentSquareBrackets</key>
62+
<true/>
63+
</dict>
64+
</dict>
65+
</plist>

JSON/JSON (Basic).sublime-syntax

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
%YAML 1.2
2+
---
3+
# https://yaml.org/spec/1.2/spec.html
4+
# https://www.sublimetext.com/docs/syntax.html
5+
# https://www.sublimetext.com/docs/syntax.html#testing
6+
# https://www.sublimetext.com/docs/scope_naming.html
7+
name: JSON (Basic)
8+
scope: source.json.basic
9+
version: 2
10+
hidden: true
11+
12+
13+
####[ Variables ]#######################################################################################################
14+
15+
16+
variables:
17+
boolean: |-
18+
(?x: # ignore whitespace
19+
false|
20+
true
21+
)
22+
'null': |-
23+
(?x: # ignore whitespace
24+
null
25+
)
26+
27+
28+
####[ Contexts ]########################################################################################################
29+
30+
31+
contexts:
32+
33+
main:
34+
- include: any
35+
36+
any:
37+
- include: 'null'
38+
- include: boolean
39+
- include: numbers
40+
- include: strings
41+
- include: sequence
42+
- include: mapping
43+
44+
####[ Null ]############################################################################################################
45+
46+
'null':
47+
- match: \b({{null}})\b
48+
captures:
49+
1: constant.language.null.json
50+
51+
####[ Boolean ]#########################################################################################################
52+
53+
boolean:
54+
- match: \b({{boolean}})\b
55+
captures:
56+
1: constant.language.boolean.json
57+
58+
####[ Numbers ]#########################################################################################################
59+
60+
numbers:
61+
- include: decimal-float
62+
- include: decimal-integer
63+
64+
decimal-float:
65+
- match: |-
66+
(?x: # ignore whitespace
67+
([-]?) # capture group 1: optional sign
68+
( # capture group 2: numeric value
69+
(?:0|[1-9]\d*) # zero or any other (positive) decimal integer
70+
(?:
71+
(?:(\.)\d+)(?:[eE][-+]?\d+)?| # ... and either a period, one or more number characters, an optional exponent
72+
(?:[eE][-+]?\d+) # ... or an exponent
73+
)
74+
)
75+
)
76+
scope: meta.number.float.decimal.json
77+
captures:
78+
1: keyword.operator.arithmetic.json
79+
2: constant.numeric.value.json
80+
3: punctuation.separator.decimal.json
81+
82+
decimal-integer:
83+
- match: |-
84+
(?x: # ignore whitespace
85+
([-]?) # capture group 1: optional sign
86+
(0|[1-9]\d*) # capture group 2: zero or any other (positive) decimal integer
87+
)
88+
scope: meta.number.integer.decimal.json
89+
captures:
90+
1: keyword.operator.arithmetic.json
91+
2: constant.numeric.value.json
92+
93+
####[ Strings ]#########################################################################################################
94+
95+
strings:
96+
- include: double-quoted-string
97+
98+
double-quoted-string:
99+
- match: '"'
100+
scope: punctuation.definition.string.begin.json
101+
push: inside-double-quoted-string
102+
103+
inside-double-quoted-string:
104+
- meta_scope: string.quoted.double.json
105+
- match: '"'
106+
scope: punctuation.definition.string.end.json
107+
pop: 1
108+
- include: double-quoted-string-escape
109+
- match: |-
110+
(?x: # ignore whitespace
111+
$\n? # eol (end of line) optionally followed by newline
112+
)
113+
scope: invalid.illegal.unclosed-string.json
114+
pop: 1
115+
116+
double-quoted-string-escape:
117+
- match: |-
118+
(?x: # ignore whitespace
119+
(\\\")|
120+
(\\\\)|
121+
(\\\/)|
122+
(\\b)|
123+
(\\f)|
124+
(\\n)|
125+
(\\r)|
126+
(\\t)|
127+
(\\u[0-9a-fA-F]{4}) # backslash, lower u, exactly four hexadecimal characters
128+
)
129+
captures:
130+
1: constant.character.escape.double-quote.json
131+
2: constant.character.escape.back-slash.json
132+
3: constant.character.escape.forward-slash.json
133+
4: constant.character.escape.backspace.json
134+
5: constant.character.escape.form-feed.json
135+
6: constant.character.escape.newline.json # linefeed
136+
7: constant.character.escape.carriage-return.json
137+
8: constant.character.escape.horizontal-tab.json
138+
9: constant.character.escape.unicode-symbol.json
139+
- match: \\.
140+
scope: invalid.illegal.unrecognized-string-escape.json
141+
142+
####[ Sequence ]########################################################################################################
143+
144+
sequence:
145+
- match: \[
146+
scope: punctuation.section.sequence.begin.json
147+
push: inside-sequence
148+
149+
inside-sequence:
150+
- meta_scope: meta.sequence.json
151+
- match: \]
152+
scope: punctuation.section.sequence.end.json
153+
pop: 1
154+
- include: any
155+
- match: ','
156+
scope: punctuation.separator.sequence.json
157+
- match: '[^\s\]]'
158+
scope: invalid.illegal.expected-sequence-separator.json
159+
160+
####[ Mapping ]#########################################################################################################
161+
162+
mapping:
163+
- match: \{
164+
scope: punctuation.section.mapping.begin.json
165+
push: inside-mapping
166+
167+
inside-mapping:
168+
- meta_scope: meta.mapping.json
169+
- match: \}
170+
scope: punctuation.section.mapping.end.json
171+
pop: 1
172+
- match: '"'
173+
scope: punctuation.definition.string.begin.json
174+
push: inside-double-quoted-string-inside-mapping
175+
- include: latter-part-of-key-value-pair-inside-mapping
176+
- match: '[^\s\}]'
177+
scope: invalid.illegal.expected-mapping-key.json
178+
179+
inside-double-quoted-string-inside-mapping:
180+
- clear_scopes: 1
181+
- meta_scope: meta.mapping.key.json string.quoted.double.json
182+
- include: inside-double-quoted-string
183+
184+
latter-part-of-key-value-pair-inside-mapping:
185+
- match: ':'
186+
scope: punctuation.separator.mapping.key-value.json
187+
push:
188+
- match: ',|\s?(?=\})'
189+
scope: invalid.illegal.expected-mapping-value.json
190+
pop: 1
191+
- match: (?=\S)
192+
set:
193+
- clear_scopes: 1
194+
- meta_scope: meta.mapping.value.json
195+
- include: any
196+
- match: ''
197+
set:
198+
- match: ','
199+
scope: punctuation.separator.mapping.pair.json
200+
pop: 1
201+
- match: \s*(?=\})
202+
pop: 1
203+
- match: \s(?!/[/*])(?=[^\s,])|[^\s,]
204+
scope: invalid.illegal.expected-mapping-separator.json
205+
pop: 1

0 commit comments

Comments
 (0)