@@ -34,6 +34,10 @@ local diag_severity_to_string = function(severity)
34
34
end
35
35
end
36
36
37
+ local diagnostic_filter = function (diag )
38
+ return diag .source == " Lua Diagnostics." and diag .message == " Undefined global `vim`."
39
+ end
40
+
37
41
local tracked_functions = {}
38
42
M .debounce_strategy = {
39
43
CALL_FIRST_AND_LAST = 0 ,
@@ -214,39 +218,76 @@ M.human_size = function (size)
214
218
return human
215
219
end
216
220
217
- --- Gets diagnostic severity counts for all files
218
- --- @return table table { file_path = { Error = int , Warning = int , Information = int , Hint = int , Unknown = int } }
221
+ --- Gets most severe diagnostics count for each open file and each ancestor directory.
222
+ --- severity_number and severity_string refer to the highest severity
223
+ --- with non-zero diagnostics count. They are nil if all counts are 0.
224
+ --- We only store the count of the most severe non-zero diagnostic.
225
+ --- @return table table
226
+ --- { file_path = {
227
+ --- severity_number = int or nil,
228
+ --- severity_string = string or nil,
229
+ --- [1] = int or nil,
230
+ --- [2] = int or nil,
231
+ --- [3] = int or nil,
232
+ --- [4] = int or nil
233
+ --- } or nil }
219
234
M .get_diagnostic_counts = function ()
220
- local d = vim .diagnostic .get ()
221
235
local lookup = {}
222
- for _ , diag in ipairs (d ) do
223
- if diag .source == " Lua Diagnostics." and diag .message == " Undefined global `vim`." then
224
- -- ignore this diagnostic
225
- else
226
- local success , file_name = pcall (vim .api .nvim_buf_get_name , diag .bufnr )
227
- if success then
228
- local sev = diag_severity_to_string (diag .severity )
229
- if sev then
230
- local entry = lookup [file_name ] or { severity_number = 4 }
231
- entry [sev ] = (entry [sev ] or 0 ) + 1
232
- entry .severity_number = math.min (entry .severity_number , diag .severity )
233
- entry .severity_string = diag_severity_to_string (entry .severity_number )
234
- lookup [file_name ] = entry
236
+
237
+ for ns , _ in pairs (vim .diagnostic .get_namespaces ()) do
238
+ for _ , bufnr in ipairs (vim .api .nvim_list_bufs ()) do
239
+ if vim .diagnostic .is_disabled (bufnr , ns ) then
240
+ break
241
+ end
242
+ local success , file_name = pcall (vim .api .nvim_buf_get_name , bufnr )
243
+ if not success then
244
+ break
245
+ end
246
+ for severity , _ in ipairs (vim .diagnostic .severity ) do
247
+ local diagnostics = vim .diagnostic .get (bufnr , { namespace = ns , severity = severity })
248
+
249
+ -- It would be nice to not do this manual filtering below
250
+ -- Note that lua diagnostics can be suppressed using neodev
251
+ -- or lua_ls settings instead.
252
+ -- https://github.com/folke/neodev.nvim
253
+ local ignored_diagnostics = 0
254
+ for _ , diag in ipairs (diagnostics ) do
255
+ if diagnostic_filter (diag ) then
256
+ ignored_diagnostics = ignored_diagnostics + 1
257
+ end
258
+ end
259
+
260
+ local n_diagnostics = # diagnostics - ignored_diagnostics
261
+ if n_diagnostics > 0 then
262
+ lookup [file_name ] = {
263
+ severity_number = severity ,
264
+ severity_string = diag_severity_to_string (severity ),
265
+ [severity ] = n_diagnostics ,
266
+ }
267
+ break
235
268
end
236
269
end
237
270
end
238
271
end
239
272
240
- for file_name , entry in pairs (lookup ) do
273
+ for file_name , file_entry in pairs (lookup ) do
241
274
-- Now bubble this status up to the parent directories
242
275
local parts = M .split (file_name , M .path_separator )
243
276
table.remove (parts ) -- pop the last part so we don't override the file's status
244
277
M .reduce (parts , " " , function (acc , part )
245
278
local path = (M .is_windows and acc == " " ) and part or M .path_join (acc , part )
246
- local path_entry = lookup [path ] or { severity_number = 4 }
247
- path_entry .severity_number = math.min (path_entry .severity_number , entry .severity_number )
248
- path_entry .severity_string = diag_severity_to_string (path_entry .severity_number )
249
- lookup [path ] = path_entry
279
+
280
+ if lookup [path ] == nil and file_entry .severity_number then
281
+ lookup [path ] = {
282
+ severity_number = file_entry .severity_number ,
283
+ severity_string = file_entry .severity_string ,
284
+ }
285
+ elseif lookup [path ] and lookup [path ].severity_number and file_entry .severity_number then
286
+ local min_severity = math.min (lookup [path ].severity_number , file_entry .severity_number )
287
+ lookup [path ].severity_number = min_severity
288
+ lookup [path ].severity_string = diag_severity_to_string (min_severity )
289
+ end
290
+
250
291
return path
251
292
end )
252
293
end
0 commit comments