diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-08 14:25:00 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-08 14:25:00 +0800 |
commit | d8c8c6ded148a8e130956b08010db3a0cda8585a (patch) | |
tree | 115ec85b2d498f0522d58899ef4eefa15e49286d | |
parent | 0a2df61286035af2bd506605547f6a5d5024c691 (diff) | |
download | lua-language-server-d8c8c6ded148a8e130956b08010db3a0cda8585a.zip |
优化诊断性能
-rw-r--r-- | server-beta/src/files.lua | 2 | ||||
-rw-r--r-- | server-beta/src/provider/diagnostic.lua (renamed from server-beta/src/service/diagnostic.lua) | 75 | ||||
-rw-r--r-- | server-beta/src/workspace/workspace.lua | 2 |
3 files changed, 60 insertions, 19 deletions
diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua index c0aa5379..03a1589e 100644 --- a/server-beta/src/files.lua +++ b/server-beta/src/files.lua @@ -74,7 +74,7 @@ function m.setText(uri, text) file.globals = nil searcher.refreshCache() - local diagnostic = require 'service.diagnostic' + local diagnostic = require 'provider.diagnostic' diagnostic.refresh(originUri) end diff --git a/server-beta/src/service/diagnostic.lua b/server-beta/src/provider/diagnostic.lua index 6e5d77c8..2b4ae9b3 100644 --- a/server-beta/src/service/diagnostic.lua +++ b/server-beta/src/provider/diagnostic.lua @@ -10,6 +10,7 @@ local m = {} m.version = 0 m._start = false +m.cache = {} local function concat(t, sep) if type(t) ~= 'table' then @@ -86,32 +87,69 @@ local function buildDiagnostic(uri, diag) } end -function m.doDiagnostic(uri, syntaxOnly) - local ast = files.getAst(uri) - if not ast then - proto.notify('textDocument/publishDiagnostics', { - uri = uri, - diagnostics = {}, - }) - return +local function merge(a, b) + local t = {} + if a then + for i = 1, #a do + t[#t+1] = a[i] + end + end + if b then + for i = 1, #b do + t[#t+1] = b[i] + end end + return t +end - local diagnostics = {} +function m.clear(uri) + m.cache[uri] = nil + proto.notify('textDocument/publishDiagnostics', { + uri = uri, + diagnostics = {}, + }) +end + +function m.syntaxErrors(uri, ast) + local results = {} for _, err in ipairs(ast.errs) do - diagnostics[#diagnostics+1] = buildSyntaxError(uri, err) + results[#results+1] = buildSyntaxError(uri, err) end - if not syntaxOnly and m._start then - local diags = core(uri) - for _, diag in ipairs(diags) do - diagnostics[#diagnostics+1] = buildDiagnostic(uri, diag) - end + return results +end + +function m.diagnostics(uri, syntaxOnly) + if syntaxOnly or not m._start then + return m.cache[uri] + end + + local results = {} + + local diags = core(uri) + for _, diag in ipairs(diags) do + results[#results+1] = buildDiagnostic(uri, diag) end + m.cache[uri] = results + + return results +end + +function m.doDiagnostic(uri, syntaxOnly) + local ast = files.getAst(uri) + if not ast then + m.clear(uri) + return + end + + local syntax = m.syntaxErrors(uri, ast) + local diagnostics = m.diagnostics(uri, syntaxOnly) + proto.notify('textDocument/publishDiagnostics', { uri = uri, - diagnostics = diagnostics, + diagnostics = merge(syntax, diagnostics), }) end @@ -125,10 +163,13 @@ function m.refresh(uri) return end await.create(function () - await.sleep(1.0) + await.sleep(0.2) if myVersion ~= m.version then return end + if uri then + m.doDiagnostic(uri) + end for destUri in files.eachFile() do if destUri ~= uri then m.doDiagnostic(files.getOriginUri(destUri)) diff --git a/server-beta/src/workspace/workspace.lua b/server-beta/src/workspace/workspace.lua index 79fd03df..67f4f875 100644 --- a/server-beta/src/workspace/workspace.lua +++ b/server-beta/src/workspace/workspace.lua @@ -6,7 +6,7 @@ local config = require 'config' local glob = require 'glob' local platform = require 'bee.platform' local await = require 'await' -local diagnostic = require 'service.diagnostic' +local diagnostic = require 'provider.diagnostic' local m = {} m.type = 'workspace' |