summaryrefslogtreecommitdiff
path: root/src/treesit.h
diff options
context:
space:
mode:
authorYuan Fu <casouri@gmail.com>2022-10-11 09:30:42 -0700
committerYuan Fu <casouri@gmail.com>2022-10-11 09:30:42 -0700
commitc2ecb08775dc24618de507d2d1ce0f9b0debe17e (patch)
tree6f91be66864feeb2dcba92ff85d212a7fb147830 /src/treesit.h
parent57e37e9128b4f6f9a2aae0bc25de6c208d58e5d0 (diff)
downloademacs-c2ecb08775dc24618de507d2d1ce0f9b0debe17e.tar.gz
Lazily compile tree-sitter query
See comment on struct Lisp_TS_Query for why. Previous commits adding python and js support for tree-sitter breaks Emacs build if language definitions aren't available at build time. This commit fixes that. Now query object don't compile the query upon creation, but stores the query source and language symbol, and compiles upon first use (in treesit-query-capture). I want ts_ensure_query_compiled to return signal symbol and data rather than signaling itself, because it's a helper function not lisp function. But because it calls ts_load_language, I had to update ts_load_language to also use the signal symbol/data interface. * src/treesit.h (struct Lisp_TS_Query): Add two new field. * src/treesit.c (ts_load_language): Return signal symbol and data rather than signaling itself. (Ftreesit_langauge_available_p) (Ftreesit_parser_create): Update usage of ts_load_language (make_ts_query): Now returns a lisp object. (ts_query_error_to_string): Moved because it's used by ts_compose_query_signal_data. (ts_compose_query_signal_data) (ts_ensure_query_compiled): New functions. (Ftreesit_query_compile): Delay compiling the query. (Ftreesit_query_capture): Instead of creating a query object which compiles the query, now goes two routes: if QUERY is a query object, make sure it is compiled and use the TSQuery; if QUERY is a cons or string, compile directly to TSQuery, and free it after use. Creating a lisp query requires the language symbol, but in this function we only have TSLanguage.
Diffstat (limited to 'src/treesit.h')
-rw-r--r--src/treesit.h20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/treesit.h b/src/treesit.h
index 0c043f7d250..20e7cd4107c 100644
--- a/src/treesit.h
+++ b/src/treesit.h
@@ -84,11 +84,27 @@ struct Lisp_TS_Node
ptrdiff_t timestamp;
};
-/* A compiled tree-sitter query. */
+/* A compiled tree-sitter query.
+
+ When we create a query object by treesit-compile-query, it is not
+ immediately compiled, because that would require the language
+ definition to be loaded. For example, python.el contains
+
+ (defvar xxx (treesit-compile-query ...))
+
+ and (require 'python.el) requires python's language definition to
+ be available. In the case of python.el, Emacs requires it when
+ building, so that breaks the build. */
struct Lisp_TS_Query
{
union vectorlike_header header;
- /* Pointer to the query object. */
+ /* Language symbol for the query. */
+ Lisp_Object language;
+ /* Source lisp (sexp or string) query. */
+ Lisp_Object source;
+ /* Pointer to the query object. This can be NULL, meaning this
+ query is not initialized/compiled. We compile the query when
+ it is used the first time (in treesit-query-capture). */
TSQuery *query;
/* Pointer to a cursor. If we are storing the query object, we
might as well store a cursor, too. */