From 3479d42406b7952fe22917c14f9e1dd5522d364c Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Thu, 30 Jan 2025 21:16:39 -0800 Subject: [PATCH] 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. --- etc/NEWS | 4 +++ lisp/treesit.el | 66 ++++++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 9d0eb7b7d58..7f6084490d8 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1163,6 +1163,10 @@ This variable allows major modes to setup Imenu for multiple languages. This new function makes it easier to customize indent rules for tree-sitter modes. +*** New variable 'treesit-simple-indent-override-rules' +Users can customize this variable to add simple custom indentation rules +for tree-sitter major modes. + +++ ** New optional BUFFER argument for 'string-pixel-width'. If supplied, 'string-pixel-width' will use any face remappings from diff --git a/lisp/treesit.el b/lisp/treesit.el index 1940dd37795..97fb2f478f2 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1757,6 +1757,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 @@ -2303,35 +2310,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")) @@ -2404,6 +2415,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