aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2024-12-10 22:08:10 -0800
committerYuan Fu2024-12-10 22:10:41 -0800
commit10e49d7fae708d64fe68effe5890ea77c875a799 (patch)
tree4be0f15ae943b9f1a75196b923f732ff58b4b8ef
parent63b9ddda8f8dcfea2927cc4fab7134a7f65c0229 (diff)
downloademacs-10e49d7fae708d64fe68effe5890ea77c875a799.tar.gz
emacs-10e49d7fae708d64fe68effe5890ea77c875a799.zip
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.
-rw-r--r--lisp/progmodes/c-ts-mode.el39
1 files changed, 25 insertions, 14 deletions
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."
364 364
365NODE and PARENT as usual." 365NODE and PARENT as usual."
366 (when (treesit-node-match-p parent "for_statement") 366 (when (treesit-node-match-p parent "for_statement")
367 (pcase (treesit-node-field-name node) 367 ;; The first version of this function tests for the field name of
368 ("initializer" 368 ;; NODE, which is a lot cleaner. Alas, older tree-sitter library
369 ;; Anchor is the opening paren. 369 ;; has a bug in treesit-node-field-name-for-child, which make it
370 (cons (treesit-node-start (treesit-node-child parent 1)) 1)) 370 ;; give the wrong field name for a child node.
371 ((or "condition" "update") 371 (cond
372 (cons (treesit-node-start (treesit-node-prev-sibling node 'named)) 372 ;; Body (Check if NODE is the last child, because when
373 0)) 373 ;; initializer/condition/update is empty, the index of body can
374 ("body" 374 ;; change). Eg, for (;;) {...}
375 (cons (c-ts-common--standalone-parent parent) 375 ((treesit-node-eq node (treesit-node-child parent -1 'named))
376 c-ts-mode-indent-offset)) 376 (cons (c-ts-common--standalone-parent parent)
377 (_ (if (treesit-node-match-p node ")") 377 c-ts-mode-indent-offset))
378 ;; Anchor is the opening paren. 378 ;; Initializer.
379 (cons (treesit-node-start (treesit-node-child parent 1)) 0) 379 ((and (treesit-node-check node 'named)
380 nil))))) 380 (eq (treesit-node-index node 'named) 0 ))
381 ;; Anchor is the opening paren.
382 (cons (treesit-node-start (treesit-node-child parent 1)) 1))
383 ;; Condition and update.
384 ((and (treesit-node-check node 'named)
385 (<= 1 (treesit-node-index node 'named) 2))
386 (cons (treesit-node-start (treesit-node-prev-sibling node 'named))
387 0))
388 ((treesit-node-match-p node ")")
389 ;; Anchor is the opening paren.
390 (cons (treesit-node-start (treesit-node-child parent 1)) 0))
391 (t nil))))
381 392
382(defvar c-ts-mode--preproc-indent-rules 393(defvar c-ts-mode--preproc-indent-rules
383 `(((node-is "preproc") column-0 0) 394 `(((node-is "preproc") column-0 0)