From: Yuan Fu Date: Fri, 31 Jan 2025 00:54:20 +0000 (-0800) Subject: Add treesit-add-simple-indent-rules X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=2a58961b3288cfbf1fb6cdb98e7eee6221d63e56;p=emacs.git 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. (cherry picked from commit ef28af35bb4c43d71fe4c10d02fe93f30e830c5e) --- diff --git a/lisp/treesit.el b/lisp/treesit.el index 079187e3a54..e05f87946c6 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -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 diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el index 699b42df6b7..811dc3684ad 100644 --- a/test/src/treesit-tests.el +++ b/test/src/treesit-tests.el @@ -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)