[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
feature/tree-sitter 3114eb8078 3/3: * lisp/treesit.el (treesit-node-at):
From: |
Yuan Fu |
Subject: |
feature/tree-sitter 3114eb8078 3/3: * lisp/treesit.el (treesit-node-at): Add LARGEST argument. |
Date: |
Sun, 23 Oct 2022 19:19:31 -0400 (EDT) |
branch: feature/tree-sitter
commit 3114eb807864e19cd8ffdc5d8f8e11366a619769
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
* lisp/treesit.el (treesit-node-at): Add LARGEST argument.
---
lisp/treesit.el | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 2fe48b34aa..5381cccc8e 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -101,7 +101,7 @@ Return the root node of the syntax tree."
(treesit-parser-language
(treesit-node-parser node)))
-(defun treesit-node-at (pos &optional parser-or-lang named)
+(defun treesit-node-at (pos &optional parser-or-lang named largest)
"Return the smallest node that starts at or after buffer position POS.
\"Starts at or after POS\" means the start of the node is greater or
@@ -112,7 +112,10 @@ Return nil if none find. If NAMED is 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
that parser; if PARSER-OR-LANG is a language, find a parser using
-that language in the current buffer, and use that."
+that language in the current buffer, and use that.
+
+If LARGEST is non-nil, return the largest node instead of the
+smallest."
(let ((node (if (treesit-parser-p parser-or-lang)
(treesit-parser-root-node parser-or-lang)
(treesit-buffer-root-node parser-or-lang)))
@@ -121,7 +124,11 @@ that language in the current buffer, and use that."
(while (setq next (treesit-node-first-child-for-pos
node pos named))
(setq node next))
- node))
+ (if (not largest)
+ node
+ (treesit-parent-while
+ node (lambda (n) (eq (treesit-node-start n)
+ (treesit-node-start node)))))))
(defun treesit-node-on (beg end &optional parser-or-lang named)
"Return the smallest node covering BEG to END.