summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/ts_utils.lua
diff options
context:
space:
mode:
authorkiyan42 <yazdani.kiyan@protonmail.com>2020-10-13 23:55:37 +0200
committerKiyan Yazdani <yazdani.kiyan@protonmail.com>2020-10-19 21:08:15 +0200
commit1735528db5866d4ccdf117fb7f4c7fd2b724e4db (patch)
treef7b20f610547eb54dd8a217658b006bc8040b405 /lua/nvim-treesitter/ts_utils.lua
parent36b5f6f075114b5566d0ab225f97466ff36de41f (diff)
Treesitter indent
also fixes the memoize_by_buf_tick function
Diffstat (limited to 'lua/nvim-treesitter/ts_utils.lua')
-rw-r--r--lua/nvim-treesitter/ts_utils.lua29
1 files changed, 12 insertions, 17 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua
index 6647cc96..b235c013 100644
--- a/lua/nvim-treesitter/ts_utils.lua
+++ b/lua/nvim-treesitter/ts_utils.lua
@@ -201,31 +201,26 @@ end
--- Memoizes a function based on the buffer tick of the provided bufnr.
-- The cache entry is cleared when the buffer is detached to avoid memory leaks.
--- @param fn: the fn to memoize
--- @param bufnr_fn: a function that receives all arguments passed to the function
--- and returns the bufnr from the arguments
+-- @param fn: the fn to memoize, taking the bufnr as first argument
-- @returns a memoized function
-function M.memoize_by_buf_tick(fn, bufnr_fn)
- local bufnr_fn = bufnr_fn or function(a) return a end
+function M.memoize_by_buf_tick(fn)
local cache = {}
- return function(...)
- local bufnr = bufnr_fn(...)
- local tick = api.nvim_buf_get_changedtick(bufnr)
-
+ return function(bufnr)
if cache[bufnr] then
- if cache[bufnr].last_tick == tick then
- return cache[bufnr].result
- end
+ return cache[bufnr]
else
cache[bufnr] = {}
- api.nvim_buf_attach(bufnr, false, { on_detach = function() cache[bufnr] = nil end })
+ api.nvim_buf_attach(bufnr, false,
+ {
+ on_changedtick = function() cache[bufnr] = fn(bufnr) end,
+ on_detach = function() cache[bufnr] = nil end
+ }
+ )
end
- cache[bufnr].last_tick = tick
- cache[bufnr].result = fn(...)
-
- return cache[bufnr].result
+ cache[bufnr] = fn(bufnr)
+ return cache[bufnr]
end
end