summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-06-18 19:16:58 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-06-18 19:16:58 +0800
commit32cddbd58e15ae8b0da84b6699cc542daa8ed459 (patch)
tree916e2a0ed11bae8e01574fe5e89eb4151fa4f223 /script/core
parent805d1c96d07c42a6247b1408395d87df763139c1 (diff)
downloadlua-language-server-32cddbd58e15ae8b0da84b6699cc542daa8ed459.zip
improve
Diffstat (limited to 'script/core')
-rw-r--r--script/core/infer.lua6
-rw-r--r--script/core/noder.lua92
-rw-r--r--script/core/searcher.lua42
3 files changed, 104 insertions, 36 deletions
diff --git a/script/core/infer.lua b/script/core/infer.lua
index a2c12fba..6a795dd4 100644
--- a/script/core/infer.lua
+++ b/script/core/infer.lua
@@ -500,13 +500,13 @@ function m.searchInfers(source, field, mark)
local id = noder.getID(source)
if id then
local node = noder.getNodeByID(source, id)
- if node and node.sources then
- for _, src in ipairs(node.sources) do
+ if node and node.source then
+ noder.eachSource(node, function (src)
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 8fd7603c..2ef34e80 100644
--- a/script/core/noder.lua
+++ b/script/core/noder.lua
@@ -22,11 +22,21 @@ local URI_REGEX = URI_CHAR .. '([^' .. URI_CHAR .. ']*)' .. URI_CHAR .. '(.
-- 当前节点的id
---@field id string
-- 使用该ID的单元
+---@field source parser.guide.object
+-- 使用该ID的单元
---@field sources parser.guide.object[]
-- 前进的关联ID
----@field forward string[]
+---@field forward string
+-- 第一个前进关联的tag
+---@field ftag string|boolean
+-- 前进的关联ID
+---@field forwards string[]
-- 后退的关联ID
----@field backward string[]
+---@field backward string
+-- 第一个后退关联的tag
+---@field btag string|boolean
+-- 后退的关联ID
+---@field backwards string[]
-- 函数调用参数信息(用于泛型)
---@field call parser.guide.object
@@ -339,13 +349,21 @@ local function pushForward(noders, id, forwardID, tag)
end
local node = getNode(noders, id)
if not node.forward then
- node.forward = {}
+ node.forward = forwardID
+ node.ftag = tag
+ return
+ end
+ if node.forward == forwardID then
+ return
+ end
+ if not node.forwards then
+ node.forwards = {}
end
- if node.forward[forwardID] ~= nil then
+ if node.forwards[forwardID] ~= nil then
return
end
- node.forward[forwardID] = tag or false
- node.forward[#node.forward+1] = forwardID
+ node.forwards[forwardID] = tag or false
+ node.forwards[#node.forwards+1] = forwardID
end
---添加关联的后退ID
@@ -361,13 +379,21 @@ local function pushBackward(noders, id, backwardID, tag)
end
local node = getNode(noders, id)
if not node.backward then
- node.backward = {}
+ node.backward = backwardID
+ node.btag = tag
+ return
+ end
+ if node.backward == backwardID then
+ return
end
- if node.backward[backwardID] ~= nil then
+ if not node.backwards then
+ node.backwards = {}
+ end
+ if node.backwards[backwardID] ~= nil then
return
end
- node.backward[backwardID] = tag or false
- node.backward[#node.backward+1] = backwardID
+ node.backwards[backwardID] = tag or false
+ node.backwards[#node.backwards+1] = backwardID
end
local m = {}
@@ -409,12 +435,58 @@ function m.pushSource(noders, source, id)
return
end
local node = getNode(noders, id)
+ if not node.source then
+ node.source = source
+ return
+ end
if not node.sources then
node.sources = {}
end
node.sources[#node.sources+1] = source
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)
+ end
+ 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])
+ end
+ 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])
+ end
+ end
+end
+
local function bindValue(noders, source, id)
local value = source.value
if not value then
diff --git a/script/core/searcher.lua b/script/core/searcher.lua
index 839417fd..75176961 100644
--- a/script/core/searcher.lua
+++ b/script/core/searcher.lua
@@ -406,10 +406,9 @@ function m.searchRefsByID(status, uri, expect, mode)
end
local function checkForward(id, node, field)
- for _, forwardID in ipairs(node.forward) do
- local tag = node.forward[forwardID]
+ noder.eachForward(node, function (forwardID, tag)
if not checkThenPushTag('forward', tag) then
- goto CONTINUE
+ return
end
local targetUri, targetID = noder.getUriAndID(forwardID)
if targetUri and not files.eq(targetUri, uri) then
@@ -418,21 +417,19 @@ function m.searchRefsByID(status, uri, expect, mode)
searchID(targetID or forwardID, field)
end
popTag('forward', tag)
- ::CONTINUE::
- end
+ end)
end
local function checkBackward(id, node, field)
if mode ~= 'ref' and mode ~= 'field' and mode ~= 'allref' and not field then
return
end
- for _, backwardID in ipairs(node.backward) do
- local tag = node.backward[backwardID]
+ noder.eachBackward(node, function (backwardID, tag)
if tag == 'deep' and mode ~= 'allref' then
- goto CONTINUE
+ return
end
if not checkThenPushTag('backward', tag) then
- goto CONTINUE
+ return
end
local targetUri, targetID = noder.getUriAndID(backwardID)
if targetUri and not files.eq(targetUri, uri) then
@@ -441,8 +438,7 @@ function m.searchRefsByID(status, uri, expect, mode)
searchID(targetID or backwardID, field)
end
popTag('backward', tag)
- ::CONTINUE::
- end
+ end)
end
local function checkSpecial(id, field)
@@ -543,11 +539,11 @@ function m.searchRefsByID(status, uri, expect, mode)
if node.call then
callStack[#callStack+1] = node.call
end
- if field == nil and node.sources then
- for _, source in ipairs(node.sources) do
+ if field == nil and node.source then
+ noder.eachSource(node, function (source)
local force = genericCallArgs[source]
m.pushResult(status, mode, source, force)
- end
+ end)
end
if node.require then
@@ -561,9 +557,9 @@ function m.searchRefsByID(status, uri, expect, mode)
checkBackward(id, node, field)
end
- if node.sources then
- checkGeneric(node.sources[1], field)
- checkENV(node.sources[1], field)
+ if node.source then
+ checkGeneric(node.source, field)
+ checkENV(node.source, field)
end
if mode == 'allref' then
@@ -706,21 +702,21 @@ local function searchAllGlobalByUri(status, mode, uri, fullID)
local noders = noder.getNoders(root)
if fullID then
for id, node in pairs(noders) do
- if node.sources
+ if node.source
and id == fullID then
- for _, source in ipairs(node.sources) do
+ noder.eachSource(node, function (source)
m.pushResult(status, mode, source)
- end
+ end)
end
end
else
for id, node in pairs(noders) do
- if node.sources
+ if node.source
and id:sub(1, 2) == 'g:'
and not id:find(noder.SPLIT_CHAR) then
- for _, source in ipairs(node.sources) do
+ noder.eachSource(node, function (source)
m.pushResult(status, mode, source)
- end
+ end)
end
end
end