From: Yuan Fu Date: Fri, 31 Jan 2025 05:16:39 +0000 (-0800) Subject: Add treesit-simple-indent-override-rules X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=da187109e5984ff1d463f63479be6d82960c30f3;p=emacs.git Add treesit-simple-indent-override-rules * lisp/treesit.el: (treesit-simple-indent-override-rules): New variable. (treesit-simple-indent): Try treesit-simple-indent-override-rules first. (treesit-add-simple-indent-rules): Mention the new variable. * etc/NEWS: Update news. (cherry picked from commit 3479d42406b7952fe22917c14f9e1dd5522d364c) --- diff --git a/lisp/treesit.el b/lisp/treesit.el index e05f87946c6..81ca131769f 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1755,6 +1755,13 @@ should take the same argument as MATCHER or ANCHOR. If it matches, return a cons (ANCHOR-POS . OFFSET), where ANCHOR-POS is a position and OFFSET is the indent offset; if it doesn't match, return nil.") +(defvar-local treesit-simple-indent-override-rules nil + "Extra simple indent rules for customizing indentation. + +This variable should take the same form as +`treesit-simple-indent-rules'. Rules in this variable take precedence +over `treesit-simple-indent-rules'.") + (defun treesit--indent-prev-line-node (pos) "Return the largest node on the previous line of POS." (save-excursion @@ -2297,35 +2304,39 @@ OFFSET." (message "PARENT is nil, not indenting")) (cons nil nil)) (let* ((language (treesit-node-language parent)) - (rules (alist-get language - treesit-simple-indent-rules))) + (rules-list (list + (alist-get language + treesit-simple-indent-override-rules) + (alist-get language + treesit-simple-indent-rules)))) (catch 'match - (dolist (rule rules) - (if (functionp rule) - (let ((result (funcall rule node parent bol))) - (when result + (dolist (rules rules-list) + (dolist (rule rules) + (if (functionp rule) + (let ((result (funcall rule node parent bol))) + (when result + (when treesit--indent-verbose + (message "Matched rule: %S" rule)) + (throw 'match result))) + (let ((pred (nth 0 rule)) + (anchor (nth 1 rule)) + (offset (nth 2 rule))) + ;; Found a match. + (when (treesit--simple-indent-eval + (list pred node parent bol)) (when treesit--indent-verbose (message "Matched rule: %S" rule)) - (throw 'match result))) - (let ((pred (nth 0 rule)) - (anchor (nth 1 rule)) - (offset (nth 2 rule))) - ;; Found a match. - (when (treesit--simple-indent-eval - (list pred node parent bol)) - (when treesit--indent-verbose - (message "Matched rule: %S" rule)) - (let ((anchor-pos - (treesit--simple-indent-eval - (list anchor node parent bol))) - (offset-val - (cond ((numberp offset) offset) - ((and (symbolp offset) - (boundp offset)) - (symbol-value offset)) - (t (treesit--simple-indent-eval - (list offset node parent bol)))))) - (throw 'match (cons anchor-pos offset-val))))))) + (let ((anchor-pos + (treesit--simple-indent-eval + (list anchor node parent bol))) + (offset-val + (cond ((numberp offset) offset) + ((and (symbolp offset) + (boundp offset)) + (symbol-value offset)) + (t (treesit--simple-indent-eval + (list offset node parent bol)))))) + (throw 'match (cons anchor-pos offset-val)))))))) ;; Didn't find any match. (when treesit--indent-verbose (message "No matched rule")) @@ -2398,6 +2409,9 @@ RULES." (defun treesit-add-simple-indent-rules (language rules &optional where anchor) "Add simple indent RULES for LANGUAGE. +This function only affects `treesit-simple-indent-rules', +`treesit-simple-indent-override-rules' is not affected. + 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