Skip to content

Commit baffd85

Browse files
committed
added editing shortcuts and keybindings
- `C-c C-t`: Toggle between `true` and `false` at point - `C-c C-k`: Replace the sexp at point with `null` - `C-c C-i`: Increment the number at point - `C-c C-d`: Decrement the number at point
1 parent 39ba450 commit baffd85

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ json-mode.el
33

44
Major mode for editing JSON files.
55

6-
Extends the builtin js-mode to add better syntax highlighting for JSON.
6+
Extends the builtin js-mode to add better syntax highlighting for JSON
7+
and some nice editing keybindings.
78

89
Install
910
----
@@ -19,6 +20,10 @@ Default Keybindings
1920

2021
- `C-c C-f`: format the region/buffer with `json-reformat` (<https://github.com/gongo/json-reformat>)
2122
- `C-c C-p`: display a path to the object at point with `json-snatcher` (<https://github.com/Sterlingg/json-snatcher>)
23+
- `C-c C-t`: Toggle between `true` and `false` at point
24+
- `C-c C-k`: Replace the sexp at point with `null`
25+
- `C-c C-i`: Increment the number at point
26+
- `C-c C-d`: Decrement the number at point
2227

2328
Indent Width
2429
----

json-mode.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,72 @@ This function calls `json-mode--update-auto-mode' to change the
155155

156156
(define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify)
157157

158+
(defun json-toggle-boolean ()
159+
"If point is on `true' or `false', toggle it."
160+
(interactive)
161+
(unless (nth 8 (syntax-ppss)) ; inside a keyword, string or comment
162+
(let* ((bounds (bounds-of-thing-at-point 'symbol))
163+
(string (and bounds (buffer-substring-no-properties (car bounds) (cdr bounds))))
164+
(pt (point)))
165+
(when (and bounds (member string '("true" "false")))
166+
(delete-region (car bounds) (cdr bounds))
167+
(cond
168+
((string= "true" string)
169+
(insert "false")
170+
(goto-char (if (= pt (cdr bounds)) (1+ pt) pt)))
171+
(t
172+
(insert "true")
173+
(goto-char (if (= pt (cdr bounds)) (1- pt) pt))))))))
174+
175+
(define-key json-mode-map (kbd "C-c C-t") 'json-toggle-boolean)
176+
177+
(defun json-nullify-sexp ()
178+
"Replace the sexp at point with `null'."
179+
(interactive)
180+
(let ((syntax (syntax-ppss)) symbol)
181+
(cond
182+
((nth 4 syntax) nil) ; inside a comment
183+
((nth 3 syntax) ; inside a string
184+
(goto-char (nth 8 syntax))
185+
(when (save-excursion (forward-sexp) (skip-chars-forward "[:space:]") (eq (char-after) ?:))
186+
;; sexp is an object key, so we nullify the entire object
187+
(goto-char (nth 1 syntax)))
188+
(kill-sexp)
189+
(insert "null"))
190+
((setq symbol (bounds-of-thing-at-point 'symbol))
191+
(cond
192+
((looking-at-p "null"))
193+
((save-excursion (skip-chars-backward "[0-9.]") (looking-at json-mode-number-re))
194+
(kill-region (match-beginning 0) (match-end 0))
195+
(insert "null"))
196+
(t (kill-region (car symbol) (cdr symbol)) (insert "null"))))
197+
((< 0 (nth 0 syntax))
198+
(goto-char (nth 1 syntax))
199+
(kill-sexp)
200+
(insert "null"))
201+
(t nil))))
202+
203+
(define-key json-mode-map (kbd "C-c C-k") 'json-nullify-sexp)
204+
205+
(defun json-increment-number-at-point (&optional delta)
206+
"Add DELTA to the number at point; DELTA defaults to 1."
207+
(interactive)
208+
(when (save-excursion (skip-chars-backward "[0-9.]") (looking-at json-mode-number-re))
209+
(let ((num (+ (or delta 1)
210+
(string-to-number (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))
211+
(pt (point)))
212+
(delete-region (match-beginning 0) (match-end 0))
213+
(insert (number-to-string num))
214+
(goto-char pt))))
215+
216+
(define-key json-mode-map (kbd "C-c C-i") 'json-increment-number-at-point)
217+
218+
(defun json-decrement-number-at-point ()
219+
"Decrement the number at point."
220+
(interactive)
221+
(json-increment-number-at-point -1))
222+
223+
(define-key json-mode-map (kbd "C-c C-d") 'json-decrement-number-at-point)
158224

159225
(provide 'json-mode)
160226
;;; json-mode.el ends here

0 commit comments

Comments
 (0)