1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 14:30:50 -08:00

Make treesit-node-at/on guess language at point

If PARSER-OR-LANG is nil, it makes more sense to guess the language at
point by treesit-language-at than to simply use the first parser in
the parser list.

* doc/lispref/parsing.texi (Retrieving Nodes): Update manual.
* lisp/treesit.el (treesit-node-at)
(treesit-node-on): Guess language at point.  Update docstring.
(treesit-buffer-root-node): Update docstring.
This commit is contained in:
Yuan Fu 2022-12-24 15:31:03 -08:00
parent 7f7def2ae6
commit 35c2ca2ca6
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 34 additions and 28 deletions

View file

@ -576,12 +576,12 @@ leaf node after @var{pos}.
Finally, if there is no leaf node after @var{pos}, return the first Finally, if there is no leaf node after @var{pos}, return the first
leaf node before @var{pos}. leaf node before @var{pos}.
When @var{parser-or-lang} is @code{nil} or omitted, this function uses If @var{parser-or-lang} is a parser object, this function uses that
the first parser in @code{(treesit-parser-list)} of the current parser; if @var{parser-or-lang} is a language, this function uses the
buffer. If @var{parser-or-lang} is a parser object, it uses that first parser for that language in the current buffer, or creates one
parser; if @var{parser-or-lang} is a language, it finds the first if none exists; if @var{parser-or-lang} is @code{nil}, this function
parser using that language in @code{(treesit-parser-list)}, and uses tries to guess the language at @var{pos} by
that. @code{treesit-language-at}.
If this function cannot find a suitable node to return, it returns If this function cannot find a suitable node to return, it returns
@code{nil}. @code{nil}.
@ -610,13 +610,14 @@ is at or after @var{end}.
inside any top-level construct (function definition, etc.) most inside any top-level construct (function definition, etc.) most
probably will give you the root node, because the root node is the probably will give you the root node, because the root node is the
smallest node that covers that empty line. Most of the time, you want smallest node that covers that empty line. Most of the time, you want
to use @code{treesit-node-at}, described above, instead. to use @code{treesit-node-at} instead.
When @var{parser-or-lang} is @code{nil}, this function uses the first If @var{parser-or-lang} is a parser object, this function uses that
parser in @code{(treesit-parser-list)} of the current buffer. If parser; if @var{parser-or-lang} is a language, this function uses the
@var{parser-or-lang} is a parser object, it uses that parser; if first parser for that language in the current buffer, or creates one
@var{parser-or-lang} is a language, it finds the first parser using if none exists; if @var{parser-or-lang} is @code{nil}, this function
that language in @code{(treesit-parser-list)}, and uses that. tries to guess the language at @var{beg} by
@code{treesit-language-at}.
If @var{named} is non-@code{nil}, this function looks for a named node If @var{named} is non-@code{nil}, this function looks for a named node
only (@pxref{tree-sitter named node, named node}). only (@pxref{tree-sitter named node, named node}).
@ -628,9 +629,10 @@ This function returns the root node of the syntax tree generated by
@end defun @end defun
@defun treesit-buffer-root-node &optional language @defun treesit-buffer-root-node &optional language
This function finds the first parser that uses @var{language} in This function finds the first parser for @var{language} in the current
@code{(treesit-parser-list)} of the current buffer, and returns the buffer, or creates one if none exists, and returns the root node
root node generated by that parser. If it cannot find an appropriate generated by that parser. If @var{language} is omitted, it uses the
first parser in the parser list. If it cannot find an appropriate
parser, it returns @code{nil}. parser, it returns @code{nil}.
@end defun @end defun

View file

@ -171,13 +171,15 @@ before POS.
Return nil if no leaf node can be returned. If NAMED is non-nil, Return nil if no leaf node can be returned. If NAMED is non-nil,
only look for named nodes. only look for named nodes.
If PARSER-OR-LANG is nil, use the first parser in If PARSER-OR-LANG is a parser, use that parser; if PARSER-OR-LANG
`treesit-parser-list'; if PARSER-OR-LANG is a parser, use is a language, find the first parser for that language in the
that parser; if PARSER-OR-LANG is a language, find a parser using current buffer, or create one if none exists; If PARSER-OR-LANG
that language in the current buffer, and use that." is nil, try to guess the language at POS by
`treesit-language-at'."
(let* ((root (if (treesit-parser-p parser-or-lang) (let* ((root (if (treesit-parser-p parser-or-lang)
(treesit-parser-root-node parser-or-lang) (treesit-parser-root-node parser-or-lang)
(treesit-buffer-root-node parser-or-lang))) (treesit-buffer-root-node
(or parser-or-lang (treesit-language-at pos)))))
(node root) (node root)
(node-before root) (node-before root)
(pos-1 (max (1- pos) (point-min))) (pos-1 (max (1- pos) (point-min)))
@ -219,13 +221,15 @@ to use `treesit-node-at' instead.
Return nil if none was found. If NAMED is non-nil, only look for Return nil if none was found. If NAMED is non-nil, only look for
named node. named node.
If PARSER-OR-LANG is nil, use the first parser in If PARSER-OR-LANG is a parser, use that parser; if PARSER-OR-LANG
`treesit-parser-list'; if PARSER-OR-LANG is a parser, use is a language, find the first parser for that language in the
that parser; if PARSER-OR-LANG is a language, find a parser using current buffer, or create one if none exists; If PARSER-OR-LANG
that language in the current buffer, and use that." is nil, try to guess the language at BEG by
`treesit-language-at'."
(let ((root (if (treesit-parser-p parser-or-lang) (let ((root (if (treesit-parser-p parser-or-lang)
(treesit-parser-root-node parser-or-lang) (treesit-parser-root-node parser-or-lang)
(treesit-buffer-root-node parser-or-lang)))) (treesit-buffer-root-node
(or parser-or-lang (treesit-language-at beg))))))
(treesit-node-descendant-for-range root beg (or end beg) named))) (treesit-node-descendant-for-range root beg (or end beg) named)))
(defun treesit-node-top-level (node &optional type) (defun treesit-node-top-level (node &optional type)
@ -246,10 +250,10 @@ regexp, rather than using NODE's type."
(defun treesit-buffer-root-node (&optional language) (defun treesit-buffer-root-node (&optional language)
"Return the root node of the current buffer. "Return the root node of the current buffer.
Use the first parser in `treesit-parser-list'.
If optional argument LANGUAGE is non-nil, use the first parser Use the first parser in the parser list if LANGUAGE is omitted.
for LANGUAGE." If LANGUAGE is non-nil, use the first parser for LANGUAGE in the
parser list, or create one if none exists."
(if-let ((parser (if-let ((parser
(if language (if language
(treesit-parser-create language) (treesit-parser-create language)