summaryrefslogtreecommitdiff
path: root/modules/prog-json.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-03-02 19:46:38 -0600
committerCraig Jennings <c@cjennings.net>2026-03-02 19:46:38 -0600
commitb5cae72d8f824f235f351821f7d7052b66bc2513 (patch)
tree264261c26ec5a6c721e5dbd83e93237e842e53b5 /modules/prog-json.el
parenta46f8af939b112b603a2c95b2e83a1932b208e20 (diff)
feat(json,yaml): add tree-sitter modes, formatting, and jq integration
New prog-json module: json-ts-mode with jq formatting (C-; f) and jq-interactively (C-c C-q). Upgraded prog-yaml to yaml-ts-mode with prettier formatting. Both use treesit-auto for grammar management. Includes 18 new tests (10 JSON, 8 YAML), 185/185 passing.
Diffstat (limited to 'modules/prog-json.el')
-rw-r--r--modules/prog-json.el60
1 files changed, 60 insertions, 0 deletions
diff --git a/modules/prog-json.el b/modules/prog-json.el
new file mode 100644
index 00000000..6dba6dee
--- /dev/null
+++ b/modules/prog-json.el
@@ -0,0 +1,60 @@
+;;; prog-json.el --- JSON Editing, Formatting, and jq Integration -*- lexical-binding: t; coding: utf-8; -*-
+;; Author: Craig Jennings <c@cjennings.net>
+
+;;; Commentary:
+;; JSON editing with tree-sitter highlighting, one-key formatting, and
+;; interactive jq queries against the current buffer.
+;;
+;; Features:
+;; - Tree-sitter: Better syntax highlighting and structural navigation
+;; - Formatting: Pretty-print with sorted keys via C-; f
+;; - jq: Interactive jq REPL against current JSON buffer
+;;
+;; Workflow:
+;; 1. Open .json file → json-ts-mode with tree-sitter highlighting
+;; 2. C-; f → Format/pretty-print the buffer
+;; 3. C-c C-q → Open jq interactive buffer to query/transform JSON
+
+;;; Code:
+
+(defvar json-ts-mode-map)
+
+;; -------------------------------- JSON Mode ----------------------------------
+;; tree-sitter mode for JSON files (built-in, Emacs 29+)
+;; NOTE: No :mode directive here — treesit-auto (in prog-general.el) handles
+;; the auto-mode-alist mapping and auto-installs the grammar on first use.
+
+(use-package json-ts-mode
+ :ensure nil
+ :defer t)
+
+;; -------------------------------- Formatting ---------------------------------
+;; pretty-print with sorted keys, bound to standard format key
+
+(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")
+ (let ((point (point)))
+ (shell-command-on-region (point-min) (point-max) "jq --sort-keys ." nil t)
+ (goto-char (min point (point-max))))
+ (json-pretty-print-buffer-ordered)))
+
+(defun cj/json-setup ()
+ "Set up JSON buffer keybindings."
+ (local-set-key (kbd "C-; f") #'cj/json-format-buffer))
+
+(add-hook 'json-ts-mode-hook #'cj/json-setup)
+
+;; --------------------------------- jq Mode -----------------------------------
+;; interactive jq queries against JSON buffers
+
+(use-package jq-mode
+ :defer t
+ :bind (:map json-ts-mode-map
+ ("C-c C-q" . jq-interactively)))
+
+(provide 'prog-json)
+;;; prog-json.el ends here.