aboutsummaryrefslogtreecommitdiff
path: root/modules/prog-json.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-06-20 11:32:21 -0400
committerCraig Jennings <c@cjennings.net>2026-06-20 11:32:21 -0400
commit5b5d24b5d14b9828b2acc2e39b4530be9091e512 (patch)
tree7f754389fd8d62d148a76ce21b7b055177d5a463 /modules/prog-json.el
parented7e6b2a89fc31d2e23f2c53f8af49d3ff00586d (diff)
downloaddotemacs-5b5d24b5d14b9828b2acc2e39b4530be9091e512.tar.gz
dotemacs-5b5d24b5d14b9828b2acc2e39b4530be9091e512.zip
refactor: extract shared format-region helper into system-lib
prog-json and prog-yaml each carried a byte-identical cj/--<lang>-format-region that runs a formatter over the buffer via call-process-region and replaces it on exit 0. Hoist it to system-lib as cj/format-region-with-program with a generic output buffer, and point both formatters at it. Adds the first direct unit coverage of the helper (Normal, Boundary, Error).
Diffstat (limited to 'modules/prog-json.el')
-rw-r--r--modules/prog-json.el31
1 files changed, 4 insertions, 27 deletions
diff --git a/modules/prog-json.el b/modules/prog-json.el
index 953b5f79b..e7abd1828 100644
--- a/modules/prog-json.el
+++ b/modules/prog-json.el
@@ -9,7 +9,7 @@
;; Eager reason: none necessary; currently eager but should load by JSON major
;; mode (Phase 6 deferral candidate).
;; Top-level side effects: one add-hook, package configuration via use-package.
-;; Runtime requires: none (configures packages via use-package).
+;; Runtime requires: system-lib (cj/format-region-with-program).
;; Direct test load: yes.
;;
;; JSON editing with tree-sitter highlighting, one-key formatting, and
@@ -27,6 +27,8 @@
;;; Code:
+(require 'system-lib)
+
(defvar json-ts-mode-map)
;; -------------------------------- JSON Mode ----------------------------------
@@ -41,38 +43,13 @@
;; -------------------------------- Formatting ---------------------------------
;; pretty-print with sorted keys, bound to standard format key
-(defun cj/--json-format-region (program &rest args)
- "Replace the buffer with PROGRAM ARGS run over its contents, via argv.
-Runs PROGRAM (with ARGS) on the whole buffer through
-`call-process-region' — no shell, so no quoting or word-splitting.
-The buffer is replaced only when PROGRAM exits zero; on a non-zero
-exit the buffer is left untouched and an error is signalled with
-the program's stderr text. Point is preserved as closely as the
-reformatted size allows. Returns t on success."
- (let* ((point (point))
- (src (current-buffer))
- (out (generate-new-buffer " *json-format-out*"))
- (status (apply #'call-process-region
- (point-min) (point-max) program
- nil out nil args)))
- (unwind-protect
- (if (and (integerp status) (zerop status))
- (progn
- (with-current-buffer src
- (replace-buffer-contents out)
- (goto-char (min point (point-max))))
- t)
- (user-error "%s failed: %s" program
- (string-trim (with-current-buffer out (buffer-string)))))
- (kill-buffer out))))
-
(defun cj/json-format-buffer ()
"Format the current JSON buffer with sorted keys.
Uses jq if available for reliable formatting, otherwise falls
back to the built-in `json-pretty-print-buffer-ordered'."
(interactive)
(if (executable-find "jq")
- (cj/--json-format-region "jq" "--sort-keys" ".")
+ (cj/format-region-with-program "jq" "--sort-keys" ".")
(json-pretty-print-buffer-ordered)))
(defun cj/json-setup ()