Skip to content

Commit

Permalink
[JSON] Rewrite syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jrappen committed Oct 21, 2021
1 parent 258ace9 commit 7ddeece
Show file tree
Hide file tree
Showing 18 changed files with 1,203 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plist version="1.0">
<dict>
<key>scope</key>
<string>source.json</string>
<string>source.json.jsonc</string>
<key>settings</key>
<dict>
<key>shellVariables</key>
Expand Down
14 changes: 7 additions & 7 deletions JSON/Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "context":
[
{ "key": "setting.auto_match_enabled" },
{ "key": "selector", "operand": "source.json" },
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
{ "key": "selection_empty", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "['\\w]$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
Expand All @@ -14,7 +14,7 @@
{ "keys": ["\""], "command": "insert_snippet", "args": {"contents": "\"$0\""}, "context":
[
{ "key": "setting.auto_match_enabled" },
{ "key": "selector", "operand": "source.json" },
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
{ "key": "selection_empty", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\"\\w]$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
Expand All @@ -25,7 +25,7 @@
{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{$0}"}, "context":
[
{ "key": "setting.auto_match_enabled" },
{ "key": "selector", "operand": "source.json" },
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
{ "key": "selection_empty", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
]
Expand All @@ -35,7 +35,7 @@
{ "keys": ["["], "command": "insert_snippet", "args": {"contents": "[$0]"}, "context":
[
{ "key": "setting.auto_match_enabled" },
{ "key": "selector", "operand": "source.json" },
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
{ "key": "selection_empty", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |]|,|:|\\}|$)", "match_all": true }
]
Expand All @@ -45,7 +45,7 @@
{ "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
[
{ "key": "setting.auto_indent" },
{ "key": "selector", "operand": "source.json" },
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
{ "key": "selection_empty", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
Expand All @@ -54,10 +54,10 @@
{ "keys": ["shift+enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
[
{ "key": "setting.auto_indent" },
{ "key": "selector", "operand": "source.json" },
{ "key": "selector", "operand": "source.json | source.json.jsonc" },
{ "key": "selection_empty", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
]
},
]
]
File renamed without changes.
65 changes: 65 additions & 0 deletions JSON/Indentation Rules - JSONC.tmPreferences
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>scope</key>
<string>source.json.jsonc</string>
<key>settings</key>
<dict>
<key>decreaseIndentPattern</key>
<string>(?x)
# When an object is closed, but not opened
(
^
(
# Consume strings
"(?:[^"\\]|\\.)*"
|
# Consume all chars that don't start a string, comment or
# open an object on this line
[^"/{\n]
)*
\}.*$
)
|
# When an array is closed by itself on a line (interacts with indentSquareBrackets)
(
^(.*\*/)?\s*\].*$
)
</string>
<key>increaseIndentPattern</key>
<string>(?x)
# When an object is opened, but not closed
(
^.*\{
(
# Consume strings
"(?:[^"\\]|\\.)*"
|
# Consume all chars that don't start a string, comment or
# end the object that was opened on this line
[^"/}]
)*
# Stop matching at the end of the line, or once we hit a comment
($|/[/*])
)
|
# When an array is opened, but not closed
(
^.*\[
(
# Consume strings
"(?:[^"\\]|\\.)*"
|
# Consume all chars that don't start a string, comment or
# end the array that was opened on this line
[^"/\]]
)*
# Stop matching at the end of the line, or once we hit a comment
($|/[/*])
)
</string>
<key>indentSquareBrackets</key>
<true/>
</dict>
</dict>
</plist>
205 changes: 205 additions & 0 deletions JSON/JSON (Basic).sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
%YAML 1.2
---
# https://yaml.org/spec/1.2/spec.html
# https://www.sublimetext.com/docs/syntax.html
# https://www.sublimetext.com/docs/syntax.html#testing
# https://www.sublimetext.com/docs/scope_naming.html
name: JSON (Basic)
scope: source.json.basic
version: 2
hidden: true


####[ Variables ]#######################################################################################################


variables:
boolean: |-
(?x: # ignore whitespace
false|
true
)
'null': |-
(?x: # ignore whitespace
null
)
####[ Contexts ]########################################################################################################


contexts:

main:
- include: any

any:
- include: 'null'
- include: boolean
- include: numbers
- include: strings
- include: sequence
- include: mapping

####[ Null ]############################################################################################################

'null':
- match: \b({{null}})\b
captures:
1: constant.language.null.json

####[ Boolean ]#########################################################################################################

boolean:
- match: \b({{boolean}})\b
captures:
1: constant.language.boolean.json

####[ Numbers ]#########################################################################################################

numbers:
- include: decimal-float
- include: decimal-integer

decimal-float:
- match: |-
(?x: # ignore whitespace
([-]?) # capture group 1: optional sign
( # capture group 2: numeric value
(?:0|[1-9]\d*) # zero or any other (positive) decimal integer
(?:
(?:(\.)\d+)(?:[eE][-+]?\d+)?| # ... and either a period, one or more number characters, an optional exponent
(?:[eE][-+]?\d+) # ... or an exponent
)
)
)
scope: meta.number.float.decimal.json
captures:
1: keyword.operator.arithmetic.json
2: constant.numeric.value.json
3: punctuation.separator.decimal.json
decimal-integer:
- match: |-
(?x: # ignore whitespace
([-]?) # capture group 1: optional sign
(0|[1-9]\d*) # capture group 2: zero or any other (positive) decimal integer
)
scope: meta.number.integer.decimal.json
captures:
1: keyword.operator.arithmetic.json
2: constant.numeric.value.json
####[ Strings ]#########################################################################################################

strings:
- include: double-quoted-string

double-quoted-string:
- match: '"'
scope: punctuation.definition.string.begin.json
push: inside-double-quoted-string

inside-double-quoted-string:
- meta_scope: string.quoted.double.json
- match: '"'
scope: punctuation.definition.string.end.json
pop: 1
- include: double-quoted-string-escape
- match: |-
(?x: # ignore whitespace
$\n? # eol (end of line) optionally followed by newline
)
scope: invalid.illegal.unclosed-string.json
pop: 1
double-quoted-string-escape:
- match: |-
(?x: # ignore whitespace
(\\\")|
(\\\\)|
(\\\/)|
(\\b)|
(\\f)|
(\\n)|
(\\r)|
(\\t)|
(\\u[0-9a-fA-F]{4}) # backslash, lower u, exactly four hexadecimal characters
)
captures:
1: constant.character.escape.double-quote.json
2: constant.character.escape.back-slash.json
3: constant.character.escape.forward-slash.json
4: constant.character.escape.backspace.json
5: constant.character.escape.form-feed.json
6: constant.character.escape.newline.json # linefeed
7: constant.character.escape.carriage-return.json
8: constant.character.escape.horizontal-tab.json
9: constant.character.escape.unicode-symbol.json
- match: \\.
scope: invalid.illegal.unrecognized-string-escape.json

####[ Sequence ]########################################################################################################

sequence:
- match: \[
scope: punctuation.section.sequence.begin.json
push: inside-sequence

inside-sequence:
- meta_scope: meta.sequence.json
- match: \]
scope: punctuation.section.sequence.end.json
pop: 1
- include: any
- match: ','
scope: punctuation.separator.sequence.json
- match: '[^\s\]]'
scope: invalid.illegal.expected-sequence-separator.json

####[ Mapping ]#########################################################################################################

mapping:
- match: \{
scope: punctuation.section.mapping.begin.json
push: inside-mapping

inside-mapping:
- meta_scope: meta.mapping.json
- match: \}
scope: punctuation.section.mapping.end.json
pop: 1
- match: '"'
scope: punctuation.definition.string.begin.json
push: inside-double-quoted-string-inside-mapping
- include: latter-part-of-key-value-pair-inside-mapping
- match: '[^\s\}]'
scope: invalid.illegal.expected-mapping-key.json

inside-double-quoted-string-inside-mapping:
- clear_scopes: 1
- meta_scope: meta.mapping.key.json string.quoted.double.json
- include: inside-double-quoted-string

latter-part-of-key-value-pair-inside-mapping:
- match: ':'
scope: punctuation.separator.mapping.key-value.json
push:
- match: ',|\s?(?=\})'
scope: invalid.illegal.expected-mapping-value.json
pop: 1
- match: (?=\S)
set:
- clear_scopes: 1
- meta_scope: meta.mapping.value.json
- include: any
- match: ''
set:
- match: ','
scope: punctuation.separator.mapping.pair.json
pop: 1
- match: \s*(?=\})
pop: 1
- match: \s(?!/[/*])(?=[^\s,])|[^\s,]
scope: invalid.illegal.expected-mapping-separator.json
pop: 1
Loading

0 comments on commit 7ddeece

Please sign in to comment.