]> git.eshelyaron.com Git - emacs.git/commitdiff
Add treesit-add-simple-indent-rules
authorYuan Fu <casouri@gmail.com>
Fri, 31 Jan 2025 00:54:20 +0000 (16:54 -0800)
committerEshel Yaron <me@eshelyaron.com>
Mon, 3 Feb 2025 11:11:24 +0000 (12:11 +0100)
* 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.

(cherry picked from commit ef28af35bb4c43d71fe4c10d02fe93f30e830c5e)

lisp/treesit.el
test/src/treesit-tests.el

index 079187e3a540f1f0079012cde4d932c93fa38cba..e05f87946c6a10a0b970bef7915871ea3d5b3abf 100644 (file)
@@ -2395,6 +2395,35 @@ RULES."
                             offset)))))
              (cons lang (mapcar #'optimize-rule indent-rules)))))
 
+(defun treesit-add-simple-indent-rules (language rules &optional where anchor)
+  "Add simple indent RULES for LANGUAGE.
+
+WHERE can be either :before or :after, which means adding RULES before
+or after the existing rules in `treesit-simple-indent-rules'.  If
+ommited, default to adding the rules before (so it overrides existing
+rules).
+
+If ANCHOR is non-nil, add RULES before/after the rules in
+`treesit-simple-indent-rules' that's `equal' to ANCHOR.  If ANCHOR is
+omitted or no existing rules matches it, add RULES at the beginning or
+end of existing rules."
+  (when (not (memq where '(nil :before :after)))
+    (error "WHERE must be either :before, :after, or nil"))
+  (let* ((existing-rules (alist-get language treesit-simple-indent-rules))
+         (anchor-idx (and anchor (seq-position existing-rules anchor)))
+         (new-rules
+          (if anchor-idx
+              (let* ((pivot (if (eq where :after)
+                                (1+ anchor-idx)
+                              anchor-idx))
+                     (first-half (seq-subseq existing-rules 0 pivot))
+                     (second-half (seq-subseq existing-rules pivot)))
+                (append first-half rules second-half))
+            (if (eq where :after)
+                (append existing-rules rules)
+              (append rules existing-rules)))))
+    (setf (alist-get language treesit-simple-indent-rules) new-rules)))
+
 ;;; Search
 
 (defun treesit-search-forward-goto
index 699b42df6b7ca97290a84fdc95dd48917d63da33..811dc3684ad168084f1a4bee50890ce6481cca66 100644 (file)
@@ -411,6 +411,27 @@ BODY is the test body."
    (let ((missing-bracket (treesit-node-child array -1)))
      (treesit-search-forward missing-bracket "" t))))
 
+;;; Indent
+
+(ert-deftest treesit-test-add-simple-indent-rules ()
+  "Test `treesit-add-simple-indent-rules'."
+  (let ((treesit-simple-indent-rules
+         (copy-tree '((c (a a a) (b b b) (c c c))))))
+    (treesit-add-simple-indent-rules 'c '((d d d)))
+    (should (equal treesit-simple-indent-rules
+                   '((c (d d d) (a a a) (b b b) (c c c)))))
+    (treesit-add-simple-indent-rules 'c '((e e e)) :after)
+    (should (equal treesit-simple-indent-rules
+                   '((c (d d d) (a a a) (b b b) (c c c) (e e e)))))
+    (treesit-add-simple-indent-rules 'c '((f f f)) :after '(b b b))
+    (should (equal treesit-simple-indent-rules
+                   '((c (d d d) (a a a) (b b b) (f f f)
+                        (c c c) (e e e)))))
+    (treesit-add-simple-indent-rules 'c '((g g g)) :before '(b b b))
+    (should (equal treesit-simple-indent-rules
+                   '((c (d d d) (a a a) (g g g)
+                        (b b b) (f f f) (c c c) (e e e)))))))
+
 ;;; Query
 
 (defun treesit--ert-pred-last-sibling (node)