diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-08-15 19:17:46 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-08-15 19:17:46 +0800 |
commit | 910fc1dae705ce2475c165ec28e20edd9cb89d61 (patch) | |
tree | bc02339f4e5a73a8a393a941058812bd53d510b0 /script-beta | |
parent | a87e0c7cbd8ff44deb009b12efa8c28473db0470 (diff) | |
download | lua-language-server-910fc1dae705ce2475c165ec28e20edd9cb89d61.zip |
允许给 await 设置一个全局的延迟检查器
Diffstat (limited to 'script-beta')
-rw-r--r-- | script-beta/await.lua | 13 | ||||
-rw-r--r-- | script-beta/files.lua | 14 | ||||
-rw-r--r-- | script-beta/provider/diagnostic.lua | 8 |
3 files changed, 28 insertions, 7 deletions
diff --git a/script-beta/await.lua b/script-beta/await.lua index 5ee9e20b..78f40e66 100644 --- a/script-beta/await.lua +++ b/script-beta/await.lua @@ -4,8 +4,9 @@ local timer = require 'timer' local m = {} m.type = 'await' -m.coTracker = setmetatable({}, { __mode = 'k' }) +m.coTracker = setmetatable({}, { __mode = 'k' }) m.coPriority = setmetatable({}, { __mode = 'k' }) +m.coDelayer = setmetatable({}, { __mode = 'k' }) m.delayQueue = {} m.delayQueueIndex = 1 @@ -30,6 +31,11 @@ function m.create(callback, ...) return m.checkResult(co, coroutine.resume(co, ...)) end +function m.setDelayer(callback) + local co = coroutine.running() + m.coDelayer[co] = callback +end + --- 休眠一段时间 ---@param time number function m.sleep(time, getVersion) @@ -75,8 +81,11 @@ function m.delay(getVersion) return end local version = getVersion and getVersion() + local delayer = m.coDelayer[co] + local dVersion = delayer and delayer() m.delayQueue[#m.delayQueue+1] = function () - if version == (getVersion and getVersion()) then + if version == (getVersion and getVersion()) + and dVersion == (delayer and delayer()) then return m.checkResult(co, coroutine.resume(co)) else coroutine.close(co) diff --git a/script-beta/files.lua b/script-beta/files.lua index ae2ac0da..3d1a2c90 100644 --- a/script-beta/files.lua +++ b/script-beta/files.lua @@ -75,6 +75,7 @@ function m.setText(uri, text) if not m.fileMap[uri] then m.fileMap[uri] = { uri = originUri, + version = 0, } end local file = m.fileMap[uri] @@ -85,10 +86,23 @@ function m.setText(uri, text) file.ast = nil file.lines = nil file.cache = {} + file.version = file.version + 1 m.globalVersion = m.globalVersion + 1 m.onWatch('update', originUri) end +--- 获取文件版本 +function m.getVersion(uri) + if platform.OS == 'Windows' then + uri = uri:lower() + end + local file = m.fileMap[uri] + if not file then + return nil + end + return file.version +end + --- 获取文件文本 ---@param uri string ---@return string text diff --git a/script-beta/provider/diagnostic.lua b/script-beta/provider/diagnostic.lua index 86d80034..5285642a 100644 --- a/script-beta/provider/diagnostic.lua +++ b/script-beta/provider/diagnostic.lua @@ -219,13 +219,11 @@ files.watch(function (env, uri) if env == 'remove' then m.clear(uri) elseif env == 'update' then - if not m._start then - return - end await.create(function () - await.delay(function () - return files.globalVersion + await.setDelayer(function () + return files.getVersion(uri) end) + await.delay() m.doDiagnostic(uri) end) end |