aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/query.lua
diff options
context:
space:
mode:
authorRiley Bruins <ribru17@hotmail.com>2025-01-11 15:44:07 -0800
committerChristian Clason <ch.clason+github@icloud.com>2025-01-12 16:44:24 +0100
commit3fdc4302415972eb5d98ba832372236be3d22572 (patch)
tree9993ceaf881d0c6fb77f7a39c1cca3e6fc6a3826 /runtime/lua/vim/treesitter/query.lua
parent40bf23adaf98dc357a59f9524a16e06f990faeaa (diff)
downloadrneovim-3fdc4302415972eb5d98ba832372236be3d22572.tar.gz
rneovim-3fdc4302415972eb5d98ba832372236be3d22572.tar.bz2
rneovim-3fdc4302415972eb5d98ba832372236be3d22572.zip
perf(treesitter): cache queries strongly
**Problem:** Query parsing uses a weak cache which is invalidated frequently **Solution:** Make the cache strong, and invalidate it manually when necessary (that is, when `rtp` is changed or `query.set()` is called) Co-authored-by: Christian Clason <c.clason@uni-graz.at>
Diffstat (limited to 'runtime/lua/vim/treesitter/query.lua')
-rw-r--r--runtime/lua/vim/treesitter/query.lua13
1 files changed, 11 insertions, 2 deletions
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua
index b9bcbe9a80..b0b0fecd38 100644
--- a/runtime/lua/vim/treesitter/query.lua
+++ b/runtime/lua/vim/treesitter/query.lua
@@ -262,6 +262,7 @@ local explicit_queries = setmetatable({}, {
---@param query_name string Name of the query (e.g., "highlights")
---@param text string Query text (unparsed).
function M.set(lang, query_name, text)
+ M.get:clear(lang, query_name)
explicit_queries[lang][query_name] = M.parse(lang, text)
end
@@ -284,7 +285,15 @@ M.get = memoize('concat-2', function(lang, query_name)
end
return M.parse(lang, query_string)
-end)
+end, false)
+
+api.nvim_create_autocmd('OptionSet', {
+ pattern = { 'runtimepath' },
+ group = api.nvim_create_augroup('ts_query_cache_reset', { clear = true }),
+ callback = function()
+ M.get:clear()
+ end,
+})
--- Parses a {query} string and returns a `Query` object (|lua-treesitter-query|), which can be used
--- to search the tree for the query patterns (via |Query:iter_captures()|, |Query:iter_matches()|),
@@ -316,7 +325,7 @@ M.parse = memoize('concat-2', function(lang, query)
assert(language.add(lang))
local ts_query = vim._ts_parse_query(lang, query)
return Query.new(lang, ts_query)
-end)
+end, false)
--- Implementations of predicates that can optionally be prefixed with "any-".
---