From: Yuan Fu Date: Mon, 6 Feb 2023 04:22:52 +0000 (-0800) Subject: Add 'live' property to treesit-node-check (bug#61235) X-Git-Tag: emacs-29.0.90~460 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=51901736965;p=emacs.git Add 'live' property to treesit-node-check (bug#61235) * doc/lispref/parsing.texi (Accessing Node Information): Document. * src/treesit.c (treesit_parser_live_p): New function. (Ftreesit_node_check): Add 'live' property. * test/src/treesit-tests.el (treesit-node-api): Add tests. --- diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index cebb59b6501..6e13af9a20f 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi @@ -970,10 +970,15 @@ A node ``has error'' if the text it spans contains a syntax error. It can be that the node itself has an error, or one of its descendants has an error. +@cindex tree-sitter live node +@cindex live node, tree-sitter +A node is ``live'' if its parser is not deleted, and the buffer it +belongs to is live. + @defun treesit-node-check node property This function checks if @var{node} has the specified @var{property}. @var{property} can be @code{named}, @code{missing}, @code{extra}, -@code{outdated}, or @code{has-error}. +@code{outdated}, @code{has-error}, or @code{live}. @end defun @defun treesit-node-type node diff --git a/src/treesit.c b/src/treesit.c index cfa3721b5e7..01c7621c6ea 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -1475,6 +1475,15 @@ This symbol is the one used to create the parser. */) return XTS_PARSER (parser)->language_symbol; } +/* Return true if PARSER is not deleted and its buffer is live. */ +static bool +treesit_parser_live_p (Lisp_Object parser) +{ + CHECK_TS_PARSER (parser); + return ((!XTS_PARSER (parser)->deleted) && + (!NILP (Fbuffer_live_p (XTS_PARSER (parser)->buffer)))); +} + /*** Parser API */ DEFUN ("treesit-parser-root-node", @@ -1908,7 +1917,8 @@ DEFUN ("treesit-node-check", Ftreesit_node_check, Streesit_node_check, 2, 2, 0, doc: /* Return non-nil if NODE has PROPERTY, nil otherwise. -PROPERTY could be `named', `missing', `extra', `outdated', or `has-error'. +PROPERTY could be `named', `missing', `extra', `outdated', +`has-error', or `live'. Named nodes correspond to named rules in the language definition, whereas "anonymous" nodes correspond to string literals in the @@ -1924,7 +1934,10 @@ A node is "outdated" if the parser has reparsed at least once after the node was created. A node "has error" if itself is a syntax error or contains any syntax -errors. */) +errors. + +A node is "live" if its parser is not deleted and its buffer is +live. */) (Lisp_Object node, Lisp_Object property) { if (NILP (node)) return Qnil; @@ -1947,9 +1960,11 @@ errors. */) result = ts_node_is_extra (treesit_node); else if (EQ (property, Qhas_error)) result = ts_node_has_error (treesit_node); + else if (EQ (property, Qlive)) + result = treesit_parser_live_p (XTS_NODE (node)->parser); else signal_error ("Expecting `named', `missing', `extra', " - "`outdated', or `has-error', but got", + "`outdated', `has-error', or `live', but got", property); return result ? Qt : Qnil; } @@ -3448,6 +3463,7 @@ syms_of_treesit (void) DEFSYM (Qextra, "extra"); DEFSYM (Qoutdated, "outdated"); DEFSYM (Qhas_error, "has-error"); + DEFSYM (Qlive, "live"); DEFSYM (QCanchor, ":anchor"); DEFSYM (QCequal, ":equal"); diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el index a2ab3be7cd8..5aa12e8aa0e 100644 --- a/test/src/treesit-tests.el +++ b/test/src/treesit-tests.el @@ -100,6 +100,7 @@ (should (eq nil (treesit-node-check root-node 'missing))) (should (eq nil (treesit-node-check root-node 'extra))) (should (eq nil (treesit-node-check root-node 'has-error))) + (should (eq t (treesit-node-check root-node 'live))) ;; `treesit-node-child'. (setq doc-node (treesit-node-child root-node 0)) (should (equal "array" (treesit-node-type doc-node))) @@ -160,7 +161,18 @@ :type 'args-out-of-range) ;; `treesit-node-eq'. (should (treesit-node-eq root-node root-node)) - (should (not (treesit-node-eq root-node doc-node)))))) + (should (not (treesit-node-eq root-node doc-node))) + + ;; Further test for `treesit-node-check'. + (treesit-parser-delete parser) + (should (equal nil (treesit-node-check root-node 'live))) + ;; Recreate parser. + (setq parser (treesit-parser-create 'json)) + (setq root-node (treesit-parser-root-node + parser)) + (should (equal t (treesit-node-check root-node 'live))) + (kill-buffer) + (should (equal nil (treesit-node-check root-node 'live)))))) ;;; Indirect buffer