From d2555c83e1e4338f52a740cbd163fed043219888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 2 Nov 2020 20:51:42 +0800 Subject: =?UTF-8?q?=E5=88=86=E6=AD=A5=E7=BB=99=E5=87=BA=E8=AF=8A=E6=96=AD?= =?UTF-8?q?=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-beta/core/diagnostics/init.lua | 28 +++++++----- script-beta/provider/diagnostic.lua | 83 +++++++++++++++++++++++------------ 2 files changed, 74 insertions(+), 37 deletions(-) (limited to 'script-beta') 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) -- cgit debian/1.2.3+git2.25.1-1-2-gaceb0