(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
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)))
(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.