[json] Fix functions optionally working on regions

so that they work if no region has even been
selected in the current emacs section.
This commit is contained in:
Maximilian Wolff 2021-05-11 21:24:52 +02:00
parent 060f558530
commit 346b7a8a12
No known key found for this signature in database
GPG key ID: 2DD07025BFDBD89A
2 changed files with 21 additions and 20 deletions

View file

@ -35,29 +35,29 @@
(when (eq json-backend 'lsp)
(lsp)))
(defun spacemacs/json-navigator-dwim (arg &optional start end)
(defun spacemacs/json-navigator-dwim (arg)
"Display the JSON hierarchy of the whole buffer or the active region.
If ARG is a universal prefix argument then display the hierarchy after point."
(interactive "P\nr")
(interactive "P")
(if arg
(json-navigator-navigate-after-point)
(if (equal start end)
(if (not (use-region-p))
(save-excursion (json-navigator-navigate-region (point-min) (point-max)))
(json-navigator-navigate-region start end))))
(json-navigator-navigate-region (region-beginning) (region-end)))))
(defun spacemacs/json-reformat-dwim (arg &optional start end)
(defun spacemacs/json-reformat-dwim (arg)
"Reformat the whole buffer of the active region.
If ARG is non-nil (universal prefix argument) then try to decode the strings.
If ARG is a numerical prefix argument then specify the indentation level."
(interactive "P\nr")
(interactive "P")
(let ((json-reformat:indent-width js-indent-level)
(json-reformat:pretty-string? nil))
(cond
((numberp arg) (setq json-reformat:indent-width arg))
(arg (setq json-reformat:pretty-string? t)))
(if (equal start end)
(if (not (use-region-p))
(save-excursion (json-reformat-region (point-min) (point-max)))
(json-reformat-region start end))))
(json-reformat-region (region-beginning) (region-end)))))
(defun spacemacs/json-setup-prettier ()
"Tell prettier the content is to be parsed as JSON regardless of any file

View file

@ -21,17 +21,17 @@
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(setq json-packages
'(
company
add-node-modules-path
flycheck
json-mode
json-navigator
json-reformat
json-snatcher
prettier-js
web-beautify))
(defconst json-packages
'(
company
add-node-modules-path
flycheck
json-mode
json-navigator
json-reformat
json-snatcher
prettier-js
web-beautify))
(defun json/post-init-company ()
(spacemacs//json-setup-company))
@ -58,7 +58,8 @@
:defer t
:init
(progn
;; json-navigator-mode
(evilified-state-evilify-map json-navigator-mode-map
:mode json-navigator-mode)
(spacemacs/set-leader-keys-for-major-mode 'json-mode
"Th" 'spacemacs/json-navigator-dwim))))