summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/infer.lua4
-rw-r--r--script/core/noder.lua78
-rw-r--r--script/core/searcher.lua28
-rw-r--r--script/vm/getDocs.lua4
-rw-r--r--script/vm/getGlobals.lua4
5 files changed, 75 insertions, 43 deletions
diff --git a/script/core/infer.lua b/script/core/infer.lua
index 2d791bc0..ddf51d76 100644
--- a/script/core/infer.lua
+++ b/script/core/infer.lua
@@ -515,12 +515,12 @@ function m.searchInfers(source, field, mark)
if id then
local node = noder.getNodeByID(source, id)
if node and node.source then
- noder.eachSource(node, function (src)
+ for src in noder.eachSource(node) do
if not mark[src] then
mark[src] = true
searchInfer(src, infers, mark)
end
- end)
+ end
end
end
end
diff --git a/script/core/noder.lua b/script/core/noder.lua
index 20b2655a..3dabf66c 100644
--- a/script/core/noder.lua
+++ b/script/core/noder.lua
@@ -464,45 +464,75 @@ function m.pushSource(noders, source, id)
node.sources[#node.sources+1] = source
end
+local DUMMY_FUNCTION = function () end
+
---遍历关联单元
---@param node node
----@param callback fun(source:parser.guide.object)
-function m.eachSource(node, callback)
- if node.source then
- callback(node.source)
- end
- if node.sources then
- for _, source in ipairs(node.sources) do
- callback(source)
+---@return fun():parser.guide.object
+function m.eachSource(node)
+ if not node.source then
+ return DUMMY_FUNCTION
+ end
+ local index
+ local sources = node.sources
+ return function ()
+ if not index then
+ index = 0
+ return node.source
+ end
+ if not sources then
+ return nil
end
+ index = index + 1
+ return sources[index]
end
end
---遍历forward
---@param node node
----@param callback fun(forwardID:string, tag:string)
-function m.eachForward(node, callback)
- if node.forward then
- callback(node.forward, node.ftag)
- end
- if node.forwards then
- for _, id in ipairs(node.forwards) do
- callback(id, node.forwards[id])
+---@return fun():string, string
+function m.eachForward(node)
+ if not node.forward then
+ return DUMMY_FUNCTION
+ end
+ local index
+ local forwards = node.forwards
+ return function ()
+ if not index then
+ index = 0
+ return node.forward, node.ftag
end
+ if not forwards then
+ return nil
+ end
+ index = index + 1
+ local id = forwards[index]
+ local tag = forwards[id]
+ return id, tag
end
end
---遍历backward
---@param node node
----@param callback fun(backwardID:string, tag:string)
-function m.eachBackward(node, callback)
- if node.backward then
- callback(node.backward, node.btag)
- end
- if node.backwards then
- for _, id in ipairs(node.backwards) do
- callback(id, node.backwards[id])
+---@return fun():string, string
+function m.eachBackward(node)
+ if not node.backward then
+ return DUMMY_FUNCTION
+ end
+ local index
+ local backwards = node.backwards
+ return function ()
+ if not index then
+ index = 0
+ return node.backward, node.btag
+ end
+ if not backwards then
+ return nil
end
+ index = index + 1
+ local id = backwards[index]
+ local tag = backwards[id]
+ return id, tag
end
end
diff --git a/script/core/searcher.lua b/script/core/searcher.lua
index effbdab4..f86deddf 100644
--- a/script/core/searcher.lua
+++ b/script/core/searcher.lua
@@ -441,9 +441,9 @@ function m.searchRefsByID(status, uri, expect, mode)
end
local function checkForward(id, node, field)
- noder.eachForward(node, function (forwardID, tag)
+ for forwardID, tag in noder.eachForward(node) do
if not checkThenPushTag('forward', tag) then
- return
+ goto CONTINUE
end
local targetUri, targetID = noder.getUriAndID(forwardID)
if targetUri and not files.eq(targetUri, uri) then
@@ -452,7 +452,8 @@ function m.searchRefsByID(status, uri, expect, mode)
searchID(targetID or forwardID, field)
end
popTag('forward', tag)
- end)
+ ::CONTINUE::
+ end
end
local function checkBackward(id, node, field)
@@ -462,12 +463,12 @@ function m.searchRefsByID(status, uri, expect, mode)
if ignoredIDs[id] then
return
end
- noder.eachBackward(node, function (backwardID, tag)
+ for backwardID, tag in noder.eachBackward(node) do
if tag == 'deep' and mode ~= 'allref' and mode ~= 'alldef' then
- return
+ goto CONTINUE
end
if not checkThenPushTag('backward', tag) then
- return
+ goto CONTINUE
end
local targetUri, targetID = noder.getUriAndID(backwardID)
if targetUri and not files.eq(targetUri, uri) then
@@ -476,7 +477,8 @@ function m.searchRefsByID(status, uri, expect, mode)
searchID(targetID or backwardID, field)
end
popTag('backward', tag)
- end)
+ ::CONTINUE::
+ end
end
local function checkSpecial(id, field)
@@ -580,10 +582,10 @@ function m.searchRefsByID(status, uri, expect, mode)
callStack[#callStack+1] = node.call
end
if field == nil and node.source and not ignoredSources[id] then
- noder.eachSource(node, function (source)
+ for source in noder.eachSource(node) do
local force = genericCallArgs[source]
m.pushResult(status, mode, source, force)
- end)
+ end
end
if node.require then
@@ -771,9 +773,9 @@ local function searchAllGlobalByUri(status, mode, uri, fullID)
for id, node in pairs(noders) do
if node.source
and id == fullID then
- noder.eachSource(node, function (source)
+ for source in noder.eachSource(node) do
m.pushResult(status, mode, source)
- end)
+ end
end
end
else
@@ -781,9 +783,9 @@ local function searchAllGlobalByUri(status, mode, uri, fullID)
if node.source
and id:sub(1, 2) == 'g:'
and not id:find(noder.SPLIT_CHAR) then
- noder.eachSource(node, function (source)
+ for source in noder.eachSource(node) do
m.pushResult(status, mode, source)
- end)
+ end
end
end
end
diff --git a/script/vm/getDocs.lua b/script/vm/getDocs.lua
index 533cd78d..79e60fed 100644
--- a/script/vm/getDocs.lua
+++ b/script/vm/getDocs.lua
@@ -19,12 +19,12 @@ function vm.getDocDefines(name)
local results = {}
local id = 'def:dn:' .. name
for node in collector.each(id) do
- noder.eachSource(node, function (source)
+ for source in noder.eachSource(node) do
if source.type == 'doc.class.name'
or source.type == 'doc.alias.name' then
results[#results+1] = source
end
- end)
+ end
end
cache[name] = results
return results
diff --git a/script/vm/getGlobals.lua b/script/vm/getGlobals.lua
index f32a84b1..696f218e 100644
--- a/script/vm/getGlobals.lua
+++ b/script/vm/getGlobals.lua
@@ -22,9 +22,9 @@ function vm.getGlobalSets(name)
id = ('def:g:%q'):format(name)
end
for node in collector.each(id) do
- noder.eachSource(node, function (source)
+ for source in noder.eachSource(node) do
results[#results+1] = source
- end)
+ end
end
return results
end