Skip to content

Commit

Permalink
feat: cache aerial tree-sitter queries (#325)
Browse files Browse the repository at this point in the history
When moving away from nvim-treesitter helpers, I overlooked the caching
they were performing. This means that currently on every symbols
refetch, neovim scans filesystem and gets all queries. Definitely useful
when writing new queries, but slow nonetheless.

This commit introduces a simple query cache and an API method to clear
said cache. With this, performance should improve, yet a way to iterate
over query design remains accessible.
  • Loading branch information
Slotos committed Nov 12, 2023
1 parent 3b89343 commit 51bdd35
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ hi AerialGuide2 guifg=Blue
- [nav_open()](doc/api.md#nav_open)
- [nav_close()](doc/api.md#nav_close)
- [nav_toggle()](doc/api.md#nav_toggle)
- [treesitter_clear_query_cache()](doc/api.md#treesitter_clear_query_cache)
- [sync_folds(bufnr)](doc/api.md#sync_foldsbufnr)
- [info()](doc/api.md#info)
- [num_symbols(bufnr)](doc/api.md#num_symbolsbufnr)
Expand Down
4 changes: 4 additions & 0 deletions doc/aerial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@ nav_toggle() *aerial.nav_toggl
Toggle the nav windows open/closed


treesitter_clear_query_cache() *aerial.treesitter_clear_query_cache*
Clear aerial's tree-sitter query cache


sync_folds({bufnr}) *aerial.sync_folds*
Sync code folding with the current tree state.

Expand Down
7 changes: 7 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [nav_open()](#nav_open)
- [nav_close()](#nav_close)
- [nav_toggle()](#nav_toggle)
- [treesitter_clear_query_cache()](#treesitter_clear_query_cache)
- [sync_folds(bufnr)](#sync_foldsbufnr)
- [info()](#info)
- [num_symbols(bufnr)](#num_symbolsbufnr)
Expand Down Expand Up @@ -326,6 +327,12 @@ Close the nav windows
Toggle the nav windows open/closed


## treesitter_clear_query_cache()

`treesitter_clear_query_cache()` \
Clear aerial's tree-sitter query cache


## sync_folds(bufnr)

`sync_folds(bufnr)` \
Expand Down
21 changes: 19 additions & 2 deletions lua/aerial/backends/treesitter/helpers.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
local M = {}
local query_cache = {}

--@note Clear query cache, forcing reload
M.clear_query_cache = function()
query_cache = {}
end

---@param start_node TSNode
---@param end_node TSNode
Expand Down Expand Up @@ -39,18 +45,29 @@ end
if vim.treesitter.query.get == nil then
---@param lang string
---@return Query|nil
M.get_query = function(lang)
M.load_query = function(lang)
---@diagnostic disable-next-line: deprecated
return vim.treesitter.query.get_query(lang, "aerial")
end
else
---@param lang string
---@return Query|nil
M.get_query = function(lang)
M.load_query = function(lang)
return vim.treesitter.query.get(lang, "aerial")
end
end

---@param lang string
---@return Query|nil
---@note caches queries to avoid filesystem hits on neovim 0.9+
M.get_query = function(lang)
if not query_cache[lang] then
query_cache[lang] = { query = M.load_query(lang) }
end

return query_cache[lang].query
end

---@param lang string
---@return boolean
M.has_parser = function(lang)
Expand Down
3 changes: 3 additions & 0 deletions lua/aerial/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ M.nav_close = lazy("nav_view", "close")
---Toggle the nav windows open/closed
M.nav_toggle = lazy("nav_view", "toggle")

---Clear aerial's tree-sitter query cache
M.treesitter_clear_query_cache = lazy("backends.treesitter.helpers", "clear_query_cache")

---Sync code folding with the current tree state.
---@param bufnr? integer
---@note
Expand Down

0 comments on commit 51bdd35

Please sign in to comment.