Skip to content

Commit 11a8b8e

Browse files
authored
Merge branch 'master' into package-tidy-up
2 parents c18a3ac + eedb456 commit 11a8b8e

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

json-mode.el

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
;; Author: Josh Johnston
66
;; URL: https://github.com/joshwnj/json-mode
77
;; Version: 1.6.0
8-
;; Package-Requires: ((json-reformat "0.0.5") (json-snatcher "1.0.0"))
8+
;; Package-Requires: ((json-snatcher "1.0.0") (emacs "24.4"))
99

1010
;; This program is free software; you can redistribute it and/or modify
1111
;; it under the terms of the GNU General Public License as published by
@@ -29,7 +29,6 @@
2929
(require 'js)
3030
(require 'rx)
3131
(require 'json-snatcher)
32-
(require 'json-reformat)
3332

3433
(defgroup json '()
3534
"Major mode for editing JSON files."
@@ -60,11 +59,14 @@ Return the new `auto-mode-alist' entry"
6059
(add-to-list 'auto-mode-alist new-entry)
6160
new-entry))
6261

62+
;;; make byte-compiler happy
63+
(defvar json-mode--auto-mode-entry)
64+
6365
;;;###autoload
6466
(defcustom json-mode-auto-mode-list '(".babelrc"
6567
".bowerrc"
6668
"composer.lock")
67-
"List of filenames to pass for the JSON entry of `auto-mode-alist'.
69+
"List of filenames for the JSON entry of `auto-mode-alist'.
6870
6971
Note however that custom `json-mode' entries in `auto-mode-alist'
7072
won’t be affected."
@@ -105,16 +107,59 @@ This function calls `json-mode--update-auto-mode' to change the
105107

106108
(defconst json-font-lock-keywords-1
107109
(list
108-
(list json-mode-quoted-key-re 1 font-lock-keyword-face)
109-
(list json-mode-quoted-string-re 1 font-lock-string-face)
110110
(list json-mode-keyword-re 1 font-lock-constant-face)
111111
(list json-mode-number-re 1 font-lock-constant-face))
112112
"Level one font lock.")
113113

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

119164
;; Well formatted JSON files almost always begin with “{” or “[”.
120165
;;;###autoload
@@ -130,21 +175,20 @@ This function calls `json-mode--update-auto-mode' to change the
130175

131176
;;;###autoload
132177
(defun json-mode-kill-path ()
133-
"Add the path to the node at point to the kill ring."
178+
"Save JSON path to object at point to kill ring."
134179
(interactive)
135180
(kill-new (jsons-print-path)))
136181

137182
(define-key json-mode-map (kbd "C-c C-k") 'json-mode-kill-path)
138183

139184
;;;###autoload
140-
(defun json-mode-beautify ()
185+
(defun json-mode-beautify (begin end)
141186
"Beautify / pretty-print the active region (or the entire buffer if no active region)."
142-
(interactive)
143-
(let ((json-reformat:indent-width js-indent-level)
144-
(json-reformat:pretty-string? t))
145-
(if (use-region-p)
146-
(json-reformat-region (region-beginning) (region-end))
147-
(json-reformat-region (buffer-end -1) (buffer-end 1)))))
187+
(interactive "r")
188+
(unless (use-region-p)
189+
(setq begin (point-min)
190+
end (point-max)))
191+
(json-pretty-print begin end))
148192

149193
(define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify)
150194

0 commit comments

Comments
 (0)