summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
Diffstat (limited to 'script')
-rw-r--r--script/progress.lua70
-rw-r--r--script/provider/diagnostic.lua35
-rw-r--r--script/provider/provider.lua5
-rw-r--r--script/workspace/workspace.lua3
4 files changed, 75 insertions, 38 deletions
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,