diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-08 11:09:58 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-08 11:09:58 +0800 |
commit | d2b9dee8ce0ae345e4efd43c112bb3efd4071888 (patch) | |
tree | 125564af4ce2685d2495352e26e0230f40f79a66 /server-beta/src | |
parent | a6930417f3da299d4f15f4404b6d3dd900ec38f9 (diff) | |
download | lua-language-server-d2b9dee8ce0ae345e4efd43c112bb3efd4071888.zip |
语法错误诊断
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/await.lua (renamed from server-beta/src/task.lua) | 4 | ||||
-rw-r--r-- | server-beta/src/files.lua | 27 | ||||
-rw-r--r-- | server-beta/src/parser/compile.lua | 15 | ||||
-rw-r--r-- | server-beta/src/proto/proto.lua | 6 | ||||
-rw-r--r-- | server-beta/src/proto/provider.lua | 8 | ||||
-rw-r--r-- | server-beta/src/pub/pub.lua | 6 | ||||
-rw-r--r-- | server-beta/src/pub/report.lua | 6 | ||||
-rw-r--r-- | server-beta/src/service/diagnostic.lua | 93 | ||||
-rw-r--r-- | server-beta/src/service/service.lua | 12 | ||||
-rw-r--r-- | server-beta/src/timer.lua | 7 | ||||
-rw-r--r-- | server-beta/src/workspace/workspace.lua | 4 |
11 files changed, 150 insertions, 38 deletions
diff --git a/server-beta/src/task.lua b/server-beta/src/await.lua index edb05fe6..ee64e896 100644 --- a/server-beta/src/task.lua +++ b/server-beta/src/await.lua @@ -1,8 +1,8 @@ local timer = require 'timer' ----@class task +---@class await local m = {} -m.type = 'task' +m.type = 'await' m.coTracker = setmetatable({}, { __mode = 'k' }) diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua index 91fdff35..78610315 100644 --- a/server-beta/src/files.lua +++ b/server-beta/src/files.lua @@ -1,10 +1,10 @@ -local platform = require 'bee.platform' -local config = require 'config' -local glob = require 'glob' -local furi = require 'file-uri' -local parser = require 'parser' -local searcher = require 'searcher.searcher' -local guide = require 'parser.guide' +local platform = require 'bee.platform' +local config = require 'config' +local glob = require 'glob' +local furi = require 'file-uri' +local parser = require 'parser' +local searcher = require 'searcher.searcher' +local guide = require 'parser.guide' local m = {} @@ -31,6 +31,16 @@ function m.close(uri) m.openMap[uri] = nil end +--- 是否打开 +---@param uri string +---@return boolean +function m.isOpen(uri) + if platform.OS == 'Windows' then + uri = uri:lower() + end + return m.openMap[uri] == true +end + --- 是否存在 ---@return boolean function m.exists(uri) @@ -63,6 +73,9 @@ function m.setText(uri, text) file.ast = nil file.globals = nil searcher.refreshCache() + + local diagnostic = require 'service.diagnostic' + diagnostic.refresh(originUri) end --- 监听编译完成 diff --git a/server-beta/src/parser/compile.lua b/server-beta/src/parser/compile.lua index 5da2c0c6..1a3e5a21 100644 --- a/server-beta/src/parser/compile.lua +++ b/server-beta/src/parser/compile.lua @@ -256,8 +256,9 @@ local vmMap = { finish = obj.finish, relative = { { - label.start, - label.finish, + message = 'REDEFINED_LABEL', + start = label.start, + finish = label.finish, } } } @@ -461,12 +462,14 @@ local function compileGoTo(obj) }, relative = { { - start = label.start, - finish = label.finish, + message = 'JUMPED_LABEL', + start = label.start, + finish = label.finish, }, { - start = loc.start, - finish = loc.finish, + message = 'JUMPED_LOCAL', + start = loc.start, + finish = loc.finish, } }, } diff --git a/server-beta/src/proto/proto.lua b/server-beta/src/proto/proto.lua index 27ba68b8..e851610a 100644 --- a/server-beta/src/proto/proto.lua +++ b/server-beta/src/proto/proto.lua @@ -1,6 +1,6 @@ local subprocess = require 'bee.subprocess' local util = require 'utility' -local task = require 'task' +local await = require 'await' local pub = require 'pub' local jsonrpc = require 'jsonrpc' local ErrorCodes = require 'define.ErrorCodes' @@ -60,7 +60,7 @@ function m.request(name, params) params = params, } io.stdout:write(buf) - return task.wait(function (waker) + return await.wait(function (waker) m.waiting[id] = waker end) end @@ -77,7 +77,7 @@ function m.doMethod(proto) end return end - task.create(function () + await.create(function () local clock = os.clock() local ok, res = xpcall(abil, log.error, proto.params) local passed = os.clock() - clock diff --git a/server-beta/src/proto/provider.lua b/server-beta/src/proto/provider.lua index 1f6f6813..08229f29 100644 --- a/server-beta/src/proto/provider.lua +++ b/server-beta/src/proto/provider.lua @@ -1,6 +1,6 @@ local util = require 'utility' local cap = require 'proto.capability' -local task = require 'task' +local await = require 'await' local files = require 'files' local proto = require 'proto.proto' local define = require 'proto.define' @@ -89,7 +89,7 @@ proto.on('initialized', function (params) } } }) - task.create(function () + await.create(function () workspace.preload() end) return true @@ -134,7 +134,9 @@ proto.on('textDocument/didChange', function (params) local change = params.contentChanges local uri = doc.uri local text = change[1].text - files.setText(uri, text) + if files.isLua(uri) or files.isOpen(uri) then + files.setText(uri, text) + end end) proto.on('textDocument/hover', function () diff --git a/server-beta/src/pub/pub.lua b/server-beta/src/pub/pub.lua index f6db6d4f..1591ba31 100644 --- a/server-beta/src/pub/pub.lua +++ b/server-beta/src/pub/pub.lua @@ -1,6 +1,6 @@ local thread = require 'bee.thread' local utility = require 'utility' -local task = require 'task' +local await = require 'await' local timer = require 'timer' local errLog = thread.channel 'errlog' @@ -113,7 +113,7 @@ function m.task(name, params) for _, brave in ipairs(m.braves) do if m.isIdle(brave) then if m.pushTask(brave, info) then - return task.wait(function (waker) + return await.wait(function (waker) info.callback = waker end) else @@ -126,7 +126,7 @@ function m.task(name, params) -- 交给该勇者 m.taskQueue[#m.taskQueue+1] = info log.info(('Add task %q(%d) in queue, length %d.'):format(name, info.id, #m.taskQueue)) - return task.wait(function (waker) + return await.wait(function (waker) info.callback = waker end) end diff --git a/server-beta/src/pub/report.lua b/server-beta/src/pub/report.lua index d73a4e69..edd3ee0e 100644 --- a/server-beta/src/pub/report.lua +++ b/server-beta/src/pub/report.lua @@ -1,5 +1,5 @@ -local pub = require 'pub.pub' -local task = require 'task' +local pub = require 'pub.pub' +local await = require 'await' pub.on('log', function (params, brave) log.raw(brave.id, params.level, params.msg, params.src, params.line) @@ -11,7 +11,7 @@ end) pub.on('proto', function (params) local proto = require 'proto' - task.create(function () + await.create(function () if params.method then proto.doMethod(params) else 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() diff --git a/server-beta/src/timer.lua b/server-beta/src/timer.lua index 0add4c22..1d4343f1 100644 --- a/server-beta/src/timer.lua +++ b/server-beta/src/timer.lua @@ -1,6 +1,7 @@ local setmetatable = setmetatable local mathMax = math.max local mathFloor = math.floor +local osClock = os.clock _ENV = nil @@ -199,7 +200,11 @@ function m.clock() return curFrame / 1000.0 end -function m.update(delta) +local lastClock = osClock() +function m.update() + local currentClock = osClock() + local delta = currentClock - lastClock + lastClock = currentClock if curIndex ~= 0 then curFrame = curFrame - 1 end diff --git a/server-beta/src/workspace/workspace.lua b/server-beta/src/workspace/workspace.lua index 6ee1be42..f65fb178 100644 --- a/server-beta/src/workspace/workspace.lua +++ b/server-beta/src/workspace/workspace.lua @@ -5,7 +5,7 @@ local files = require 'files' local config = require 'config' local glob = require 'glob' local platform = require 'bee.platform' -local task = require 'task' +local await = require 'await' local m = {} m.type = 'workspace' @@ -126,7 +126,7 @@ function m.preload() if read >= max then break end - task.sleep(0.1) + await.sleep(0.1) end log.info('Preload finish.') |