mirror of
https://github.com/chenasraf/nvim-treesitter.git
synced 2026-05-18 01:39:00 +00:00
perf: don't compute locals on buffer updates
Instead we lazily evaluate them on request.
This allow two things :
* better performances
* being sure the locas are up to date
This commit is contained in:
@@ -9,12 +9,6 @@ local M = {}
|
||||
-- this is the main interface through the plugin
|
||||
function M.setup(lang)
|
||||
if parsers.has_parser(lang) then
|
||||
if locals.is_supported(lang) then
|
||||
print("Locals setup for", lang)
|
||||
api.nvim_command(string.format([[
|
||||
autocmd NvimTreesitter FileType %s lua vim.api.nvim_buf_attach(0, true, {on_lines=require'nvim-treesitter.locals'.on_lines})
|
||||
]], lang))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -10,18 +10,14 @@ local M = {
|
||||
locals={}
|
||||
}
|
||||
|
||||
function M.is_supported(lang)
|
||||
return queries.get_query(lang, "locals") ~= nil
|
||||
end
|
||||
|
||||
function M.collect_locals(bufnr)
|
||||
local ft = api.nvim_buf_get_option(bufnr, "ft")
|
||||
|
||||
if not ft then return end
|
||||
|
||||
local query = queries.get_query(ft, 'locals')
|
||||
local parser = parsers.get_parser(bufnr, ft)
|
||||
if not query then return end
|
||||
|
||||
local parser = parsers.get_parser(bufnr, ft)
|
||||
if not parser then return end
|
||||
|
||||
local root = parser:parse():root()
|
||||
@@ -36,12 +32,18 @@ function M.collect_locals(bufnr)
|
||||
return locals
|
||||
end
|
||||
|
||||
function M.on_lines(_, buf, _, firstline, lastline, new_lastline)
|
||||
M.locals[buf] = M.collect_locals(buf)
|
||||
local function update_cached_locals(bufnr, changed_tick)
|
||||
M.locals[bufnr] = {tick=changed_tick, cache=( M.collect_locals(bufnr) or {} )}
|
||||
end
|
||||
|
||||
function M.get_locals(bufnr)
|
||||
return M.locals[bufnr or api.nvim_get_current_buf()] or {}
|
||||
local bufnr = bufnr or api.nvim_get_current_buf()
|
||||
local cached_local = M.locals[bufnr]
|
||||
if not cached_local or api.nvim_buf_get_changedtick(bufnr) < cached_local.tick then
|
||||
update_cached_locals(bufnr,api.nvim_buf_get_changedtick(bufnr))
|
||||
end
|
||||
|
||||
return M.locals[bufnr].cache
|
||||
end
|
||||
|
||||
function M.get_definitions(bufnr)
|
||||
|
||||
Reference in New Issue
Block a user