Skip to content

Commit 90ad838

Browse files
committed
Allow highlight comments in json with jsonc-mode
1 parent 0e819e5 commit 90ad838

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

json-mode.el

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ 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"
@@ -105,6 +108,10 @@ This function calls `json-mode--update-auto-mode' to change the
105108
(defconst json-mode-number-re (rx (group (one-or-more digit)
106109
(optional ?\. (one-or-more digit)))))
107110
(defconst json-mode-keyword-re (rx (group (or "true" "false" "null"))))
111+
(defconst json-mode-line-comments-re
112+
(rx (and bol (0+ space) (group (and "//" (*? nonl) eol)))))
113+
(defconst json-mode-block-comments-re
114+
(rx (group (and "/*" (*? anything) "*/"))))
108115

109116
(defconst json-font-lock-keywords-1
110117
(list
@@ -113,13 +120,28 @@ This function calls `json-mode--update-auto-mode' to change the
113120
(list json-mode-keyword-re 1 font-lock-constant-face)
114121
(list json-mode-number-re 1 font-lock-constant-face)
115122
)
116-
"Level one font lock.")
123+
"Level one font lock for `json-mode'.")
124+
125+
(defconst jsonc-font-lock-keywords-1
126+
(list
127+
(list json-mode-line-comments-re 1 font-lock-comment-face)
128+
(list json-mode-block-comments-re 1 font-lock-comment-face)
129+
(list json-mode-quoted-key-re 1 font-lock-keyword-face)
130+
(list json-mode-quoted-string-re 1 font-lock-string-face)
131+
(list json-mode-keyword-re 1 font-lock-constant-face)
132+
(list json-mode-number-re 1 font-lock-constant-face))
133+
"Level one font lock for `jsonc-mode'.")
117134

118135
;;;###autoload
119136
(define-derived-mode json-mode javascript-mode "JSON"
120137
"Major mode for editing JSON files"
121138
(set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
122139

140+
;;;###autoload
141+
(define-derived-mode jsonc-mode json-mode "JSONC"
142+
"Major mode for editing JSON files with comments"
143+
(set (make-local-variable 'font-lock-defaults) '(jsonc-font-lock-keywords-1 t)))
144+
123145
;; Well formatted JSON files almost always begin with “{” or “[”.
124146
;;;###autoload
125147
(add-to-list 'magic-fallback-mode-alist '("^[{[]$" . json-mode))

0 commit comments

Comments
 (0)