Skip to content

Commit 610714a

Browse files
committed
first step of pull diagnostic
1 parent 505ed94 commit 610714a

File tree

2 files changed

+104
-8
lines changed

2 files changed

+104
-8
lines changed

script/provider/diagnostic.lua

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local scope = require 'workspace.scope'
1515
local time = require 'bee.time'
1616
local ltable = require 'linked-table'
1717
local furi = require 'file-uri'
18+
local json = require 'json'
1819

1920
---@class diagnosticProvider
2021
local m = {}
@@ -194,33 +195,43 @@ local function copyDiagsWithoutSyntax(diags)
194195
end
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()
294305
end
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+
296342
function m.refresh(uri)
297343
if not ws.isReady(uri) then
298344
return
@@ -426,13 +472,27 @@ function m.diagnosticsScope(uri, force)
426472
end, id)
427473
end
428474

475+
---@param uri uri
476+
---@return 'server' | 'client'
477+
function m.getOwner(uri)
478+
--TODO
479+
return 'client'
480+
end
481+
429482
ws.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
433490
end)
434491

435492
files.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)
454514
config.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
460524
end)

script/provider/provider.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,38 @@ m.register 'inlayHint/resolve' {
12121212
end
12131213
}
12141214

1215+
m.register 'textDocument/diagnostic' {
1216+
capability = {
1217+
diagnosticProvider = {
1218+
identifier = 'identifier',
1219+
interFileDependencies = true,
1220+
workspaceDiagnostics = false,
1221+
}
1222+
},
1223+
---@async
1224+
function (params)
1225+
local uri = files.getRealUri(params.textDocument.uri)
1226+
workspace.awaitReady(uri)
1227+
local core = require 'provider.diagnostic'
1228+
if not params.previousResultId then
1229+
core.clearCache(uri)
1230+
end
1231+
local results, unchanged = core.pullDiagnostic(uri, false)
1232+
if unchanged then
1233+
return {
1234+
kind = 'unchanged',
1235+
resultId = uri,
1236+
}
1237+
else
1238+
return {
1239+
kind = 'full',
1240+
resultId = uri,
1241+
items = results or {},
1242+
}
1243+
end
1244+
end
1245+
}
1246+
12151247
local function refreshStatusBar()
12161248
local valid = config.get(nil, 'Lua.window.statusBar')
12171249
for _, scp in ipairs(workspace.folders) do

0 commit comments

Comments
 (0)