From 1bb7b43e1cfb911737bc4807671910238277470f Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 10 Oct 2022 11:56:12 -0700 Subject: [PATCH] Make treesit-node-at faster * lisp/treesit.el (treesit-node-at): We only need to use treesit-node-first-child-for-pos. This is both faster for large buffers and simpler. Also remove the TODO comment. --- lisp/treesit.el | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index b51236b95e9..1e911df1d96 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -115,19 +115,17 @@ greater or larger than POINT. Return nil if none find. If NAMED non-nil, only look for named node. If PARSER-OR-LANG is nil, use the first parser in -(`treesit-parser-list'); if PARSER-OR-LANG is a parser, use +\(`treesit-parser-list'); if PARSER-OR-LANG is a parser, use that parser; if PARSER-OR-LANG is a language, find a parser using that language in the current buffer, and use that." (let ((node (if (treesit-parser-p parser-or-lang) (treesit-parser-root-node parser-or-lang) - (treesit-buffer-root-node parser-or-lang)))) - ;; TODO: We might want a `treesit-node-descendant-for-pos' in C. - (while (cond ((and node (< (treesit-node-end node) point)) - (setq node (treesit-node-next-sibling node)) - t) - ((treesit-node-child node 0 named) - (setq node (treesit-node-child node 0 named)) - t))) + (treesit-buffer-root-node parser-or-lang))) + next) + ;; This is very fast so no need for C implementation. + (while (setq next (treesit-node-first-child-for-pos + node point named)) + (setq node next)) node)) (defun treesit-node-on (beg end &optional parser-or-lang named) -- 2.39.2