@@ -16,6 +16,7 @@ local time = require 'bee.time'
16
16
local ltable = require ' linked-table'
17
17
local furi = require ' file-uri'
18
18
local json = require ' json'
19
+ local fw = require ' filewatch'
19
20
20
21
--- @class diagnosticProvider
21
22
local m = {}
@@ -156,6 +157,18 @@ function m.clearCache(uri)
156
157
m .cache [uri ] = false
157
158
end
158
159
160
+ function m .clearCacheExcept (uris )
161
+ local excepts = {}
162
+ for _ , uri in ipairs (uris ) do
163
+ excepts [uri ] = true
164
+ end
165
+ for uri in pairs (m .cache ) do
166
+ if not excepts [uri ] then
167
+ m .cache [uri ] = false
168
+ end
169
+ end
170
+ end
171
+
159
172
function m .clearAll ()
160
173
for luri in pairs (m .cache ) do
161
174
m .clear (luri )
@@ -309,14 +322,14 @@ end
309
322
--- @return boolean ? unchanged
310
323
function m .pullDiagnostic (uri , isScopeDiag )
311
324
if not isValid (uri ) then
312
- return nil
325
+ return nil , util . equal ( m . cache [ uri ], nil )
313
326
end
314
327
315
328
await .delay ()
316
329
317
330
local state = files .getState (uri )
318
331
if not state then
319
- return nil
332
+ return nil , util . equal ( m . cache [ uri ], nil )
320
333
end
321
334
322
335
local prog <close> = progress .create (uri , lang .script .WINDOW_DIAGNOSING , 0.5 )
@@ -330,6 +343,7 @@ function m.pullDiagnostic(uri, isScopeDiag)
330
343
end )
331
344
332
345
local full = mergeDiags (syntax , diags )
346
+
333
347
if util .equal (m .cache [uri ], full ) then
334
348
return full , true
335
349
end
@@ -411,7 +425,7 @@ local function askForDisable(uri)
411
425
end
412
426
413
427
--- @async
414
- function m .awaitDiagnosticsScope (suri )
428
+ function m .awaitDiagnosticsScope (suri , callback )
415
429
local scp = scope .getScope (suri )
416
430
while loading .count () > 0 do
417
431
await .sleep (1.0 )
@@ -445,7 +459,7 @@ function m.awaitDiagnosticsScope(suri)
445
459
i = i + 1
446
460
bar :setMessage ((' %d/%d' ):format (i , # uris ))
447
461
bar :setPercentage (i / # uris * 100 )
448
- xpcall ( m . doDiagnostic , log . error , uri , true )
462
+ callback ( uri )
449
463
await .delay ()
450
464
if cancelled then
451
465
log .info (' Break workspace diagnostics' )
@@ -468,23 +482,76 @@ function m.diagnosticsScope(uri, force)
468
482
local id = ' diagnosticsScope:' .. scp :getName ()
469
483
await .close (id )
470
484
await .call (function () --- @async
471
- m .awaitDiagnosticsScope (uri )
485
+ m .awaitDiagnosticsScope (uri , function (fileUri )
486
+ xpcall (m .doDiagnostic , log .error , fileUri , true )
487
+ end )
472
488
end , id )
473
489
end
474
490
491
+ --- @async
492
+ function m .pullDiagnosticScope ()
493
+ local results = {}
494
+ local processing = 0
495
+
496
+ for _ , scp in ipairs (scope .folders ) do
497
+ if ws .isReady (scp .uri )
498
+ and config .get (scp .uri , ' Lua.diagnostics.enable' ) then
499
+ local id = ' diagnosticsScope:' .. scp :getName ()
500
+ await .close (id )
501
+ await .call (function () --- @async
502
+ processing = processing + 1
503
+ local _ <close> = util .defer (function ()
504
+ processing = processing - 1
505
+ end )
506
+
507
+ local delay = config .get (scp .uri , ' Lua.diagnostics.workspaceDelay' ) / 1000
508
+ if delay < 0 then
509
+ return
510
+ end
511
+ print (delay )
512
+ await .sleep (math.max (delay , 0.2 ))
513
+ print (' start' )
514
+
515
+ m .awaitDiagnosticsScope (scp .uri , function (fileUri )
516
+ local suc , result , unchanged = xpcall (m .pullDiagnostic , log .error , fileUri , true )
517
+ if suc then
518
+ results [# results + 1 ] = {
519
+ uri = fileUri ,
520
+ result = result ,
521
+ unchanged = unchanged ,
522
+ version = files .getVersion (fileUri ),
523
+ }
524
+ end
525
+ end )
526
+ end , id )
527
+ end
528
+ end
529
+
530
+ while processing > 0 do
531
+ await .sleep (0.1 )
532
+ end
533
+
534
+ return results
535
+ end
536
+
475
537
--- @param uri uri
476
538
--- @return ' server' | ' client'
477
539
function m .getOwner (uri )
478
540
-- TODO
479
541
return ' client'
480
542
end
481
543
544
+ function m .refreshClient ()
545
+ log .debug (' Refresh client diagnostics' )
546
+ proto .request (' workspace/diagnostic/refresh' , json .null )
547
+ end
548
+
482
549
ws .watch (function (ev , uri )
483
550
if ev == ' reload' then
484
551
if m .getOwner (uri ) == ' server' then
485
552
m .diagnosticsScope (uri )
486
553
else
487
- proto . request ( ' workspace/diagnostic/refresh ' , json . null )
554
+ m . refreshClient ( )
488
555
end
489
556
end
490
557
end )
@@ -517,7 +584,19 @@ config.watch(function (uri, key, value, oldValue)
517
584
if m .getOwner (uri ) == ' server' then
518
585
m .diagnosticsScope (uri )
519
586
else
520
- proto .request (' workspace/diagnostic/refresh' , json .null )
587
+ m .refreshClient ()
588
+ end
589
+ end
590
+ end
591
+ end )
592
+
593
+ fw .event (function (ev , path )
594
+ if util .stringEndWith (path , ' .editorconfig' ) then
595
+ for _ , scp in ipairs (ws .folders ) do
596
+ if m .getOwner (scp .uri ) == ' server' then
597
+ m .diagnosticsScope (scp .uri )
598
+ else
599
+ m .refreshClient ()
521
600
end
522
601
end
523
602
end
0 commit comments