diff options
| author | Yuan Fu | 2022-12-24 15:31:03 -0800 |
|---|---|---|
| committer | Yuan Fu | 2022-12-24 18:43:03 -0800 |
| commit | 35c2ca2ca64070f6ebc75011e5e6e2d688124bec (patch) | |
| tree | a44c4da06fa49b33ce2369a987318b347f6228c7 | |
| parent | 7f7def2ae62c80fa2fd0c73087b59060b303c230 (diff) | |
| download | emacs-35c2ca2ca64070f6ebc75011e5e6e2d688124bec.tar.gz emacs-35c2ca2ca64070f6ebc75011e5e6e2d688124bec.zip | |
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.
| -rw-r--r-- | doc/lispref/parsing.texi | 32 | ||||
| -rw-r--r-- | lisp/treesit.el | 30 |
2 files changed, 34 insertions, 28 deletions
diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index adb4c5e6e0c..e213363298d 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi | |||
| @@ -576,12 +576,12 @@ leaf node after @var{pos}. | |||
| 576 | Finally, if there is no leaf node after @var{pos}, return the first | 576 | Finally, if there is no leaf node after @var{pos}, return the first |
| 577 | leaf node before @var{pos}. | 577 | leaf node before @var{pos}. |
| 578 | 578 | ||
| 579 | When @var{parser-or-lang} is @code{nil} or omitted, this function uses | 579 | If @var{parser-or-lang} is a parser object, this function uses that |
| 580 | the first parser in @code{(treesit-parser-list)} of the current | 580 | parser; if @var{parser-or-lang} is a language, this function uses the |
| 581 | buffer. If @var{parser-or-lang} is a parser object, it uses that | 581 | first parser for that language in the current buffer, or creates one |
| 582 | parser; if @var{parser-or-lang} is a language, it finds the first | 582 | if none exists; if @var{parser-or-lang} is @code{nil}, this function |
| 583 | parser using that language in @code{(treesit-parser-list)}, and uses | 583 | tries to guess the language at @var{pos} by |
| 584 | that. | 584 | @code{treesit-language-at}. |
| 585 | 585 | ||
| 586 | If this function cannot find a suitable node to return, it returns | 586 | If this function cannot find a suitable node to return, it returns |
| 587 | @code{nil}. | 587 | @code{nil}. |
| @@ -610,13 +610,14 @@ is at or after @var{end}. | |||
| 610 | inside any top-level construct (function definition, etc.) most | 610 | inside any top-level construct (function definition, etc.) most |
| 611 | probably will give you the root node, because the root node is the | 611 | probably will give you the root node, because the root node is the |
| 612 | smallest node that covers that empty line. Most of the time, you want | 612 | smallest node that covers that empty line. Most of the time, you want |
| 613 | to use @code{treesit-node-at}, described above, instead. | 613 | to use @code{treesit-node-at} instead. |
| 614 | 614 | ||
| 615 | When @var{parser-or-lang} is @code{nil}, this function uses the first | 615 | If @var{parser-or-lang} is a parser object, this function uses that |
| 616 | parser in @code{(treesit-parser-list)} of the current buffer. If | 616 | parser; if @var{parser-or-lang} is a language, this function uses the |
| 617 | @var{parser-or-lang} is a parser object, it uses that parser; if | 617 | first parser for that language in the current buffer, or creates one |
| 618 | @var{parser-or-lang} is a language, it finds the first parser using | 618 | if none exists; if @var{parser-or-lang} is @code{nil}, this function |
| 619 | that language in @code{(treesit-parser-list)}, and uses that. | 619 | tries to guess the language at @var{beg} by |
| 620 | @code{treesit-language-at}. | ||
| 620 | 621 | ||
| 621 | If @var{named} is non-@code{nil}, this function looks for a named node | 622 | If @var{named} is non-@code{nil}, this function looks for a named node |
| 622 | only (@pxref{tree-sitter named node, named node}). | 623 | only (@pxref{tree-sitter named node, named node}). |
| @@ -628,9 +629,10 @@ This function returns the root node of the syntax tree generated by | |||
| 628 | @end defun | 629 | @end defun |
| 629 | 630 | ||
| 630 | @defun treesit-buffer-root-node &optional language | 631 | @defun treesit-buffer-root-node &optional language |
| 631 | This function finds the first parser that uses @var{language} in | 632 | This function finds the first parser for @var{language} in the current |
| 632 | @code{(treesit-parser-list)} of the current buffer, and returns the | 633 | buffer, or creates one if none exists, and returns the root node |
| 633 | root node generated by that parser. If it cannot find an appropriate | 634 | generated by that parser. If @var{language} is omitted, it uses the |
| 635 | first parser in the parser list. If it cannot find an appropriate | ||
| 634 | parser, it returns @code{nil}. | 636 | parser, it returns @code{nil}. |
| 635 | @end defun | 637 | @end defun |
| 636 | 638 | ||
diff --git a/lisp/treesit.el b/lisp/treesit.el index 3d9c61b9dc9..2b30da4be7a 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -171,13 +171,15 @@ before POS. | |||
| 171 | Return nil if no leaf node can be returned. If NAMED is non-nil, | 171 | Return nil if no leaf node can be returned. If NAMED is non-nil, |
| 172 | only look for named nodes. | 172 | only look for named nodes. |
| 173 | 173 | ||
| 174 | If PARSER-OR-LANG is nil, use the first parser in | 174 | If PARSER-OR-LANG is a parser, use that parser; if PARSER-OR-LANG |
| 175 | `treesit-parser-list'; if PARSER-OR-LANG is a parser, use | 175 | is a language, find the first parser for that language in the |
| 176 | that parser; if PARSER-OR-LANG is a language, find a parser using | 176 | current buffer, or create one if none exists; If PARSER-OR-LANG |
| 177 | that language in the current buffer, and use that." | 177 | is nil, try to guess the language at POS by |
| 178 | `treesit-language-at'." | ||
| 178 | (let* ((root (if (treesit-parser-p parser-or-lang) | 179 | (let* ((root (if (treesit-parser-p parser-or-lang) |
| 179 | (treesit-parser-root-node parser-or-lang) | 180 | (treesit-parser-root-node parser-or-lang) |
| 180 | (treesit-buffer-root-node parser-or-lang))) | 181 | (treesit-buffer-root-node |
| 182 | (or parser-or-lang (treesit-language-at pos))))) | ||
| 181 | (node root) | 183 | (node root) |
| 182 | (node-before root) | 184 | (node-before root) |
| 183 | (pos-1 (max (1- pos) (point-min))) | 185 | (pos-1 (max (1- pos) (point-min))) |
| @@ -219,13 +221,15 @@ to use `treesit-node-at' instead. | |||
| 219 | Return nil if none was found. If NAMED is non-nil, only look for | 221 | Return nil if none was found. If NAMED is non-nil, only look for |
| 220 | named node. | 222 | named node. |
| 221 | 223 | ||
| 222 | If PARSER-OR-LANG is nil, use the first parser in | 224 | If PARSER-OR-LANG is a parser, use that parser; if PARSER-OR-LANG |
| 223 | `treesit-parser-list'; if PARSER-OR-LANG is a parser, use | 225 | is a language, find the first parser for that language in the |
| 224 | that parser; if PARSER-OR-LANG is a language, find a parser using | 226 | current buffer, or create one if none exists; If PARSER-OR-LANG |
| 225 | that language in the current buffer, and use that." | 227 | is nil, try to guess the language at BEG by |
| 228 | `treesit-language-at'." | ||
| 226 | (let ((root (if (treesit-parser-p parser-or-lang) | 229 | (let ((root (if (treesit-parser-p parser-or-lang) |
| 227 | (treesit-parser-root-node parser-or-lang) | 230 | (treesit-parser-root-node parser-or-lang) |
| 228 | (treesit-buffer-root-node parser-or-lang)))) | 231 | (treesit-buffer-root-node |
| 232 | (or parser-or-lang (treesit-language-at beg)))))) | ||
| 229 | (treesit-node-descendant-for-range root beg (or end beg) named))) | 233 | (treesit-node-descendant-for-range root beg (or end beg) named))) |
| 230 | 234 | ||
| 231 | (defun treesit-node-top-level (node &optional type) | 235 | (defun treesit-node-top-level (node &optional type) |
| @@ -246,10 +250,10 @@ regexp, rather than using NODE's type." | |||
| 246 | 250 | ||
| 247 | (defun treesit-buffer-root-node (&optional language) | 251 | (defun treesit-buffer-root-node (&optional language) |
| 248 | "Return the root node of the current buffer. | 252 | "Return the root node of the current buffer. |
| 249 | Use the first parser in `treesit-parser-list'. | ||
| 250 | 253 | ||
| 251 | If optional argument LANGUAGE is non-nil, use the first parser | 254 | Use the first parser in the parser list if LANGUAGE is omitted. |
| 252 | for LANGUAGE." | 255 | If LANGUAGE is non-nil, use the first parser for LANGUAGE in the |
| 256 | parser list, or create one if none exists." | ||
| 253 | (if-let ((parser | 257 | (if-let ((parser |
| 254 | (if language | 258 | (if language |
| 255 | (treesit-parser-create language) | 259 | (treesit-parser-create language) |