Skip to content

Commit b59e37e

Browse files
committed
Ignore disabled diagnostics
1 parent 2d89ca9 commit b59e37e

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

lua/neo-tree/sources/common/components.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ M.diagnostics = function(config, node, state)
8080
if config.hide_when_expanded and node.type == "directory" and node:is_expanded() then
8181
return {}
8282
end
83-
if not diag_state then
83+
if diag_state == nil or diag_state.severity_number == nil then
8484
return {}
8585
end
8686
if config.errors_only and diag_state.severity_number > 1 then

lua/neo-tree/utils/init.lua

+62-21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ local diag_severity_to_string = function(severity)
3434
end
3535
end
3636

37+
local diagnostic_filter = function(diag)
38+
return diag.source == "Lua Diagnostics." and diag.message == "Undefined global `vim`."
39+
end
40+
3741
local tracked_functions = {}
3842
M.debounce_strategy = {
3943
CALL_FIRST_AND_LAST = 0,
@@ -214,39 +218,76 @@ M.human_size = function (size)
214218
return human
215219
end
216220

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 }
219234
M.get_diagnostic_counts = function()
220-
local d = vim.diagnostic.get()
221235
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
235268
end
236269
end
237270
end
238271
end
239272

240-
for file_name, entry in pairs(lookup) do
273+
for file_name, file_entry in pairs(lookup) do
241274
-- Now bubble this status up to the parent directories
242275
local parts = M.split(file_name, M.path_separator)
243276
table.remove(parts) -- pop the last part so we don't override the file's status
244277
M.reduce(parts, "", function(acc, part)
245278
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+
250291
return path
251292
end)
252293
end

0 commit comments

Comments
 (0)