summaryrefslogtreecommitdiff
path: root/server-beta/src/service
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-08 11:09:58 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-08 11:09:58 +0800
commitd2b9dee8ce0ae345e4efd43c112bb3efd4071888 (patch)
tree125564af4ce2685d2495352e26e0230f40f79a66 /server-beta/src/service
parenta6930417f3da299d4f15f4404b6d3dd900ec38f9 (diff)
downloadlua-language-server-d2b9dee8ce0ae345e4efd43c112bb3efd4071888.zip
语法错误诊断
Diffstat (limited to 'server-beta/src/service')
-rw-r--r--server-beta/src/service/diagnostic.lua93
-rw-r--r--server-beta/src/service/service.lua12
2 files changed, 97 insertions, 8 deletions
diff --git a/server-beta/src/service/diagnostic.lua b/server-beta/src/service/diagnostic.lua
new file mode 100644
index 00000000..6fd212a9
--- /dev/null
+++ b/server-beta/src/service/diagnostic.lua
@@ -0,0 +1,93 @@
+local await = require 'await'
+local proto = require 'proto.proto'
+local define = require 'proto.define'
+local lang = require 'language'
+local files = require 'files'
+local config = require 'config'
+
+local m = {}
+
+m.version = 0
+
+local function concat(t, sep)
+ if type(t) ~= 'table' then
+ return t
+ end
+ return table.concat(t, sep)
+end
+
+local function buildSyntaxError(uri, err)
+ local lines = files.getLines(uri)
+ local text = files.getText(uri)
+ local message = lang.script('PARSER_'..err.type, err.info)
+
+ if err.version then
+ local version = err.info and err.info.version or config.config.runtime.version
+ message = message .. ('(%s)'):format(lang.script('DIAG_NEED_VERSION'
+ , concat(err.version, '/')
+ , version
+ ))
+ end
+
+ local relative = err.info and err.info.relative
+ local relatedInformation
+ if relative then
+ relatedInformation = {}
+ for _, rel in ipairs(relative) do
+ relatedInformation[#relatedInformation+1] = {
+ message = lang.script('PARSER_'..rel.message),
+ location = define.location(uri, define.range(lines, text, rel.start, rel.finish)),
+ }
+ end
+ end
+
+ return {
+ range = define.range(lines, text, err.start, err.finish),
+ severity = define.DiagnosticSeverity.Error,
+ source = lang.script.DIAG_SYNTAX_CHECK,
+ message = message,
+ relatedInformation = relatedInformation,
+ }
+end
+
+function m.doDiagnostic(uri)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+
+ local diagnostics = {}
+
+ for _, err in ipairs(ast.errs) do
+ diagnostics[#diagnostics+1] = buildSyntaxError(uri, err)
+ end
+
+ proto.notify('textDocument/publishDiagnostics', {
+ uri = uri,
+ diagnostics = diagnostics,
+ })
+
+end
+
+function m.refresh(uri)
+ m.version = m.version + 1
+ local myVersion = m.version
+ await.create(function ()
+ m.doDiagnostic(files.getOriginUri(uri))
+ await.sleep(1.0)
+ if myVersion ~= m.version then
+ return
+ end
+ for destUri in files.eachFile() do
+ if destUri ~= uri then
+ m.doDiagnostic(files.getOriginUri(destUri))
+ await.sleep(0.001)
+ if myVersion ~= m.version then
+ return
+ end
+ end
+ end
+ end)
+end
+
+return m
diff --git a/server-beta/src/service/service.lua b/server-beta/src/service/service.lua
index 781de727..6c6d3c90 100644
--- a/server-beta/src/service/service.lua
+++ b/server-beta/src/service/service.lua
@@ -1,6 +1,6 @@
local pub = require 'pub'
local thread = require 'bee.thread'
-local task = require 'task'
+local await = require 'await'
local timer = require 'timer'
local proto = require 'proto'
@@ -33,7 +33,7 @@ function m.reportTask()
local normal = 0
local dead = 0
- for co in pairs(task.coTracker) do
+ for co in pairs(await.coTracker) do
total = total + 1
local status = coroutine.status(co)
if status == 'running' then
@@ -72,18 +72,14 @@ function m.report()
end
function m.startTimer()
- local last = os.clock()
while true do
thread.sleep(0.001)
- local current = os.clock()
- local delta = current - last
- last = current
- timer.update(delta)
+ timer.update()
end
end
function m.start()
- task.setErrorHandle(log.error)
+ await.setErrorHandle(log.error)
pub.recruitBraves(4)
proto.listen()
pub.listen()