Skip to content

Commit 30c186c

Browse files
authored
Merge pull request #61 from kiennq/feat/jsonc-mode
Allow highlight comments in json with jsonc-mode
2 parents 0e819e5 + 7e26021 commit 30c186c

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

json-mode.el

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ Return the new `auto-mode-alist' entry"
6060
(add-to-list 'auto-mode-alist new-entry)
6161
new-entry))
6262

63+
;;; make byte-compiler happy
64+
(defvar json-mode--auto-mode-entry)
65+
6366
;;;###autoload
6467
(defcustom json-mode-auto-mode-list '(
6568
".babelrc"
6669
".bowerrc"
6770
"composer.lock"
6871
)
69-
"List of filename as string to pass for the JSON entry of
70-
`auto-mode-alist'.
72+
"List of filename as string to pass for the JSON entry of `auto-mode-alist'.
7173
7274
Note however that custom `json-mode' entries in `auto-mode-alist'
7375
won’t be affected."
@@ -108,17 +110,59 @@ This function calls `json-mode--update-auto-mode' to change the
108110

109111
(defconst json-font-lock-keywords-1
110112
(list
111-
(list json-mode-quoted-key-re 1 font-lock-keyword-face)
112-
(list json-mode-quoted-string-re 1 font-lock-string-face)
113113
(list json-mode-keyword-re 1 font-lock-constant-face)
114-
(list json-mode-number-re 1 font-lock-constant-face)
115-
)
114+
(list json-mode-number-re 1 font-lock-constant-face))
116115
"Level one font lock.")
117116

117+
(defvar json-mode-syntax-table
118+
(let ((st (make-syntax-table)))
119+
;; Objects
120+
(modify-syntax-entry ?\{ "(}" st)
121+
(modify-syntax-entry ?\} "){" st)
122+
;; Arrays
123+
(modify-syntax-entry ?\[ "(]" st)
124+
(modify-syntax-entry ?\] ")[" st)
125+
;; Strings
126+
(modify-syntax-entry ?\" "\"" st)
127+
st))
128+
129+
(defvar jsonc-mode-syntax-table
130+
(let ((st (copy-syntax-table json-mode-syntax-table)))
131+
;; Comments
132+
(modify-syntax-entry ?/ ". 124" st)
133+
(modify-syntax-entry ?\n ">" st)
134+
(modify-syntax-entry ?\^m ">" st)
135+
(modify-syntax-entry ?* ". 23bn" st)
136+
st))
137+
138+
(defun json-mode--syntactic-face (state)
139+
"Return syntactic face function for the position represented by STATE.
140+
STATE is a `parse-partial-sexp' state, and the returned function is the
141+
json font lock syntactic face function."
142+
(cond
143+
((nth 3 state)
144+
;; This might be a string or a name
145+
(let ((startpos (nth 8 state)))
146+
(save-excursion
147+
(goto-char startpos)
148+
(if (looking-at-p json-mode-quoted-key-re)
149+
font-lock-keyword-face
150+
font-lock-string-face))))
151+
((nth 4 state) font-lock-comment-face)))
152+
118153
;;;###autoload
119154
(define-derived-mode json-mode javascript-mode "JSON"
120155
"Major mode for editing JSON files"
121-
(set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
156+
:syntax-table json-mode-syntax-table
157+
(set (make-local-variable 'font-lock-defaults)
158+
'(json-font-lock-keywords-1
159+
nil nil nil nil
160+
(font-lock-syntactic-face-function . json-mode--syntactic-face))))
161+
162+
;;;###autoload
163+
(define-derived-mode jsonc-mode json-mode "JSONC"
164+
"Major mode for editing JSON files with comments"
165+
:syntax-table jsonc-mode-syntax-table)
122166

123167
;; Well formatted JSON files almost always begin with “{” or “[”.
124168
;;;###autoload

0 commit comments

Comments
 (0)