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