aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2025-01-30 16:54:20 -0800
committerYuan Fu2025-01-30 21:34:31 -0800
commitef28af35bb4c43d71fe4c10d02fe93f30e830c5e (patch)
treecec88fd7cadeee6b83d2a93994e49835632a8433
parenta5965217fc1d7b56df60f8e798edd48ae52c8624 (diff)
downloademacs-ef28af35bb4c43d71fe4c10d02fe93f30e830c5e.tar.gz
emacs-ef28af35bb4c43d71fe4c10d02fe93f30e830c5e.zip
Add treesit-add-simple-indent-rules
* lisp/treesit.el: (treesit-add-simple-indent-rules): New function. * etc/NEWS: Update NEWS. * test/src/treesit-tests.el: (treesit-test-add-simple-indent-rules): Add a test for the new function.
-rw-r--r--etc/NEWS4
-rw-r--r--lisp/treesit.el29
-rw-r--r--test/src/treesit-tests.el21
3 files changed, 54 insertions, 0 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 1e7365258a3..9d0eb7b7d58 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1159,6 +1159,10 @@ at point to explore.
1159*** New variable 'treesit-aggregated-simple-imenu-settings' 1159*** New variable 'treesit-aggregated-simple-imenu-settings'
1160This variable allows major modes to setup Imenu for multiple languages. 1160This variable allows major modes to setup Imenu for multiple languages.
1161 1161
1162*** New function 'treesit-add-simple-indent-rules'
1163This new function makes it easier to customize indent rules for
1164tree-sitter modes.
1165
1162+++ 1166+++
1163** New optional BUFFER argument for 'string-pixel-width'. 1167** New optional BUFFER argument for 'string-pixel-width'.
1164If supplied, 'string-pixel-width' will use any face remappings from 1168If supplied, 'string-pixel-width' will use any face remappings from
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 76b6bdc9cb2..1940dd37795 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2401,6 +2401,35 @@ RULES."
2401 offset))))) 2401 offset)))))
2402 (cons lang (mapcar #'optimize-rule indent-rules))))) 2402 (cons lang (mapcar #'optimize-rule indent-rules)))))
2403 2403
2404(defun treesit-add-simple-indent-rules (language rules &optional where anchor)
2405 "Add simple indent RULES for LANGUAGE.
2406
2407WHERE can be either :before or :after, which means adding RULES before
2408or after the existing rules in `treesit-simple-indent-rules'. If
2409ommited, default to adding the rules before (so it overrides existing
2410rules).
2411
2412If ANCHOR is non-nil, add RULES before/after the rules in
2413`treesit-simple-indent-rules' that's `equal' to ANCHOR. If ANCHOR is
2414omitted or no existing rules matches it, add RULES at the beginning or
2415end of existing rules."
2416 (when (not (memq where '(nil :before :after)))
2417 (error "WHERE must be either :before, :after, or nil"))
2418 (let* ((existing-rules (alist-get language treesit-simple-indent-rules))
2419 (anchor-idx (and anchor (seq-position existing-rules anchor)))
2420 (new-rules
2421 (if anchor-idx
2422 (let* ((pivot (if (eq where :after)
2423 (1+ anchor-idx)
2424 anchor-idx))
2425 (first-half (seq-subseq existing-rules 0 pivot))
2426 (second-half (seq-subseq existing-rules pivot)))
2427 (append first-half rules second-half))
2428 (if (eq where :after)
2429 (append existing-rules rules)
2430 (append rules existing-rules)))))
2431 (setf (alist-get language treesit-simple-indent-rules) new-rules)))
2432
2404;;; Search 2433;;; Search
2405 2434
2406(defun treesit-search-forward-goto 2435(defun treesit-search-forward-goto
diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el
index f9c64f792d1..22f53243274 100644
--- a/test/src/treesit-tests.el
+++ b/test/src/treesit-tests.el
@@ -411,6 +411,27 @@ BODY is the test body."
411 (let ((missing-bracket (treesit-node-child array -1))) 411 (let ((missing-bracket (treesit-node-child array -1)))
412 (treesit-search-forward missing-bracket "" t)))) 412 (treesit-search-forward missing-bracket "" t))))
413 413
414;;; Indent
415
416(ert-deftest treesit-test-add-simple-indent-rules ()
417 "Test `treesit-add-simple-indent-rules'."
418 (let ((treesit-simple-indent-rules
419 (copy-tree '((c (a a a) (b b b) (c c c))))))
420 (treesit-add-simple-indent-rules 'c '((d d d)))
421 (should (equal treesit-simple-indent-rules
422 '((c (d d d) (a a a) (b b b) (c c c)))))
423 (treesit-add-simple-indent-rules 'c '((e e e)) :after)
424 (should (equal treesit-simple-indent-rules
425 '((c (d d d) (a a a) (b b b) (c c c) (e e e)))))
426 (treesit-add-simple-indent-rules 'c '((f f f)) :after '(b b b))
427 (should (equal treesit-simple-indent-rules
428 '((c (d d d) (a a a) (b b b) (f f f)
429 (c c c) (e e e)))))
430 (treesit-add-simple-indent-rules 'c '((g g g)) :before '(b b b))
431 (should (equal treesit-simple-indent-rules
432 '((c (d d d) (a a a) (g g g)
433 (b b b) (f f f) (c c c) (e e e)))))))
434
414;;; Query 435;;; Query
415 436
416(defun treesit--ert-pred-last-sibling (node) 437(defun treesit--ert-pred-last-sibling (node)