summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-06-07 20:15:05 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-06-07 20:15:05 +0800
commit4b87fcfa6ccea234a4878a95ffb2c9bbf32c6f17 (patch)
tree6e5c57c3faa6e0a4e363d21e92e0e88c2cf94cb3 /script
parent83b2804fb7e2219b7147ebd7bc877fa47816bbb9 (diff)
downloadlua-language-server-4b87fcfa6ccea234a4878a95ffb2c9bbf32c6f17.zip
#1192 improve `[[--@as number]]`
cache ases and search with dichotomy
Diffstat (limited to 'script')
-rw-r--r--script/vm/compiler.lua43
1 files changed, 38 insertions, 5 deletions
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 797fa901..1c3be7c8 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -519,14 +519,47 @@ local function bindAs(source)
if not docs then
return
end
- for _, doc in ipairs(docs) do
- if doc.type == 'doc.as' and doc.originalComment.start == source.finish + 2 then
- if doc.as then
- vm.setNode(source, vm.compileNode(doc.as), true)
+ local ases = docs._asCache
+ if not ases then
+ ases = {}
+ docs._asCache = ases
+ for _, doc in ipairs(docs) do
+ if doc.type == 'doc.as' and doc.as then
+ ases[#ases+1] = doc
end
- return true
+ end
+ table.sort(ases, function (a, b)
+ return a.start < b.start
+ end)
+ end
+
+ local max = #ases
+ local index
+ local left = 1
+ local right = max
+ for _ = 1, 1000 do
+ index = left + (right - left) // 2
+ if index <= left then
+ index = left
+ break
+ elseif index >= right then
+ index = right
+ break
+ end
+ local doc = ases[index]
+ if doc.originalComment.start < source.finish + 2 then
+ left = index + 1
+ else
+ right = index
end
end
+
+ local doc = ases[index]
+ if doc and doc.originalComment.start == source.finish + 2 then
+ vm.setNode(source, vm.compileNode(doc.as), true)
+ return true
+ end
+
return false
end