From db759f8048b87e2fc8d15f400e204c3a2b64f684 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Thu, 13 Feb 2025 20:47:23 -0800 Subject: [PATCH] Allow treesit-simple-indent-standalone-predicate to return anchor * lisp/treesit.el: (treesit-simple-indent-standalone-predicate): Allow it to return an anchor instead of t. (treesit-simple-indent-presets): Supports number. * lisp/progmodes/c-ts-common.el: (c-ts-common--standalone-parent): (c-ts-common--prev-standalone-sibling): Supports number. (cherry picked from commit 34f90bf2cab7c1711136dc055b665e46f714c3e1) --- lisp/progmodes/c-ts-common.el | 52 +++++++++++++++++++---------------- lisp/treesit.el | 34 +++++++++++++---------- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 90eb7aa64c8..e23ae786ab5 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el @@ -569,20 +569,22 @@ chaining like But ff `treesit-simple-indent-standalone-predicate' is non-nil, use that for determining standlone line." - (save-excursion - (catch 'term - (while parent - (goto-char (treesit-node-start parent)) - (when (if treesit-simple-indent-standalone-predicate - (funcall treesit-simple-indent-standalone-predicate - parent) - (looking-back (rx bol (* whitespace) (? ".")) - (line-beginning-position))) - (throw 'term (point))) - (setq parent (treesit-node-parent parent)))))) + (let (anchor) + (save-excursion + (catch 'term + (while parent + (goto-char (treesit-node-start parent)) + (when (if treesit-simple-indent-standalone-predicate + (setq anchor + (funcall treesit-simple-indent-standalone-predicate + parent)) + (looking-back (rx bol (* whitespace) (? ".")) + (line-beginning-position))) + (throw 'term (if (numberp anchor) anchor (point)))) + (setq parent (treesit-node-parent parent))))))) (defun c-ts-common--prev-standalone-sibling (node) - "Return the previous sibling of NODE that starts on a new line. + "Return the start of the previous sibling of NODE that starts on a new line. Return nil if no sibling satisfies the condition. Unlike simple-indent's standalone preset, this function handles method @@ -597,15 +599,18 @@ for determining standlone line." (save-excursion (setq node (treesit-node-prev-sibling node 'named)) (goto-char (treesit-node-start node)) - (while (and node - (goto-char (treesit-node-start node)) - (not (if treesit-simple-indent-standalone-predicate - (funcall treesit-simple-indent-standalone-predicate - node) - (looking-back (rx bol (* whitespace) (? ".")) - (pos-bol))))) - (setq node (treesit-node-prev-sibling node 'named))) - node)) + (let (anchor) + (while (and node + (goto-char (treesit-node-start node)) + (not (if treesit-simple-indent-standalone-predicate + (setq anchor + (funcall + treesit-simple-indent-standalone-predicate + node)) + (looking-back (rx bol (* whitespace) (? ".")) + (pos-bol))))) + (setq node (treesit-node-prev-sibling node 'named)))) + (if (numberp anchor) anchor (treesit-node-start node)))) (defun c-ts-common-parent-ignore-preproc (node) "Return the parent of NODE, skipping preproc nodes." @@ -696,9 +701,8 @@ The rule also handles method chaining like (cons (c-ts-common--standalone-parent parent) offset))) ;; Not first sibling - (t (cons (treesit-node-start - (or (c-ts-common--prev-standalone-sibling node) - first-sibling)) + (t (cons (or (c-ts-common--prev-standalone-sibling node) + (treesit-node-start first-sibling)) 0))))) ;; Condition 2 for initializer list, only apply to ;; second line. Eg, diff --git a/lisp/treesit.el b/lisp/treesit.el index dc9248acb26..5fabdc32314 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1817,7 +1817,8 @@ to be standalone too: }); The value should be a function that takes a node, and return t if it's -standalone.") +standalone. If the function returns a position, that position is used +as the anchor.") (defvar treesit-simple-indent-presets (list (cons 'match @@ -1952,20 +1953,23 @@ standalone.") (goto-char (treesit-node-start parent)) (back-to-indentation) (point)))) - (cons 'standalone-parent - (lambda (_n parent &rest _) - (save-excursion - (catch 'term - (while parent - (goto-char (treesit-node-start parent)) - (when - (if (null treesit-simple-indent-standalone-predicate) - (looking-back (rx bol (* whitespace)) - (line-beginning-position)) - (funcall treesit-simple-indent-standalone-predicate - parent)) - (throw 'term (point))) - (setq parent (treesit-node-parent parent))))))) + (cons + 'standalone-parent + (lambda (_n parent &rest _) + (save-excursion + (let (anchor) + (catch 'term + (while parent + (goto-char (treesit-node-start parent)) + (when (if (null treesit-simple-indent-standalone-predicate) + (looking-back (rx bol (* whitespace)) + (line-beginning-position)) + (setq anchor + (funcall + treesit-simple-indent-standalone-predicate + parent))) + (throw 'term (if (numberp anchor) anchor (point)))) + (setq parent (treesit-node-parent parent)))))))) (cons 'prev-sibling (lambda (node parent bol &rest _) (treesit-node-start (or (treesit-node-prev-sibling node t) -- 2.39.5