@@ -17,7 +17,7 @@ local COC_SEVERITY_LEVELS = {
17
17
}
18
18
19
19
--- Absolute Node path to LSP severity level
20
- --- @alias NodeSeverities table<string , lsp.DiagnosticSeverity >
20
+ --- @alias NodeSeverities table<string , vim.diagnostic.Severity >
21
21
22
22
--- @class DiagStatus
23
23
--- @field value lsp.DiagnosticSeverity | nil
@@ -37,33 +37,6 @@ local function uniformize_path(path)
37
37
return utils .canonical_path (path :gsub (" \\ " , " /" ))
38
38
end
39
39
40
- --- Marshal severities from LSP. Does nothing when LSP disabled.
41
- --- @return NodeSeverities
42
- local function from_nvim_lsp ()
43
- local buffer_severity = {}
44
-
45
- -- is_enabled is not present in all 0.10 builds/releases, see #2781
46
- local is_enabled = false
47
- if vim .fn .has (" nvim-0.10" ) == 1 and type (vim .diagnostic .is_enabled ) == " function" then
48
- is_enabled = vim .diagnostic .is_enabled ()
49
- elseif type (vim .diagnostic .is_disabled ) == " function" then --- @diagnostic disable-line : deprecated
50
- is_enabled = not vim .diagnostic .is_disabled () --- @diagnostic disable-line : deprecated
51
- end
52
-
53
- if is_enabled then
54
- for _ , diagnostic in ipairs (vim .diagnostic .get (nil , { severity = M .severity })) do
55
- if diagnostic .severity and diagnostic .bufnr and vim .api .nvim_buf_is_valid (diagnostic .bufnr ) then
56
- local bufname = uniformize_path (vim .api .nvim_buf_get_name (diagnostic .bufnr ))
57
- if not buffer_severity [bufname ] or diagnostic .severity < buffer_severity [bufname ] then
58
- buffer_severity [bufname ] = diagnostic .severity
59
- end
60
- end
61
- end
62
- end
63
-
64
- return buffer_severity
65
- end
66
-
67
40
--- Severity is within diagnostics.severity.min, diagnostics.severity.max
68
41
--- @param severity lsp.DiagnosticSeverity
69
42
--- @param config table
@@ -135,35 +108,75 @@ local function from_cache(node)
135
108
for bufname , severity in pairs (NODE_SEVERITIES ) do
136
109
local node_contains_buf = vim .startswith (bufname , nodepath .. " /" )
137
110
if node_contains_buf then
138
- if severity == M . severity . max then
111
+ if not max_severity or severity < max_severity then
139
112
max_severity = severity
140
- break
141
- else
142
- max_severity = math.min (max_severity or severity , severity )
143
113
end
144
114
end
145
115
end
146
116
end
147
117
return { value = max_severity , cache_version = NODE_SEVERITIES_VERSION }
148
118
end
149
119
150
- --- Fired on DiagnosticChanged and CocDiagnosticChanged events:
120
+ --- Fired on DiagnosticChanged for a single buffer
121
+ --- @param ev table standard event with data.diagnostics populated
122
+ function M .update_lsp (ev )
123
+ if not M .enable or not ev or not ev .data or not ev .data .diagnostics then
124
+ return
125
+ end
126
+
127
+ local profile_event = log .profile_start (" DiagnosticChanged event" )
128
+
129
+ --- @type vim.Diagnostic[]
130
+ local diagnostics = ev .data .diagnostics
131
+
132
+ -- use the buffer from the event, as ev.data.diagnostics will be empty on resolved diagnostics
133
+ local bufname = uniformize_path (vim .api .nvim_buf_get_name (ev .buf ))
134
+
135
+ --- @type vim.diagnostic.Severity ?
136
+ local new_severity = nil
137
+
138
+ -- most severe (lowest) severity in user range
139
+ for _ , diagnostic in ipairs (diagnostics ) do
140
+ if diagnostic .severity >= M .severity .max and diagnostic .severity <= M .severity .min then
141
+ if not new_severity or diagnostic .severity < new_severity then
142
+ new_severity = diagnostic .severity
143
+ end
144
+ end
145
+ end
146
+
147
+ -- record delta and schedule a redraw
148
+ if new_severity ~= NODE_SEVERITIES [bufname ] then
149
+ NODE_SEVERITIES [bufname ] = new_severity
150
+ NODE_SEVERITIES_VERSION = NODE_SEVERITIES_VERSION + 1
151
+
152
+ utils .debounce (" DiagnosticChanged redraw" , M .debounce_delay , function ()
153
+ local profile_redraw = log .profile_start (" DiagnosticChanged redraw" )
154
+
155
+ local explorer = core .get_explorer ()
156
+ if explorer then
157
+ explorer .renderer :draw ()
158
+ end
159
+
160
+ log .profile_end (profile_redraw )
161
+ end )
162
+ end
163
+
164
+ log .profile_end (profile_event )
165
+ end
166
+
167
+ --- Fired on CocDiagnosticChanged events:
151
168
--- debounced retrieval, cache update, version increment and draw
152
- function M .update ()
169
+ function M .update_coc ()
153
170
if not M .enable then
154
171
return
155
172
end
156
- utils .debounce (" diagnostics " , M .debounce_delay , function ()
173
+ utils .debounce (" CocDiagnosticChanged update " , M .debounce_delay , function ()
157
174
local profile = log .profile_start (" diagnostics update" )
158
- if is_using_coc () then
159
- NODE_SEVERITIES = from_coc ()
160
- else
161
- NODE_SEVERITIES = from_nvim_lsp ()
162
- end
175
+ NODE_SEVERITIES = from_coc ()
163
176
NODE_SEVERITIES_VERSION = NODE_SEVERITIES_VERSION + 1
164
177
if log .enabled (" diagnostics" ) then
165
178
for bufname , severity in pairs (NODE_SEVERITIES ) do
166
- log .line (" diagnostics" , " Indexing bufname '%s' with severity %d" , bufname , severity )
179
+ log .line (" diagnostics" , " COC Indexing bufname '%s' with severity %d" , bufname , severity )
167
180
end
168
181
end
169
182
log .profile_end (profile )
0 commit comments