summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script-beta/core/semantic-tokens.lua5
-rw-r--r--script-beta/parser/guide.lua34
2 files changed, 35 insertions, 4 deletions
diff --git a/script-beta/core/semantic-tokens.lua b/script-beta/core/semantic-tokens.lua
index 8947bd88..2927a295 100644
--- a/script-beta/core/semantic-tokens.lua
+++ b/script-beta/core/semantic-tokens.lua
@@ -126,14 +126,11 @@ return function (uri, start, finish)
local results = {}
local count = 0
- guide.eachSource(ast.ast, function (source)
+ guide.eachSourceBetween(ast.ast, start, finish, function (source)
local method = Care[source.type]
if not method then
return
end
- if source.start > finish or source.finish < start then
- return
- end
method(source, results)
count = count + 1
if count % 100 == 0 then
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua
index e67d34f3..57d2d402 100644
--- a/script-beta/parser/guide.lua
+++ b/script-beta/parser/guide.lua
@@ -339,6 +339,14 @@ function m.isInRange(source, offset)
return (source.vstart or source.start) <= offset and (source.range or source.finish) >= offset - 1
end
+function m.isBetween(source, start, finish)
+ return source.start <= finish and source.finish >= start - 1
+end
+
+function m.isBetweenRange(source, start, finish)
+ return (source.vstart or source.start) <= finish and (source.range or source.finish) >= start - 1
+end
+
--- 添加child
function m.addChilds(list, obj, map)
local keys = map[obj.type]
@@ -382,6 +390,32 @@ function m.eachSourceContain(ast, offset, callback)
end
end
+--- 遍历所有在某个范围内的source
+function m.eachSourceBetween(ast, start, finish, callback)
+ local list = { ast }
+ local mark = {}
+ while true do
+ local len = #list
+ if len == 0 then
+ return
+ end
+ local obj = list[len]
+ list[len] = nil
+ if not mark[obj] then
+ mark[obj] = true
+ if m.isBetweenRange(obj, start, finish) then
+ if m.isBetween(obj, start, finish) then
+ local res = callback(obj)
+ if res ~= nil then
+ return res
+ end
+ end
+ m.addChilds(list, obj, m.childMap)
+ end
+ end
+ end
+end
+
--- 遍历所有指定类型的source
function m.eachSourceType(ast, type, callback)
local cache = ast.typeCache