aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorThomas Vigouroux <tomvig38@gmail.com>2020-04-19 19:36:26 +0200
committerThomas Vigouroux <tomvig38@gmail.com>2020-04-19 20:16:03 +0200
commitd25549917de8047eb91d6f56ee2bdcd5e667d6ae (patch)
tree45970745e65c16d52bcb93db4a4a466c6f9f105c /lua
parentfix: prepare injections mechanism (diff)
downloadnvim-treesitter-d25549917de8047eb91d6f56ee2bdcd5e667d6ae.tar
nvim-treesitter-d25549917de8047eb91d6f56ee2bdcd5e667d6ae.tar.gz
nvim-treesitter-d25549917de8047eb91d6f56ee2bdcd5e667d6ae.tar.bz2
nvim-treesitter-d25549917de8047eb91d6f56ee2bdcd5e667d6ae.tar.lz
nvim-treesitter-d25549917de8047eb91d6f56ee2bdcd5e667d6ae.tar.xz
nvim-treesitter-d25549917de8047eb91d6f56ee2bdcd5e667d6ae.tar.zst
nvim-treesitter-d25549917de8047eb91d6f56ee2bdcd5e667d6ae.zip
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
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter.lua6
-rw-r--r--lua/nvim-treesitter/locals.lua20
2 files changed, 11 insertions, 15 deletions
diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua
index ef6c31506..8dae7c33b 100644
--- a/lua/nvim-treesitter.lua
+++ b/lua/nvim-treesitter.lua
@@ -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
diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua
index e58e5fc58..808b0f3ff 100644
--- a/lua/nvim-treesitter/locals.lua
+++ b/lua/nvim-treesitter/locals.lua
@@ -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)