diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-01-25 20:05:35 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-01-25 20:05:35 +0800 |
commit | 9bf5c99c7676d04dead6ea84a0e8b8518c1dae7e (patch) | |
tree | a326bb4255d9964703127d1800f491870f9a98dd | |
parent | 7667109acd76492c82b17fbe57666b5db675d7ed (diff) | |
download | lua-language-server-9bf5c99c7676d04dead6ea84a0e8b8518c1dae7e.zip |
supports cancel workspace diagnostic
-rw-r--r-- | locale/en-us/script.lua | 2 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 2 | ||||
-rw-r--r-- | script/progress.lua | 70 | ||||
-rw-r--r-- | script/provider/diagnostic.lua | 35 | ||||
-rw-r--r-- | script/provider/provider.lua | 5 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 3 |
6 files changed, 79 insertions, 38 deletions
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index 6d82c3ab..006992c3 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -59,6 +59,8 @@ MWS_UCONFIG_UPDATED = 'User setting updated.' MWS_WCONFIG_UPDATED = 'Workspace setting updated.' WORKSPACE_SKIP_LARGE_FILE = 'Too large file: {} skipped. The currently set size limit is: {} KB, and the file size is: {} KB.' +WORKSPACE_LOADING = 'Loading workspace' +WORKSPACE_DIAGNOSTIC = 'Diagnosing workspace' PARSER_CRASH = 'Parser crashed! Last words:{}' PARSER_UNKNOWN = 'Unknown syntax error...' diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index c3627dc6..cfb6f983 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -59,6 +59,8 @@ MWS_UCONFIG_UPDATED = '用户配置已更新。' MWS_WCONFIG_UPDATED = '工作区配置已更新。' WORKSPACE_SKIP_LARGE_FILE = '已跳过过大的文件:{}。当前设置的大小限制为:{} KB,该文件大小为:{} KB' +WORKSPACE_LOADING = '正在加载工作目录' +WORKSPACE_DIAGNOSTIC = '正在对工作目录进行诊断' PARSER_CRASH = '语法解析崩溃了!遗言:{}' PARSER_UNKNOWN = '未知语法错误...' diff --git a/script/progress.lua b/script/progress.lua index 066b5d76..6f7e1315 100644 --- a/script/progress.lua +++ b/script/progress.lua @@ -16,11 +16,12 @@ mt._title = nil mt._message = nil mt._removed = false mt._clock = 0.0 -mt._delay = 1.0 +mt._delay = 0.0 mt._percentage = nil mt._showed = false mt._dirty = true mt._updated = 0.0 +mt._onCancel = nil ---移除进度条 function mt:remove() @@ -64,31 +65,56 @@ function mt:setPercentage(per) self:_update() end +---取消事件 +function mt:onCancel(callback) + self._onCancel = callback + self:_update() +end + function mt:_update() if self._removed then return end - if not self._showed then + if not self._dirty then return end - if not self._dirty then + self._dirty = false + if not self._showed + and self._clock + self._delay <= os.clock() then + self._showed = true + self._updated = os.clock() + proto.request('window/workDoneProgress/create', { + token = self._token, + }) + proto.notify('$/progress', { + token = self._token, + value = { + kind = 'begin', + title = self._title, + cancellable = self._onCancel ~= nil, + message = self._message, + percentage = self._percentage, + } + }) + log.info('Create progress:', self._token, self._title) + return + end + if not self._showed then return end if os.clock() - self._updated < 0.05 then return end - self._dirty = false self._updated = os.clock() proto.notify('$/progress', { token = self._token, value = { kind = 'report', - cancellable = false, message = self._message, percentage = self._percentage, } }) - log.info('Report progress:', self._token, self._title, self._message) + log.info('Report progress:', self._token, self._title, self._message, self._percentage) end function mt:__close() @@ -97,31 +123,11 @@ function mt:__close() end function m.update() - local clock = os.clock() ---@param prog progress - for token, prog in pairs(m.map) do + for _, prog in pairs(m.map) do if prog._removed then goto CONTINUE end - if not prog._showed - and prog._clock + prog._delay <= clock then - prog._showed = true - proto.request('window/workDoneProgress/create', { - token = token, - }) - proto.notify('$/progress', { - token = token, - value = { - kind = 'begin', - title = prog._title, - cancellable = false, - message = prog._message, - percentage = prog._percentage, - } - }) - log.info('Create progress:', token, prog._title) - goto CONTINUE - end prog:_update() ::CONTINUE:: end @@ -143,6 +149,16 @@ function m.create(title, delay) return prog end +---取消一个进度条 +function m.cancel(token) + local prog = m.map[token] + if not prog then + return + end + xpcall(prog._onCancel, log.error, prog) + prog:remove() +end + timer.loop(0.1, m.update) return m diff --git a/script/provider/diagnostic.lua b/script/provider/diagnostic.lua index 9be9f87a..de8c3be1 100644 --- a/script/provider/diagnostic.lua +++ b/script/provider/diagnostic.lua @@ -1,12 +1,13 @@ -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 core = require 'core.diagnostics' -local util = require 'utility' -local ws = require 'workspace' +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 core = require 'core.diagnostics' +local util = require 'utility' +local ws = require 'workspace' +local progress = require "progress" local m = {} m._start = false @@ -254,10 +255,24 @@ function m.diagnosticsAll() await.sleep(delay) m.diagnosticsAllClock = os.clock() local clock = os.clock() - for uri in files.eachFile() do + local bar <close> = progress.create(lang.script.WORKSPACE_DIAGNOSTIC) + local cancelled + bar:onCancel(function () + log.debug('Cancel workspace diagnostics') + cancelled = true + end) + local uris = files.getAllUris() + for i, uri in ipairs(uris) do + bar:setMessage(('%d/%d'):format(i, #uris)) + bar:setPercentage(i / #uris * 100) m.doDiagnostic(uri) await.delay() + if cancelled then + log.debug('Break workspace diagnostics') + break + end end + bar:remove() log.debug('全文诊断耗时:', os.clock() - clock) end, 'files.version', 'diagnosticsAll') end diff --git a/script/provider/provider.lua b/script/provider/provider.lua index ef300e02..0b880ff7 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -691,3 +691,8 @@ proto.on('textDocument/foldingRange', function (params) return results end) + +proto.on('window/workDoneProgress/cancel', function (params) + local progress = require 'progress' + progress.cancel(params.token) +end) diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 06bb900d..6f99891e 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -229,11 +229,12 @@ function m.awaitPreload() local diagnostic = require 'provider.diagnostic' await.close 'preload' await.setID 'preload' + await.sleep(0.1) diagnostic.pause() m.libraryMatchers = nil m.nativeMatcher = nil m.cache = {} - local progressBar <close> = progress.create('正在加载文件', 0) + local progressBar <close> = progress.create(lang.script.WORKSPACE_LOADING) local progressData = { max = 0, read = 0, |