From: Yuan Fu Date: Wed, 11 Dec 2024 06:08:10 +0000 (-0800) Subject: Make c-ts-mode tests pass even for older tree-sitter libraries X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=cb522b5e58363a15fe98c6f029d501fc6585c11c;p=emacs.git 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. (cherry picked from commit 10e49d7fae708d64fe68effe5890ea77c875a799) --- diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index dec9411b87c..4cf0c6c405b 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -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)