static Lisp_Object
make_treesit_query (Lisp_Object query, Lisp_Object language)
{
- TSQueryCursor *treesit_cursor = ts_query_cursor_new ();
struct Lisp_TS_Query *lisp_query;
lisp_query = ALLOCATE_PSEUDOVECTOR (struct Lisp_TS_Query,
lisp_query->language = language;
lisp_query->source = query;
lisp_query->query = NULL;
- lisp_query->cursor = treesit_cursor;
+ lisp_query->cursor = NULL;
return make_lisp_ptr (lisp_query, Lisp_Vectorlike);
}
build_string ("Debug the query with `treesit-query-validate'"));
}
+/* Ensure QUERY has a non-NULL cursor, and return it. */
+static TSQueryCursor *
+treesit_ensure_query_cursor (Lisp_Object query)
+{
+ if (!XTS_COMPILED_QUERY (query)->cursor)
+ XTS_COMPILED_QUERY (query)->cursor = ts_query_cursor_new ();
+
+ return XTS_COMPILED_QUERY (query)->cursor;
+}
+
/* Ensure the QUERY is compiled. Return the TSQuery. It could be
NULL if error occurs, in which case ERROR_OFFSET and ERROR_TYPE are
bound. If error occurs, return NULL, and assign SIGNAL_SYMBOL and
{
*ts_query = treesit_ensure_query_compiled (query, signal_symbol,
signal_data);
- *cursor = XTS_COMPILED_QUERY (query)->cursor;
+ *cursor = treesit_ensure_query_cursor (query);
/* We don't need to free ts_query and cursor because they
are stored in a lisp object, which is tracked by gc. */
*need_free = false;
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). */
+ /* 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. (See treesit_ensure_query_compiled.) */
TSQuery *query;
- /* Pointer to a cursor. If we are storing the query object, we
- might as well store a cursor, too. */
+ /* Pointer to a cursor. If we are storing the query object, we might
+ as well store a cursor, too. This can be NULL; caller should use
+ treesit_ensure_query_cursor to access the cursor. We made cursor
+ to be NULL-able because it makes dumping and loading queries
+ easy. */
TSQueryCursor *cursor;
};