@@ -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