From 928bad93609773ca5e9c94cf6e8f24e895e263d3 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 17 Feb 2025 13:27:52 -0800 Subject: [PATCH] Rename treesit-simple-indent-modify-rules and fix docstring * lisp/treesit.el (treesit-simple-indent-modify-rules): Rename and fix docstring. It should be matcher, not anchor. Also reword a little bit. * etc/NEWS: Update. * lisp/textmodes/mhtml-ts-mode.el: (mhtml-ts-mode--treesit-indent-rules): Update accordingly. --- etc/NEWS | 11 +++++------ lisp/textmodes/mhtml-ts-mode.el | 4 ++-- lisp/treesit.el | 18 ++++++++++++------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 190da3e3797..95606658a1d 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1404,12 +1404,11 @@ settings. In a multi-language major mode it is sometimes necessary to replace features from one of the major modes, with others that are better suited to the new multilingual context. -*** New function 'treesit-modify-indent-rules'. -Given two tree-sitter indent rules, it replaces, adds, or prepends the -new rules to the old ones, then returns the new tree-sitter indent -rules. In a multi-language major mode it is sometimes necessary to -modify rules from one of the major modes, with others that are better -suited to the new multilingual context. +*** New function 'treesit-simple-indent-modify-rules'. +Given two tree-sitter indent rules, it replaces, adds, or prepends rules +in the old rules with new ones, then returns the modified rules. In a +multi-language major mode it is sometimes necessary to modify rules from +one of the major modes to better suit the new multilingual context. +++ *** New command 'treesit-explore'. diff --git a/lisp/textmodes/mhtml-ts-mode.el b/lisp/textmodes/mhtml-ts-mode.el index 0d53176f166..64f44751213 100644 --- a/lisp/textmodes/mhtml-ts-mode.el +++ b/lisp/textmodes/mhtml-ts-mode.el @@ -324,14 +324,14 @@ NODE and PARENT are ignored." ;; Extended rules for js and css, to ;; indent appropriately when injected ;; into html - (treesit-modify-indent-rules + (treesit-simple-indent-modify-rules 'javascript `((javascript ((parent-is "program") mhtml-ts-mode--js-css-tag-bol mhtml-ts-mode--js-css-indent-offset))) js--treesit-indent-rules :replace) - (treesit-modify-indent-rules + (treesit-simple-indent-modify-rules 'css `((css ((parent-is "stylesheet") mhtml-ts-mode--js-css-tag-bol diff --git a/lisp/treesit.el b/lisp/treesit.el index cd95524fdf4..efc3b153ee8 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -2540,12 +2540,18 @@ end of existing rules." (append rules existing-rules))))) (setf (alist-get language treesit-simple-indent-rules) new-rules))) -(defun treesit-modify-indent-rules (lang new-rules rules &optional how) - "Modify a copy of RULES using NEW-RULES. -As default replace rules with the same anchor. -When HOW is :prepend NEW-RULES are prepend to RULES, when -:append NEW-RULES are appended to RULES, when :replace (the default) -NEW-RULES replace rule in RULES which the same anchor." +(defun treesit-simple-indent-modify-rules (lang new-rules rules &optional how) + "Pick out rules for LANG in RULES, and modify it according to NEW_RULES. + +RULES should have the same form as `treesit-simple-indent-rules', i.e, a +list of (LANG RULES...). Return a new modified rules in the form +of (LANG RULES...). + +If HOW is omitted or :replace, for each rule in NEW-RULES, find the old +rule that has the same matcher, and replace it. + +If HOW is :prepend, just prepend NEW-RULES to the old rules; if HOW is +:append, append NEW-RULES." (cond ((not (alist-get lang rules)) (error "No rules for language %s in RULES" lang))