Skip to content

Commit 0dd1786

Browse files
committed
helpers/cache: always return a new copy
closes #285
1 parent 7c493a2 commit 0dd1786

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lua/null-ls/helpers/cache.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ M.by_bufnr = function(cb)
3333
M.cache[key][bufnr] = cb(params) or false
3434
end
3535

36-
return M.cache[key][bufnr]
36+
return vim.deepcopy(M.cache[key][bufnr])
3737
end
3838
end
3939

4040
--- creates a function that caches the output of an async callback, indexed by bufnr
4141
---@param cb function
42-
---@return fun(params: NullLsCacheParams): any
42+
---@return fun(params: NullLsCacheParams, done: function): any
4343
M.by_bufnr_async = function(cb)
4444
-- assign next available key, since we just want to avoid collisions
4545
local key = next_key
@@ -53,17 +53,17 @@ M.by_bufnr_async = function(cb)
5353
-- make sure we always store a value so we know we've already called cb
5454
cb(params, function(result)
5555
M.cache[key][bufnr] = result or false
56-
done(M.cache[key][bufnr])
56+
done(vim.deepcopy(M.cache[key][bufnr]))
5757
end)
5858
else
59-
done(M.cache[key][bufnr])
59+
done(vim.deepcopy(M.cache[key][bufnr]))
6060
end
6161
end
6262
end
6363

6464
--- creates a function that caches the output of an async callback, indexed by project root
6565
---@param cb function
66-
---@return fun(params: NullLsParams): any
66+
---@return fun(params: NullLsParams, done: function): any
6767
M.by_bufroot_async = function(cb)
6868
-- assign next available key, since we just want to avoid collisions
6969
local key = next_key
@@ -72,15 +72,16 @@ M.by_bufroot_async = function(cb)
7272

7373
return function(params, done)
7474
local root = params.root
75+
assert(root, "root should not be empty when caching by bufroot")
7576
-- if we haven't cached a value yet, get it from cb
7677
if M.cache[key][root] == nil then
7778
-- make sure we always store a value so we know we've already called cb
7879
cb(params, function(result)
7980
M.cache[key][root] = result or false
80-
done(M.cache[key][root])
81+
done(vim.deepcopy(M.cache[key][root]))
8182
end)
8283
else
83-
done(M.cache[key][root])
84+
done(vim.deepcopy(M.cache[key][root]))
8485
end
8586
end
8687
end

0 commit comments

Comments
 (0)