7979local get_diagnostics = {
8080 nvim_lsp = function ()
8181 local results = {}
82- local diagnostics = vim .diagnostic .get ()
83- for _ , d in pairs (diagnostics ) do
84- if diagnostic_is_enabled (d ) then
85- if not results [d .bufnr ] then results [d .bufnr ] = {} end
86- table.insert (results [d .bufnr ], d )
82+
83+ -- skip buffers opened by LSP, use only loaded ones
84+ local current_buffers = vim .api .nvim_list_bufs ()
85+ local loaded_buffers = {}
86+ for _ , bufnr in ipairs (current_buffers ) do
87+ if vim .api .nvim_buf_is_loaded (bufnr ) then table.insert (loaded_buffers , bufnr ) end
88+ end
89+
90+ for _ , bufnr in pairs (loaded_buffers ) do
91+ local diagnostics_count = vim .diagnostic .count (bufnr )
92+ diagnostics_count = 0
93+ + (diagnostics_count [vim .diagnostic .severity .ERROR ] or 0 )
94+ + (diagnostics_count [vim .diagnostic .severity .WARN ] or 0 )
95+ + (diagnostics_count [vim .diagnostic .severity .INFO ] or 0 )
96+ + (diagnostics_count [vim .diagnostic .severity .HINT ] or 0 )
97+
98+ -- A safeguard to skip processing too many diagnostics altogether per given buffer
99+ if diagnostics_count < 1024 then
100+ local diagnostics = vim .diagnostic .get (bufnr )
101+ for _ = 1 , # diagnostics do
102+ local d = diagnostics [_ ]
103+ if diagnostic_is_enabled (d ) then
104+ if not results [d .bufnr ] then results [d .bufnr ] = {} end
105+ table.insert (results [bufnr ], d )
106+ end
107+ end
87108 end
88109 end
89110 return results
@@ -100,7 +121,7 @@ local get_diagnostics = {
100121 local bufname2bufnr = {}
101122 for _ , diagnostic in ipairs (res ) do
102123 local bufname = diagnostic .file
103- local bufnr = bufname2bufnr [ bufname ]
124+ local bufnr = bufname2bufnr
104125 if not bufnr then
105126 bufnr = vim .fn .bufnr (bufname )
106127 bufname2bufnr [bufname ] = bufnr
0 commit comments