Make c-ts-mode tests pass even for older tree-sitter libraries

* lisp/progmodes/c-ts-mode.el (c-ts-mode--for-loop-indent-rule):
Use child index instead of field name.
This commit is contained in:
Yuan Fu 2024-12-10 22:08:10 -08:00
parent 63b9ddda8f
commit 10e49d7fae
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -364,20 +364,31 @@ PARENT is the parent of the current node."
NODE and PARENT as usual."
(when (treesit-node-match-p parent "for_statement")
(pcase (treesit-node-field-name node)
("initializer"
;; Anchor is the opening paren.
(cons (treesit-node-start (treesit-node-child parent 1)) 1))
((or "condition" "update")
(cons (treesit-node-start (treesit-node-prev-sibling node 'named))
0))
("body"
(cons (c-ts-common--standalone-parent parent)
c-ts-mode-indent-offset))
(_ (if (treesit-node-match-p node ")")
;; Anchor is the opening paren.
(cons (treesit-node-start (treesit-node-child parent 1)) 0)
nil)))))
;; The first version of this function tests for the field name of
;; NODE, which is a lot cleaner. Alas, older tree-sitter library
;; has a bug in treesit-node-field-name-for-child, which make it
;; give the wrong field name for a child node.
(cond
;; Body (Check if NODE is the last child, because when
;; initializer/condition/update is empty, the index of body can
;; change). Eg, for (;;) {...}
((treesit-node-eq node (treesit-node-child parent -1 'named))
(cons (c-ts-common--standalone-parent parent)
c-ts-mode-indent-offset))
;; Initializer.
((and (treesit-node-check node 'named)
(eq (treesit-node-index node 'named) 0 ))
;; Anchor is the opening paren.
(cons (treesit-node-start (treesit-node-child parent 1)) 1))
;; Condition and update.
((and (treesit-node-check node 'named)
(<= 1 (treesit-node-index node 'named) 2))
(cons (treesit-node-start (treesit-node-prev-sibling node 'named))
0))
((treesit-node-match-p node ")")
;; Anchor is the opening paren.
(cons (treesit-node-start (treesit-node-child parent 1)) 0))
(t nil))))
(defvar c-ts-mode--preproc-indent-rules
`(((node-is "preproc") column-0 0)