From: Yuan Fu Date: Tue, 3 Dec 2024 04:14:55 +0000 (-0800) Subject: Make treesit-query-compile compile compiled-query eagerly X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=8c96c5ef08114299c4c91926419520aacd5c008f;p=emacs.git Make treesit-query-compile compile compiled-query eagerly * src/treesit.c (treesit_ensure_query_compiled_signal): Extrat out into a function. (Ftreesit_query_compile): If EAGER is non-nil and QUERY is a lazily compiled query, compile it eagerily. (cherry picked from commit 4ecd1639b19345786020c0f7a5f3672129dae466) --- diff --git a/src/treesit.c b/src/treesit.c index 2372944e166..28c94f307c0 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -1516,6 +1516,20 @@ treesit_ensure_query_compiled (Lisp_Object query, Lisp_Object *signal_symbol, return treesit_query; } +/* Bsically treesit_ensure_query_compiled but can signal. */ +static +void treesit_ensure_query_compiled_signal (Lisp_Object lisp_query) +{ + Lisp_Object signal_symbol = Qnil; + Lisp_Object signal_data = Qnil; + TSQuery *treesit_query = treesit_ensure_query_compiled (lisp_query, + &signal_symbol, + &signal_data); + + if (treesit_query == NULL) + xsignal (signal_symbol, signal_data); +} + /* Resolve language symbol LANG according to treesit-language-remap-alist. */ static @@ -3051,6 +3065,8 @@ DEFUN ("treesit-query-compile", doc: /* Compile QUERY to a compiled query. Querying with a compiled query is much faster than an uncompiled one. +So it's a good idea to use compiled query in tight loops, etc. + LANGUAGE is the language this query is for. If EAGER is non-nil, immediately load LANGUAGE and compile the query. @@ -3064,11 +3080,17 @@ You can use `treesit-query-validate' to validate and debug a query. */) if (NILP (Ftreesit_query_p (query))) wrong_type_argument (Qtreesit_query_p, query); CHECK_SYMBOL (language); - if (TS_COMPILED_QUERY_P (query)) - return query; treesit_initialize (); + if (TS_COMPILED_QUERY_P (query)) + { + if (NILP (eager)) + return query; + treesit_ensure_query_compiled_signal (query); + return query; + } + Lisp_Object lisp_query = make_treesit_query (query, language); /* Maybe actually compile. */ @@ -3076,15 +3098,7 @@ You can use `treesit-query-validate' to validate and debug a query. */) return lisp_query; else { - Lisp_Object signal_symbol = Qnil; - Lisp_Object signal_data = Qnil; - TSQuery *treesit_query = treesit_ensure_query_compiled (lisp_query, - &signal_symbol, - &signal_data); - - if (treesit_query == NULL) - xsignal (signal_symbol, signal_data); - + treesit_ensure_query_compiled_signal (lisp_query); return lisp_query; } }