1
1
local utils = require " nvim-tree.utils"
2
2
local view = require " nvim-tree.view"
3
- local core = require " nvim-tree.core"
4
3
local log = require " nvim-tree.log"
5
4
6
5
local M = {}
@@ -12,6 +11,16 @@ local severity_levels = {
12
11
Hint = 4 ,
13
12
}
14
13
14
+ --- A dictionary tree containing buffer-severity mappings.
15
+ --- @type table
16
+ local buffer_severity_dict = {}
17
+
18
+ --- @param path string
19
+ --- @return string
20
+ local function uniformize_path (path )
21
+ return utils .canonical_path (path :gsub (" \\ " , " /" ))
22
+ end
23
+
15
24
--- @return table
16
25
local function from_nvim_lsp ()
17
26
local buffer_severity = {}
@@ -25,7 +34,7 @@ local function from_nvim_lsp()
25
34
for _ , diagnostic in ipairs (vim .diagnostic .get (nil , { severity = M .severity })) do
26
35
local buf = diagnostic .bufnr
27
36
if vim .api .nvim_buf_is_valid (buf ) then
28
- local bufname = vim .api .nvim_buf_get_name (buf )
37
+ local bufname = uniformize_path ( vim .api .nvim_buf_get_name (buf ) )
29
38
local lowest_severity = buffer_severity [bufname ]
30
39
if not lowest_severity or diagnostic .severity < lowest_severity then
31
40
buffer_severity [bufname ] = diagnostic .severity
@@ -67,7 +76,7 @@ local function from_coc()
67
76
local buffer_severity = {}
68
77
for bufname , severity in pairs (diagnostics ) do
69
78
if is_severity_in_range (severity , M .severity ) then
70
- buffer_severity [bufname ] = severity
79
+ buffer_severity [uniformize_path ( bufname ) ] = severity
71
80
end
72
81
end
73
82
@@ -79,47 +88,52 @@ local function is_using_coc()
79
88
end
80
89
81
90
function M .update ()
82
- if not M .enable or not core . get_explorer () or not view . is_buf_valid ( view . get_bufnr ()) then
91
+ if not M .enable then
83
92
return
84
93
end
85
94
utils .debounce (" diagnostics" , M .debounce_delay , function ()
86
95
local profile = log .profile_start " diagnostics update"
87
- log .line (" diagnostics" , " update" )
88
-
89
- local buffer_severity
90
96
if is_using_coc () then
91
- buffer_severity = from_coc ()
97
+ buffer_severity_dict = from_coc ()
92
98
else
93
- buffer_severity = from_nvim_lsp ()
99
+ buffer_severity_dict = from_nvim_lsp ()
94
100
end
95
-
96
- local nodes_by_line = utils . get_nodes_by_line ( core . get_explorer (). nodes , core . get_nodes_starting_line () )
97
- for _ , node in pairs ( nodes_by_line ) do
98
- node . diag_status = nil
101
+ log . node ( " diagnostics " , buffer_severity_dict , " update " )
102
+ log . profile_end ( profile )
103
+ if view . is_buf_valid ( view . get_bufnr ()) then
104
+ require ( " nvim-tree.renderer " ). draw ()
99
105
end
106
+ end )
107
+ end
108
+
109
+ --- @param node Node
110
+ function M .update_node_severity_level (node )
111
+ if not M .enable or node == nil then
112
+ return
113
+ end
100
114
101
- for bufname , severity in pairs (buffer_severity ) do
102
- local bufpath = utils .canonical_path (bufname )
103
- log .line (" diagnostics" , " bufpath '%s' severity %d" , bufpath , severity )
104
- if 0 < severity and severity < 5 then
105
- for line , node in pairs (nodes_by_line ) do
106
- local nodepath = utils .canonical_path (node .absolute_path )
107
- log .line (" diagnostics" , " %d checking nodepath '%s'" , line , nodepath )
108
-
109
- local node_contains_buf = vim .startswith (bufpath :gsub (" \\ " , " /" ), nodepath :gsub (" \\ " , " /" ) .. " /" )
110
- if M .show_on_dirs and node_contains_buf and (not node .open or M .show_on_open_dirs ) then
111
- log .line (" diagnostics" , " matched fold node '%s'" , node .absolute_path )
112
- node .diag_status = severity
113
- elseif nodepath == bufpath then
114
- log .line (" diagnostics" , " matched file node '%s'" , node .absolute_path )
115
- node .diag_status = severity
115
+ local is_folder = node .nodes ~= nil
116
+ local nodepath = uniformize_path (node .absolute_path )
117
+
118
+ if is_folder then
119
+ local max_severity = nil
120
+ if M .show_on_dirs and (not node .open or M .show_on_open_dirs ) then
121
+ for bufname , severity in pairs (buffer_severity_dict ) do
122
+ local node_contains_buf = vim .startswith (bufname , nodepath .. " /" )
123
+ if node_contains_buf then
124
+ if severity == M .severity .max then
125
+ max_severity = severity
126
+ break
127
+ else
128
+ max_severity = math.min (max_severity or severity , severity )
116
129
end
117
130
end
118
131
end
119
132
end
120
- log .profile_end (profile )
121
- require (" nvim-tree.renderer" ).draw ()
122
- end )
133
+ node .diag_status = max_severity
134
+ else
135
+ node .diag_status = buffer_severity_dict [nodepath ]
136
+ end
123
137
end
124
138
125
139
function M .setup (opts )
0 commit comments