mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-14 01:10:02 -04: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:
parent
d169c0614c
commit
d25549917d
2 changed files with 11 additions and 15 deletions
|
|
@ -9,12 +9,6 @@ local M = {}
|
||||||
-- this is the main interface through the plugin
|
-- this is the main interface through the plugin
|
||||||
function M.setup(lang)
|
function M.setup(lang)
|
||||||
if parsers.has_parser(lang) then
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,18 +10,14 @@ local M = {
|
||||||
locals={}
|
locals={}
|
||||||
}
|
}
|
||||||
|
|
||||||
function M.is_supported(lang)
|
|
||||||
return queries.get_query(lang, "locals") ~= nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.collect_locals(bufnr)
|
function M.collect_locals(bufnr)
|
||||||
local ft = api.nvim_buf_get_option(bufnr, "ft")
|
local ft = api.nvim_buf_get_option(bufnr, "ft")
|
||||||
|
|
||||||
if not ft then return end
|
if not ft then return end
|
||||||
|
|
||||||
local query = queries.get_query(ft, 'locals')
|
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
|
if not parser then return end
|
||||||
|
|
||||||
local root = parser:parse():root()
|
local root = parser:parse():root()
|
||||||
|
|
@ -36,12 +32,18 @@ function M.collect_locals(bufnr)
|
||||||
return locals
|
return locals
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.on_lines(_, buf, _, firstline, lastline, new_lastline)
|
local function update_cached_locals(bufnr, changed_tick)
|
||||||
M.locals[buf] = M.collect_locals(buf)
|
M.locals[bufnr] = {tick=changed_tick, cache=( M.collect_locals(bufnr) or {} )}
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_locals(bufnr)
|
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
|
end
|
||||||
|
|
||||||
function M.get_definitions(bufnr)
|
function M.get_definitions(bufnr)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue