Skip to content

Commit 5cd3834

Browse files
authored
Merge branch 'master' into checkdoc-fix
2 parents 668496f + bea2042 commit 5cd3834

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

json-mode.el

Lines changed: 58 additions & 15 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-mode '()
3534
"Major mode for editing JSON files."
@@ -60,13 +59,16 @@ 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 '(
6567
".babelrc"
6668
".bowerrc"
6769
"composer.lock"
6870
)
69-
"List of filenames as for the JSON entry of `auto-mode-alist'.
71+
"List of filenames for the JSON entry of `auto-mode-alist'.
7072
7173
Note however that custom `json-mode' entries in `auto-mode-alist'
7274
won’t be affected."
@@ -107,17 +109,59 @@ This function calls `json-mode--update-auto-mode' to change the
107109

108110
(defconst json-font-lock-keywords-1
109111
(list
110-
(list json-mode-quoted-key-re 1 font-lock-keyword-face)
111-
(list json-mode-quoted-string-re 1 font-lock-string-face)
112112
(list json-mode-keyword-re 1 font-lock-constant-face)
113-
(list json-mode-number-re 1 font-lock-constant-face)
114-
)
113+
(list json-mode-number-re 1 font-lock-constant-face))
115114
"Level one font lock.")
116115

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

122166
;; Well formatted JSON files almost always begin with “{” or “[”.
123167
;;;###autoload
@@ -140,14 +184,13 @@ This function calls `json-mode--update-auto-mode' to change the
140184
(define-key json-mode-map (kbd "C-c P") 'json-mode-kill-path)
141185

142186
;;;###autoload
143-
(defun json-mode-beautify ()
187+
(defun json-mode-beautify (begin end)
144188
"Beautify / pretty-print the active region (or the entire buffer if no active region)."
145-
(interactive)
146-
(let ((json-reformat:indent-width js-indent-level)
147-
(json-reformat:pretty-string? t))
148-
(if (use-region-p)
149-
(json-reformat-region (region-beginning) (region-end))
150-
(json-reformat-region (buffer-end -1) (buffer-end 1)))))
189+
(interactive "r")
190+
(unless (use-region-p)
191+
(setq begin (point-min)
192+
end (point-max)))
193+
(json-pretty-print begin end))
151194

152195
(define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify)
153196

0 commit comments

Comments
 (0)