Skip to content

Commit

Permalink
fix(lua): refresh diagnostic for all loaded buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Feb 20, 2025
1 parent 811f395 commit d011f0e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
44 changes: 22 additions & 22 deletions lua/coc/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ local ns = vim.api.nvim_create_namespace('coc_diagnostic')
function M.on_diagnostic_change()
vim.diagnostic.reset(ns)

local bufnr = vim.api.nvim_get_current_buf()
local ok, items = pcall(vim.api.nvim_buf_get_var, bufnr, 'coc_diagnostic_map')
if not ok or type(items) ~= 'table' or vim.tbl_isempty(items) then
return
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(bufnr) then
local ok, items = pcall(vim.api.nvim_buf_get_var, bufnr, 'coc_diagnostic_map')
if ok and type(items) == 'table' and vim.tbl_count(items) > 0 then
local diagnostics = {}
for _, d in ipairs(items) do
diagnostics[#diagnostics + 1] = {
bufnr = bufnr,
lnum = d.location.range.start.line,
end_lnum = d.location.range['end'].line,
col = d.location.range.start.character,
end_col = d.location.range['end'].character,
severity = d.level,
message = d.message,
source = d.source,
code = d.code,
namespace = ns,
}
end
vim.diagnostic.set(ns, bufnr, diagnostics)
end
end
end

local diagnostics = {}
for _, d in ipairs(items) do
diagnostics[#diagnostics + 1] = {
bufnr = 0,
lnum = d.location.range.start.line,
end_lnum = d.location.range['end'].line,
col = d.location.range.start.character,
end_col = d.location.range['end'].character,
severity = d.level,
message = d.message,
source = d.source,
code = d.code,
namespace = ns,
}
end

vim.diagnostic.set(ns, bufnr, diagnostics)
end

return M
14 changes: 14 additions & 0 deletions plugin/coc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if vim.fn.has('nvim-0.10') then
vim.api.nvim_create_autocmd({ 'BufEnter' }, {
callback = function()
require('coc.diagnostic').on_diagnostic_change()
end,
})

vim.api.nvim_create_autocmd('User', {
pattern = 'CocDiagnosticChange',
callback = function()
require('coc.diagnostic').on_diagnostic_change()
end,
})
end
4 changes: 0 additions & 4 deletions plugin/coc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,6 @@ function! s:Enable(initialize)
autocmd BufReadCmd,FileReadCmd,SourceCmd list://* call coc#list#setup(expand('<amatch>'))
autocmd BufWriteCmd __coc_refactor__* :call coc#rpc#notify('saveRefactor', [+expand('<abuf>')])
autocmd ColorScheme * call s:Highlight() | call s:Autocmd('ColorScheme')

if has('nvim-0.10')
autocmd User CocDiagnosticChange call v:lua.require('coc.diagnostic').on_diagnostic_change()
endif
augroup end
if a:initialize == 0
call coc#rpc#request('attach', [])
Expand Down

0 comments on commit d011f0e

Please sign in to comment.