summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2024-05-27 13:51:34 +0800
committerGitHub <noreply@github.com>2024-05-27 13:51:34 +0800
commite2ca72dd54da1cafda36ff9e1266526fec54081f (patch)
tree873532d8ffd366f66023922a1a54ea84bf6d14ba
parent789f06be5c0cb482c386722426f002e079580653 (diff)
parent439c1e79611c240ea977936edc8f1fcadcbf34da (diff)
downloadlua-language-server-e2ca72dd54da1cafda36ff9e1266526fec54081f.zip
Merge pull request #2680 from emmericp/throttle-awaits
Throttle calls to await.delay() in some diagnostics
-rw-r--r--script/await.lua22
-rw-r--r--script/core/diagnostics/assign-type-mismatch.lua3
-rw-r--r--script/core/diagnostics/need-check-nil.lua3
-rw-r--r--script/core/diagnostics/redundant-value.lua3
-rw-r--r--script/core/diagnostics/trailing-space.lua3
-rw-r--r--script/core/diagnostics/unbalanced-assignments.lua3
6 files changed, 32 insertions, 5 deletions
diff --git a/script/await.lua b/script/await.lua
index 22745570..f252197f 100644
--- a/script/await.lua
+++ b/script/await.lua
@@ -176,6 +176,28 @@ function m.delay()
return coroutine.yield()
end
+local throttledDelayer = {}
+throttledDelayer.__index = throttledDelayer
+
+---@async
+function throttledDelayer:delay()
+ if not m._enable then
+ return
+ end
+ self.calls = self.calls + 1
+ if self.calls == self.factor then
+ self.calls = 0
+ return m.delay()
+ end
+end
+
+function m.newThrottledDelayer(factor)
+ return setmetatable({
+ factor = factor,
+ calls = 0,
+ }, throttledDelayer)
+end
+
--- stop then close
---@async
function m.stop()
diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua
index 8472e87c..6fa26553 100644
--- a/script/core/diagnostics/assign-type-mismatch.lua
+++ b/script/core/diagnostics/assign-type-mismatch.lua
@@ -52,13 +52,14 @@ return function (uri, callback)
return
end
+ local delayer = await.newThrottledDelayer(15)
---@async
guide.eachSourceTypes(state.ast, checkTypes, function (source)
local value = source.value
if not value then
return
end
- await.delay()
+ delayer:delay()
if source.type == 'setlocal' then
local locNode = vm.compileNode(source.node)
if not locNode.hasDefined then
diff --git a/script/core/diagnostics/need-check-nil.lua b/script/core/diagnostics/need-check-nil.lua
index 9c86939a..fe72d4ba 100644
--- a/script/core/diagnostics/need-check-nil.lua
+++ b/script/core/diagnostics/need-check-nil.lua
@@ -11,9 +11,10 @@ return function (uri, callback)
return
end
+ local delayer = await.newThrottledDelayer(500)
---@async
guide.eachSourceType(state.ast, 'getlocal', function (src)
- await.delay()
+ delayer:delay()
local checkNil
local nxt = src.next
if nxt then
diff --git a/script/core/diagnostics/redundant-value.lua b/script/core/diagnostics/redundant-value.lua
index 6f60303b..ceb090c9 100644
--- a/script/core/diagnostics/redundant-value.lua
+++ b/script/core/diagnostics/redundant-value.lua
@@ -11,8 +11,9 @@ return function (uri, callback)
return
end
+ local delayer = await.newThrottledDelayer(50000)
guide.eachSource(state.ast, function (src) ---@async
- await.delay()
+ delayer:delay()
if src.redundant then
callback {
start = src.start,
diff --git a/script/core/diagnostics/trailing-space.lua b/script/core/diagnostics/trailing-space.lua
index 2e0398b2..1fdccf80 100644
--- a/script/core/diagnostics/trailing-space.lua
+++ b/script/core/diagnostics/trailing-space.lua
@@ -10,9 +10,10 @@ return function (uri, callback)
if not state or not text then
return
end
+ local delayer = await.newThrottledDelayer(5000)
local lines = state.lines
for i = 0, #lines do
- await.delay()
+ delayer:delay()
local startOffset = lines[i]
local finishOffset = text:find('[\r\n]', startOffset) or (#text + 1)
local lastOffset = finishOffset - 1
diff --git a/script/core/diagnostics/unbalanced-assignments.lua b/script/core/diagnostics/unbalanced-assignments.lua
index c21ca993..f72e1fd5 100644
--- a/script/core/diagnostics/unbalanced-assignments.lua
+++ b/script/core/diagnostics/unbalanced-assignments.lua
@@ -41,9 +41,10 @@ return function (uri, callback, code)
end
end
+ local delayer = await.newThrottledDelayer(1000)
---@async
guide.eachSourceTypes(ast.ast, types, function (source)
- await.delay()
+ delayer:delay()
checkSet(source)
end)
end