From ffd648dca62156d07d16d34c8d605eac59e7d822 Mon Sep 17 00:00:00 2001 From: Yoav Marco Date: Tue, 10 May 2022 14:04:34 +0300 Subject: [PATCH] Reuse queries in a dumb way --- src/treesit.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/treesit.c b/src/treesit.c index 91114b0..490791a 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -1491,8 +1491,22 @@ DEFUN ("treesit-query-capture", querying with the same query can reuse the query object. It also saves us from expanding the sexp query into a string. I don't know how much time that could save though. */ - TSQuery *ts_query = ts_query_new (lang, source, strlen (source), - &error_offset, &error_type); + static TSQuery *ts_query = NULL; + static char* prev_source = NULL; + if (source != prev_source) + { + printf ("Making query\n"); + if (query) + ts_query_delete (ts_query); + ts_query = ts_query_new (lang, source, strlen (source), + &error_offset, &error_type); + } + else + { + printf ("Reusing query\n"); + } + + prev_source = source; TSQueryCursor *cursor = ts_query_cursor_new (); if (ts_query == NULL) @@ -1555,7 +1569,6 @@ DEFUN ("treesit-query-capture", result = prev_result; } } - ts_query_delete (ts_query); ts_query_cursor_delete (cursor); return Fnreverse (result); } -- 2.35.3