@@ -15,6 +15,7 @@ local scope = require 'workspace.scope'
1515local time = require ' bee.time'
1616local ltable = require ' linked-table'
1717local furi = require ' file-uri'
18+ local json = require ' json'
1819
1920--- @class diagnosticProvider
2021local m = {}
@@ -194,33 +195,43 @@ local function copyDiagsWithoutSyntax(diags)
194195end
195196
196197--- @async
197- function m .doDiagnostic (uri , isScopeDiag )
198+ --- @param uri uri
199+ --- @return boolean
200+ local function isValid (uri )
198201 if not config .get (uri , ' Lua.diagnostics.enable' ) then
199- return
202+ return false
200203 end
201204 if files .isLibrary (uri , true ) then
202205 local status = config .get (uri , ' Lua.diagnostics.libraryFiles' )
203206 if status == ' Disable' then
204- return
207+ return false
205208 elseif status == ' Opened' then
206209 if not files .isOpen (uri ) then
207- return
210+ return false
208211 end
209212 end
210213 end
211214 if ws .isIgnored (uri ) then
212215 local status = config .get (uri , ' Lua.diagnostics.ignoredFiles' )
213216 if status == ' Disable' then
214- return
217+ return false
215218 elseif status == ' Opened' then
216219 if not files .isOpen (uri ) then
217- return
220+ return false
218221 end
219222 end
220223 end
221224 local scheme = furi .split (uri )
222225 local disableScheme = config .get (uri , ' Lua.diagnostics.disableScheme' )
223226 if disableScheme [scheme ] then
227+ return false
228+ end
229+ return true
230+ end
231+
232+ --- @async
233+ function m .doDiagnostic (uri , isScopeDiag )
234+ if not isValid (uri ) then
224235 return
225236 end
226237
@@ -293,6 +304,41 @@ function m.doDiagnostic(uri, isScopeDiag)
293304 pushResult ()
294305end
295306
307+ --- @async
308+ --- @return table | nil result
309+ --- @return boolean ? unchanged
310+ function m .pullDiagnostic (uri , isScopeDiag )
311+ if not isValid (uri ) then
312+ return nil
313+ end
314+
315+ await .delay ()
316+
317+ local state = files .getState (uri )
318+ if not state then
319+ return nil
320+ end
321+
322+ local prog <close> = progress .create (uri , lang .script .WINDOW_DIAGNOSING , 0.5 )
323+ prog :setMessage (ws .getRelativePath (uri ))
324+
325+ local syntax = m .syntaxErrors (uri , state )
326+ local diags = {}
327+
328+ xpcall (core , log .error , uri , isScopeDiag , function (result )
329+ diags [# diags + 1 ] = buildDiagnostic (uri , result )
330+ end )
331+
332+ local full = mergeDiags (syntax , diags )
333+ if util .equal (m .cache [uri ], full ) then
334+ return full , true
335+ end
336+
337+ m .cache [uri ] = full
338+
339+ return full
340+ end
341+
296342function m .refresh (uri )
297343 if not ws .isReady (uri ) then
298344 return
@@ -426,13 +472,27 @@ function m.diagnosticsScope(uri, force)
426472 end , id )
427473end
428474
475+ --- @param uri uri
476+ --- @return ' server' | ' client'
477+ function m .getOwner (uri )
478+ -- TODO
479+ return ' client'
480+ end
481+
429482ws .watch (function (ev , uri )
430483 if ev == ' reload' then
431- m .diagnosticsScope (uri )
484+ if m .getOwner (uri ) == ' server' then
485+ m .diagnosticsScope (uri )
486+ else
487+ proto .request (' workspace/diagnostic/refresh' , json .null )
488+ end
432489 end
433490end )
434491
435492files .watch (function (ev , uri ) --- @async
493+ if m .getOwner (uri ) == ' client' then
494+ return
495+ end
436496 if ev == ' remove' then
437497 m .clear (uri )
438498 m .refresh (uri )
@@ -454,7 +514,11 @@ end)
454514config .watch (function (uri , key , value , oldValue )
455515 if key :find ' Lua.diagnostics' then
456516 if value ~= oldValue then
457- m .diagnosticsScope (uri )
517+ if m .getOwner (uri ) == ' server' then
518+ m .diagnosticsScope (uri )
519+ else
520+ proto .request (' workspace/diagnostic/refresh' , json .null )
521+ end
458522 end
459523 end
460524end )
0 commit comments