Skip to content

Allow highlight comments in json with jsonc-mode #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions json-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ Return the new `auto-mode-alist' entry"
(add-to-list 'auto-mode-alist new-entry)
new-entry))

;;; make byte-compiler happy
(defvar json-mode--auto-mode-entry)

;;;###autoload
(defcustom json-mode-auto-mode-list '(
".babelrc"
".bowerrc"
"composer.lock"
)
"List of filename as string to pass for the JSON entry of
`auto-mode-alist'.
"List of filename as string to pass for the JSON entry of `auto-mode-alist'.

Note however that custom `json-mode' entries in `auto-mode-alist'
won’t be affected."
Expand Down Expand Up @@ -108,17 +110,59 @@ This function calls `json-mode--update-auto-mode' to change the

(defconst json-font-lock-keywords-1
(list
(list json-mode-quoted-key-re 1 font-lock-keyword-face)
(list json-mode-quoted-string-re 1 font-lock-string-face)
(list json-mode-keyword-re 1 font-lock-constant-face)
(list json-mode-number-re 1 font-lock-constant-face)
)
(list json-mode-number-re 1 font-lock-constant-face))
"Level one font lock.")

(defvar json-mode-syntax-table
(let ((st (make-syntax-table)))
;; Objects
(modify-syntax-entry ?\{ "(}" st)
(modify-syntax-entry ?\} "){" st)
;; Arrays
(modify-syntax-entry ?\[ "(]" st)
(modify-syntax-entry ?\] ")[" st)
;; Strings
(modify-syntax-entry ?\" "\"" st)
st))

(defvar jsonc-mode-syntax-table
(let ((st (copy-syntax-table json-mode-syntax-table)))
;; Comments
(modify-syntax-entry ?/ ". 124" st)
(modify-syntax-entry ?\n ">" st)
(modify-syntax-entry ?\^m ">" st)
(modify-syntax-entry ?* ". 23bn" st)
st))

(defun json-mode--syntactic-face (state)
"Return syntactic face function for the position represented by STATE.
STATE is a `parse-partial-sexp' state, and the returned function is the
json font lock syntactic face function."
(cond
((nth 3 state)
;; This might be a string or a name
(let ((startpos (nth 8 state)))
(save-excursion
(goto-char startpos)
(if (looking-at-p json-mode-quoted-key-re)
font-lock-keyword-face
font-lock-string-face))))
((nth 4 state) font-lock-comment-face)))

;;;###autoload
(define-derived-mode json-mode javascript-mode "JSON"
"Major mode for editing JSON files"
(set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
:syntax-table json-mode-syntax-table
(set (make-local-variable 'font-lock-defaults)
'(json-font-lock-keywords-1
nil nil nil nil
(font-lock-syntactic-face-function . json-mode--syntactic-face))))

;;;###autoload
(define-derived-mode jsonc-mode json-mode "JSONC"
"Major mode for editing JSON files with comments"
:syntax-table jsonc-mode-syntax-table)

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