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.
This commit is contained in:
Yuan Fu 2025-02-13 20:47:23 -08:00
parent adabee8873
commit 34f90bf2ca
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 47 additions and 39 deletions

View file

@ -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,

View file

@ -1821,7 +1821,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
@ -1956,20 +1957,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)