Skip to content

Commit 12d2587

Browse files
authored
fix(lua): refresh diagnostic for all loaded buffer (#5261)
1 parent 59a853a commit 12d2587

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

lua/coc/diagnostic.lua

+23-23
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@ local M = {}
22

33
local ns = vim.api.nvim_create_namespace('coc_diagnostic')
44

5-
function M.on_diagnostic_change()
5+
function M.refresh()
66
vim.diagnostic.reset(ns)
77

8-
local bufnr = vim.api.nvim_get_current_buf()
9-
local ok, items = pcall(vim.api.nvim_buf_get_var, bufnr, 'coc_diagnostic_map')
10-
if not ok or type(items) ~= 'table' or vim.tbl_isempty(items) then
11-
return
8+
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
9+
if vim.api.nvim_buf_is_loaded(bufnr) then
10+
local ok, items = pcall(vim.api.nvim_buf_get_var, bufnr, 'coc_diagnostic_map')
11+
if ok and type(items) == 'table' and vim.tbl_count(items) >= 0 then
12+
local diagnostics = {}
13+
for _, d in ipairs(items) do
14+
diagnostics[#diagnostics + 1] = {
15+
bufnr = bufnr,
16+
lnum = d.location.range.start.line,
17+
end_lnum = d.location.range['end'].line,
18+
col = d.location.range.start.character,
19+
end_col = d.location.range['end'].character,
20+
severity = d.level,
21+
message = d.message,
22+
source = d.source,
23+
code = d.code,
24+
namespace = ns,
25+
}
26+
end
27+
vim.diagnostic.set(ns, bufnr, diagnostics)
28+
end
29+
end
1230
end
13-
14-
local diagnostics = {}
15-
for _, d in ipairs(items) do
16-
diagnostics[#diagnostics + 1] = {
17-
bufnr = 0,
18-
lnum = d.location.range.start.line,
19-
end_lnum = d.location.range['end'].line,
20-
col = d.location.range.start.character,
21-
end_col = d.location.range['end'].character,
22-
severity = d.level,
23-
message = d.message,
24-
source = d.source,
25-
code = d.code,
26-
namespace = ns,
27-
}
28-
end
29-
30-
vim.diagnostic.set(ns, bufnr, diagnostics)
3131
end
3232

3333
return M

plugin/coc.lua

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if vim.fn.has('nvim-0.10') then
2+
vim.api.nvim_create_autocmd({ 'BufEnter' }, {
3+
callback = function()
4+
require('coc.diagnostic').refresh()
5+
end,
6+
})
7+
8+
vim.api.nvim_create_autocmd('User', {
9+
pattern = 'CocDiagnosticChange',
10+
callback = function()
11+
require('coc.diagnostic').refresh()
12+
end,
13+
})
14+
end

plugin/coc.vim

-4
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,6 @@ function! s:Enable(initialize)
414414
autocmd BufReadCmd,FileReadCmd,SourceCmd list://* call coc#list#setup(expand('<amatch>'))
415415
autocmd BufWriteCmd __coc_refactor__* :call coc#rpc#notify('saveRefactor', [+expand('<abuf>')])
416416
autocmd ColorScheme * call s:Highlight() | call s:Autocmd('ColorScheme')
417-
418-
if has('nvim-0.10')
419-
autocmd User CocDiagnosticChange call v:lua.require('coc.diagnostic').on_diagnostic_change()
420-
endif
421417
augroup end
422418
if a:initialize == 0
423419
call coc#rpc#request('attach', [])

0 commit comments

Comments
 (0)