summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-07-06 16:58:58 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-07-06 16:58:58 +0800
commit47f611c9c71bebe0f2ec0b9a52b3c578a1b8e01e (patch)
tree93ab71dcf2b0dc20ff31ed3386303d13d546e9fc /script/core
parent761ce1d1a6ad01f64566c44bd568e4990e251126 (diff)
downloadlua-language-server-47f611c9c71bebe0f2ec0b9a52b3c578a1b8e01e.zip
cleanup
Diffstat (limited to 'script/core')
-rw-r--r--script/core/noder.lua84
-rw-r--r--script/core/searcher.lua60
2 files changed, 94 insertions, 50 deletions
diff --git a/script/core/noder.lua b/script/core/noder.lua
index 2558a22e..49f4613b 100644
--- a/script/core/noder.lua
+++ b/script/core/noder.lua
@@ -29,13 +29,13 @@ local URI_REGEX = URI_CHAR .. '([^' .. URI_CHAR .. ']*)' .. URI_CHAR .. '(.
-- 前进的关联ID
---@field forward string
-- 第一个前进关联的tag
----@field ftag string|boolean
+---@field finfo? node.info
-- 前进的关联ID
---@field forwards string[]
-- 后退的关联ID
---@field backward string
-- 第一个后退关联的tag
----@field btag string|boolean
+---@field binfo? node.info
-- 后退的关联ID
---@field backwards string[]
-- 函数调用参数信息(用于泛型)
@@ -44,6 +44,11 @@ local URI_REGEX = URI_CHAR .. '([^' .. URI_CHAR .. ']*)' .. URI_CHAR .. '(.
---@alias noders table<string, node[]>
+---@class node.info
+---@field reject? string
+---@field deep? boolean
+---@field filter? fun(id: string):boolean
+
---创建source的链接信息
---@param noders noders
---@param id string
@@ -312,10 +317,11 @@ local function getID(source)
end
---添加关联的前进ID
----@param noders noders
----@param id string
+---@param noders noders
+---@param id string
---@param forwardID string
-local function pushForward(noders, id, forwardID, tag)
+---@param info? node.info
+local function pushForward(noders, id, forwardID, info)
if not id
or not forwardID
or forwardID == ''
@@ -325,7 +331,7 @@ local function pushForward(noders, id, forwardID, tag)
local node = getNode(noders, id)
if not node.forward then
node.forward = forwardID
- node.ftag = tag
+ node.finfo = info
return
end
if node.forward == forwardID then
@@ -337,15 +343,16 @@ local function pushForward(noders, id, forwardID, tag)
if node.forwards[forwardID] ~= nil then
return
end
- node.forwards[forwardID] = tag or false
+ node.forwards[forwardID] = info or false
node.forwards[#node.forwards+1] = forwardID
end
---添加关联的后退ID
----@param noders noders
----@param id string
+---@param noders noders
+---@param id string
---@param backwardID string
-local function pushBackward(noders, id, backwardID, tag)
+---@param info? node.info
+local function pushBackward(noders, id, backwardID, info)
if not id
or not backwardID
or backwardID == ''
@@ -355,7 +362,7 @@ local function pushBackward(noders, id, backwardID, tag)
local node = getNode(noders, id)
if not node.backward then
node.backward = backwardID
- node.btag = tag
+ node.binfo = info
return
end
if node.backward == backwardID then
@@ -367,7 +374,7 @@ local function pushBackward(noders, id, backwardID, tag)
if node.backwards[backwardID] ~= nil then
return
end
- node.backwards[backwardID] = tag or false
+ node.backwards[backwardID] = info or false
node.backwards[#node.backwards+1] = backwardID
end
@@ -466,7 +473,7 @@ function m.eachForward(node)
return function ()
if not index then
index = 0
- return node.forward, node.ftag
+ return node.forward, node.finfo
end
if not forwards then
return nil
@@ -480,7 +487,7 @@ end
---遍历backward
---@param node node
----@return fun():string, string
+---@return fun():string, node.info
function m.eachBackward(node)
if not node.backward then
return DUMMY_FUNCTION
@@ -490,7 +497,7 @@ function m.eachBackward(node)
return function ()
if not index then
index = 0
- return node.backward, node.btag
+ return node.backward, node.binfo
end
if not backwards then
return nil
@@ -524,7 +531,9 @@ local function bindValue(noders, source, id)
end
end
-- x = y : x -> y
- pushForward(noders, id, valueID, 'set')
+ pushForward(noders, id, valueID, {
+ reject = 'set',
+ })
-- 参数/call禁止反向查找赋值
local valueType = valueID:match '^(.-:).'
if not valueType then
@@ -533,9 +542,14 @@ local function bindValue(noders, source, id)
if valueType ~= 'p:'
and valueType ~= 's:'
and valueType ~= 'c:' then
- pushBackward(noders, valueID, id, 'set')
+ pushBackward(noders, valueID, id, {
+ reject = 'set',
+ })
else
- pushBackward(noders, valueID, id, 'deep')
+ pushBackward(noders, valueID, id, {
+ reject = 'set',
+ deep = true,
+ })
end
end
@@ -604,7 +618,11 @@ local function compileCallReturn(noders, call, sourceID, returnIndex)
)
end
pushForward(noders, sourceID, tblID)
- pushForward(noders, sourceID, indexID)
+ pushForward(noders, sourceID, indexID, {
+ filter = function (id)
+ return id:sub(1, 2) ~= 'f:'
+ end,
+ })
pushBackward(noders, tblID, sourceID)
--pushBackward(noders, indexID, callID)
return
@@ -614,7 +632,9 @@ local function compileCallReturn(noders, call, sourceID, returnIndex)
if arg1 and arg1.type == 'string' then
getNode(noders, sourceID).require = arg1[1]
end
- pushBackward(noders, callID, sourceID, 'deep')
+ pushBackward(noders, callID, sourceID, {
+ deep = true,
+ })
return
end
if node.special == 'pcall'
@@ -633,7 +653,9 @@ local function compileCallReturn(noders, call, sourceID, returnIndex)
index
)
pushForward(noders, sourceID, pfuncXID)
- pushBackward(noders, pfuncXID, sourceID, 'deep')
+ pushBackward(noders, pfuncXID, sourceID, {
+ deep = true,
+ })
return
end
local funcXID = ('%s%s%s'):format(
@@ -643,7 +665,9 @@ local function compileCallReturn(noders, call, sourceID, returnIndex)
)
getNode(noders, sourceID).call = call
pushForward(noders, sourceID, funcXID)
- pushBackward(noders, funcXID, sourceID, 'deep')
+ pushBackward(noders, funcXID, sourceID, {
+ deep = true,
+ })
end
function m.compileDocValue(noders, tp, id, source)
@@ -772,7 +796,9 @@ function m.compileNode(noders, source)
or setmethod.type == 'setfield'
or setmethod.type == 'setindex') then
pushForward(noders, id, getID(setmethod.node))
- pushBackward(noders, getID(setmethod.node), id, 'deep')
+ pushBackward(noders, getID(setmethod.node), id, {
+ deep = true,
+ })
end
end
-- 分解 @type
@@ -948,7 +974,9 @@ function m.compileNode(noders, source)
)
pushForward(noders, fullID, getID(rtn))
for _, typeUnit in ipairs(rtn.types) do
- pushBackward(noders, getID(typeUnit), fullID, 'deep')
+ pushBackward(noders, getID(typeUnit), fullID, {
+ deep = true,
+ })
end
hasDocReturn[rtn.returnIndex] = true
end
@@ -1006,7 +1034,9 @@ function m.compileNode(noders, source)
)
for _, rtnObj in ipairs(rtnObjs) do
pushForward(noders, returnID, getID(rtnObj))
- pushBackward(noders, getID(rtnObj), returnID, 'deep')
+ pushBackward(noders, getID(rtnObj), returnID, {
+ deep = true,
+ })
end
end
end
@@ -1054,7 +1084,9 @@ function m.compileNode(noders, source)
local rtnObj = rtn[1]
if rtnObj then
pushForward(noders, 'mainreturn', getID(rtnObj))
- pushBackward(noders, getID(rtnObj), 'mainreturn', 'deep')
+ pushBackward(noders, getID(rtnObj), 'mainreturn', {
+ deep = true,
+ })
end
end
end
diff --git a/script/core/searcher.lua b/script/core/searcher.lua
index fa70ab35..1d9eff5c 100644
--- a/script/core/searcher.lua
+++ b/script/core/searcher.lua
@@ -430,45 +430,57 @@ function m.searchRefsByID(status, uri, expect, mode)
searchID(newID)
end
- local ftag = {}
- local btag = {}
+ local freject = {}
+ local breject = {}
- local function checkThenPushTag(ward, tag)
- if not tag or tag == 'deep' then
+ ---@param ward '"forward"'|'"backward"'
+ ---@param info node.info
+ local function checkThenPushReject(ward, info)
+ if not info or info.deep then
return true
end
- local checkTags
- local pushTags
+ local reject = info.reject
+ if not reject then
+ return true
+ end
+ local checkReject
+ local pushReject
if ward == 'forward' then
- checkTags = btag
- pushTags = ftag
+ checkReject = breject
+ pushReject = freject
else
- checkTags = ftag
- pushTags = btag
+ checkReject = freject
+ pushReject = breject
end
- if checkTags[tag] and checkTags[tag] > 0 then
+ if checkReject[reject] and checkReject[reject] > 0 then
return false
end
- pushTags[tag] = (pushTags[tag] or 0) + 1
+ pushReject[reject] = (pushReject[reject] or 0) + 1
return true
end
- local function popTag(ward, tag)
- if not tag or tag == 'deep' then
+ ---@param ward '"forward"'|'"backward"'
+ ---@param info node.info
+ local function popReject(ward, info)
+ if not info or info.deep then
+ return
+ end
+ local reject = info.reject
+ if not reject then
return
end
local popTags
if ward == 'forward' then
- popTags = ftag
+ popTags = freject
else
- popTags = btag
+ popTags = breject
end
- popTags[tag] = popTags[tag] - 1
+ popTags[reject] = popTags[reject] - 1
end
local function checkForward(id, node, field)
- for forwardID, tag in noder.eachForward(node) do
- if not checkThenPushTag('forward', tag) then
+ for forwardID, info in noder.eachForward(node) do
+ if info and not checkThenPushReject('forward', info) then
goto CONTINUE
end
local targetUri, targetID = noder.getUriAndID(forwardID)
@@ -477,7 +489,7 @@ function m.searchRefsByID(status, uri, expect, mode)
else
searchID(targetID or forwardID, field)
end
- popTag('forward', tag)
+ popReject('forward', info)
::CONTINUE::
end
end
@@ -489,11 +501,11 @@ function m.searchRefsByID(status, uri, expect, mode)
if mode ~= 'ref' and mode ~= 'field' and mode ~= 'allref' and not field then
return
end
- for backwardID, tag in noder.eachBackward(node) do
- if tag == 'deep' and mode ~= 'allref' and mode ~= 'alldef' then
+ for backwardID, info in noder.eachBackward(node) do
+ if info and info.deep and mode ~= 'allref' and mode ~= 'alldef' then
goto CONTINUE
end
- if not checkThenPushTag('backward', tag) then
+ if info and not checkThenPushReject('backward', info) then
goto CONTINUE
end
local targetUri, targetID = noder.getUriAndID(backwardID)
@@ -502,7 +514,7 @@ function m.searchRefsByID(status, uri, expect, mode)
else
searchID(targetID or backwardID, field)
end
- popTag('backward', tag)
+ popReject('backward', info)
::CONTINUE::
end
end