aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/lua/vim/diagnostic.lua33
-rw-r--r--test/functional/lua/diagnostic_spec.lua2
2 files changed, 10 insertions, 25 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
index 9b57467a52..c545808b70 100644
--- a/runtime/lua/vim/diagnostic.lua
+++ b/runtime/lua/vim/diagnostic.lua
@@ -39,39 +39,22 @@ M.handlers = setmetatable({}, {
-- Metatable that automatically creates an empty table when assigning to a missing key
local bufnr_and_namespace_cacher_mt = {
__index = function(t, bufnr)
- if not bufnr or bufnr == 0 then
- bufnr = vim.api.nvim_get_current_buf()
- end
-
- rawset(t, bufnr, {})
-
- return rawget(t, bufnr)
- end,
-
- __newindex = function(t, bufnr, v)
- if not bufnr or bufnr == 0 then
- bufnr = vim.api.nvim_get_current_buf()
- end
-
- rawset(t, bufnr, v)
+ assert(bufnr > 0, "Invalid buffer number")
+ t[bufnr] = {}
+ return t[bufnr]
end,
}
local diagnostic_cache = setmetatable({}, {
__index = function(t, bufnr)
- if not bufnr or bufnr == 0 then
- bufnr = vim.api.nvim_get_current_buf()
- end
-
+ assert(bufnr > 0, "Invalid buffer number")
vim.api.nvim_buf_attach(bufnr, false, {
on_detach = function()
rawset(t, bufnr, nil) -- clear cache
end
})
-
- rawset(t, bufnr, {})
-
- return rawget(t, bufnr)
+ t[bufnr] = {}
+ return t[bufnr]
end,
})
@@ -659,6 +642,8 @@ function M.set(namespace, bufnr, diagnostics, opts)
opts = {opts, 't', true},
}
+ bufnr = get_bufnr(bufnr)
+
if vim.tbl_isempty(diagnostics) then
diagnostic_cache[bufnr][namespace] = nil
else
@@ -1320,7 +1305,7 @@ function M.reset(namespace, bufnr)
bufnr = {bufnr, 'n', true},
}
- local buffers = bufnr and {bufnr} or vim.tbl_keys(diagnostic_cache)
+ local buffers = bufnr and {get_bufnr(bufnr)} or vim.tbl_keys(diagnostic_cache)
for _, iter_bufnr in ipairs(buffers) do
local namespaces = namespace and {namespace} or vim.tbl_keys(diagnostic_cache[iter_bufnr])
for _, iter_namespace in ipairs(namespaces) do
diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua
index 348b9de816..05745a5099 100644
--- a/test/functional/lua/diagnostic_spec.lua
+++ b/test/functional/lua/diagnostic_spec.lua
@@ -111,7 +111,7 @@ describe('vim.diagnostic', function()
it('retrieves diagnostics from all buffers and namespaces', function()
local result = exec_lua [[
local other_bufnr = vim.api.nvim_create_buf(true, false)
- local lines = {"1st line of text", "2nd line of text", "wow", "cool", "more", "lines"}
+ local lines = vim.api.nvim_buf_get_lines(diagnostic_bufnr, 0, -1, true)
vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {