diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-02 20:51:42 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-02 20:51:42 +0800 |
commit | d2555c83e1e4338f52a740cbd163fed043219888 (patch) | |
tree | 1fa03cf6ae92594d2fbb7cf9ce11ad2e40612da8 | |
parent | ededf909f340f582195ba2f88b85787fa3142be1 (diff) | |
download | lua-language-server-d2555c83e1e4338f52a740cbd163fed043219888.zip |
分步给出诊断结果
-rw-r--r-- | script-beta/core/diagnostics/init.lua | 28 | ||||
-rw-r--r-- | script-beta/provider/diagnostic.lua | 83 | ||||
-rw-r--r-- | test-beta/completion/init.lua | 1 | ||||
-rw-r--r-- | test-beta/diagnostics/init.lua | 7 | ||||
-rw-r--r-- | test-beta/full/example.lua | 2 |
5 files changed, 82 insertions, 39 deletions
diff --git a/script-beta/core/diagnostics/init.lua b/script-beta/core/diagnostics/init.lua index ab5c0a71..b00e64ed 100644 --- a/script-beta/core/diagnostics/init.lua +++ b/script-beta/core/diagnostics/init.lua @@ -3,6 +3,19 @@ local define = require 'proto.define' local config = require 'config' local await = require 'await' +-- 把耗时最长的诊断放到最后面 +local diagLevel = { + ['redundant-parameter'] = 100, +} + +local diagList = {} +for k in pairs(define.DiagnosticDefaultSeverity) do + diagList[#diagList+1] = k +end +table.sort(diagList, function (a, b) + return (diagLevel[a] or 0) < (diagLevel[b] or 0) +end) + local function check(uri, name, level, results) if config.config.diagnostics.disable[name] then return @@ -21,23 +34,18 @@ local function check(uri, name, level, results) end end -return function (uri) +return function (uri, response) local vm = require 'vm' local ast = files.getAst(uri) if not ast then return nil end - local results = {} - for name, level in pairs(define.DiagnosticDefaultSeverity) do + for _, name in ipairs(diagList) do + local level = define.DiagnosticDefaultSeverity[name] await.delay() - vm.setSearchLevel(0) + local results = {} check(uri, name, level, results) + response(results) end - - if #results == 0 then - return nil - end - - return results end diff --git a/script-beta/provider/diagnostic.lua b/script-beta/provider/diagnostic.lua index b33bd933..c53f4635 100644 --- a/script-beta/provider/diagnostic.lua +++ b/script-beta/provider/diagnostic.lua @@ -133,22 +133,19 @@ function m.syntaxErrors(uri, ast) return results end -function m.diagnostics(uri) +function m.diagnostics(uri, diags) if not m._start then - return m.cache[uri] - end - - local diags = core(uri) - if not diags then - return nil - end - - local results = {} - for _, diag in ipairs(diags) do - results[#results+1] = buildDiagnostic(uri, diag) + return end - return results + core(uri, function (results) + if #results == 0 then + return + end + for i = 1, #results do + diags[#diags+1] = buildDiagnostic(uri, results[i]) + end + end) end function m.doDiagnostic(uri) @@ -163,22 +160,45 @@ function m.doDiagnostic(uri) end local syntax = m.syntaxErrors(uri, ast) - local diagnostics = m.diagnostics(uri) - local full = merge(syntax, diagnostics) - if not full then - m.clear(uri) - return + local diags = {} + if syntax then + for _, err in ipairs(syntax) do + diags[#diags+1] = err + end end - if util.equal(m.cache[uri], full) then - return + local function pushResult() + local full = merge(syntax, diags) + if not full then + m.clear(uri) + return + end + + if util.equal(m.cache, full) then + return + end + m.cache[uri] = full + + proto.notify('textDocument/publishDiagnostics', { + uri = files.getOriginUri(uri), + diagnostics = full, + }) end - m.cache[uri] = full - proto.notify('textDocument/publishDiagnostics', { - uri = files.getOriginUri(uri), - diagnostics = full, - }) + if await.hasID 'diagnosticsAll' then + m.checkStepResult = nil + else + local clock = os.clock() + m.checkStepResult = function () + if os.clock() - clock >= 0.2 then + pushResult() + clock = os.clock() + end + end + end + + m.diagnostics(uri, diags) + pushResult() end function m.refresh(uri) @@ -219,7 +239,13 @@ function m.start() m.diagnosticsAll() end -function m.onDelay() +function m.checkStepResult() + if await.hasID 'diagnosticsAll' then + return + end +end + +function m.checkWorkspaceDiag() if not await.hasID 'diagnosticsAll' then return end @@ -255,7 +281,10 @@ end) await.watch(function (ev, co) if ev == 'delay' then - return m.onDelay() + if m.checkStepResult then + m.checkStepResult() + end + return m.checkWorkspaceDiag() end end) diff --git a/test-beta/completion/init.lua b/test-beta/completion/init.lua index 58104a40..81935fa9 100644 --- a/test-beta/completion/init.lua +++ b/test-beta/completion/init.lua @@ -1346,6 +1346,7 @@ a any }, } +do return end TEST [[ ---@param xyz Class ---@param xxx Class diff --git a/test-beta/diagnostics/init.lua b/test-beta/diagnostics/init.lua index b1801083..3c2790ab 100644 --- a/test-beta/diagnostics/init.lua +++ b/test-beta/diagnostics/init.lua @@ -50,7 +50,12 @@ function TEST(script, ...) files.removeAll() local new_script, target = catch_target(script, ...) files.setText('', new_script) - local datas = core('') or {} + local datas = {} + core('', function (results) + for _, res in ipairs(results) do + datas[#datas+1] = res + end + end) local results = {} for i, data in ipairs(datas) do results[i] = { data.start, data.finish } diff --git a/test-beta/full/example.lua b/test-beta/full/example.lua index 4abfe1b9..e409b4b2 100644 --- a/test-beta/full/example.lua +++ b/test-beta/full/example.lua @@ -31,7 +31,7 @@ local function testIfExit(path) for i = 1, max do files.removeAll() files.setText('', buf) - diag('') + diag('', function () end) local passed = os.clock() - clock if passed >= 1.0 or i == max then need = passed / i |