diff options
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/linker.lua | 164 | ||||
-rw-r--r-- | script/core/searcher.lua | 44 |
2 files changed, 92 insertions, 116 deletions
diff --git a/script/core/linker.lua b/script/core/linker.lua index c70fc1fa..5255b20f 100644 --- a/script/core/linker.lua +++ b/script/core/linker.lua @@ -114,10 +114,14 @@ local function checkMode(source) or source.type == 'doc.extends.name' then return 'dn' end - if source.type == 'doc.class' - or source.type == 'doc.type' - or source.type == 'doc.alias' then - return 'ds' + if source.type == 'doc.class' then + return 'dc' + end + if source.type == 'doc.type' then + return 'dt' + end + if source.type == 'doc.alias' then + return 'da' end if isGlobal(source) then return 'g' @@ -125,6 +129,66 @@ local function checkMode(source) return 'l' end +local IDList = {} +---获取语法树单元的字符串ID +---@param source parser.guide.object +---@return string? id +local function getID(source) + if not source then + return nil + end + if source._id ~= nil then + return source._id or nil + end + if source.type == 'field' + or source.type == 'method' then + source._id = false + return nil + end + local current = source + local index = 0 + while true do + local id, node = getKey(current) + if not id then + break + end + index = index + 1 + IDList[index] = id + if not node then + break + end + if node.special == '_G' then + break + end + current = node + end + if index == 0 then + source._id = false + return nil + end + for i = index + 1, #IDList do + IDList[i] = nil + end + local mode = checkMode(current) + if mode then + IDList[#IDList+1] = mode + end + util.revertTable(IDList) + local id = table.concat(IDList, '|') + if index > 1 then + local lastID = table.concat(IDList, '|', 1, index) + pushLastID(id, lastID) + end + do + local lastID = id:gsub(':%d+$', '') + if id ~= lastID then + pushLastID(id, lastID) + end + end + source._id = id + return id +end + local TempList = {} ---前进 @@ -135,7 +199,7 @@ local function checkForward(source, id) local parent = source.parent if source.value then -- x = y : x -> y - list[#list+1] = source.value + list[#list+1] = getID(source.value) end -- mt:f -> self if parent.type == 'setmethod' @@ -144,7 +208,7 @@ local function checkForward(source, id) if func then local self = func.locals[1] if self.tag == 'self' then - list[#list+1] = self + list[#list+1] = getID(self) end end end @@ -154,20 +218,20 @@ local function checkForward(source, id) for _, doc in ipairs(bindDocs) do if doc.type == 'doc.class' or doc.type == 'doc.type' then - list[#list+1] = doc + list[#list+1] = getID(doc) end end end -- 分解 @type if source.type == 'doc.type' then for _, typeUnit in ipairs(source.types) do - list[#list+1] = typeUnit + list[#list+1] = getID(typeUnit) end end -- 分解 @class if source.type == 'doc.class' then - list[#list+1] = source.class - list[#list+1] = source.extends + list[#list+1] = getID(source.class) + list[#list+1] = getID(source.extends) end if #list == 0 then return nil @@ -184,27 +248,35 @@ local function checkBackward(source, id) local list = TempList local parent = source.parent if parent.value == source then - list[#list+1] = parent + list[#list+1] = getID(parent) end -- self -> mt:xx if source.tag == 'self' then local func = guide.getParentFunction(source) local setmethod = func.parent if setmethod and setmethod.type == 'setmethod' then - list[#list+1] = setmethod.node + list[#list+1] = getID(setmethod.node) end end -- name 映射回 class 与 type if source.type == 'doc.class.name' or source.type == 'doc.type.name' then - list[#list+1] = parent + list[#list+1] = getID(parent) end -- class 与 type 绑定的 source if source.type == 'doc.class' or source.type == 'doc.type' then if source.bindSources then for _, src in ipairs(source.bindSources) do - list[#list+1] = src + list[#list+1] = getID(src) + end + end + -- 将 @return 映射到函数返回值上 + if source.returnIndex then + for _, src in ipairs(parent.bindSources) do + if src.type == 'function' then + list[#list+1] = ('%s:%s'):format(getID(src), source.returnIndex) + end end end end @@ -223,59 +295,6 @@ local function checkBackward(source, id) end end -local IDList = {} ----获取语法树单元的字符串ID ----@param source parser.guide.object ----@return string? id ----@return parser.guide.object? -local function getID(source) - if source.type == 'field' - or source.type == 'method' then - return nil, nil - end - local current = source - local index = 0 - while true do - local id, node = getKey(current) - if not id then - break - end - index = index + 1 - IDList[index] = id - source = current - if not node then - break - end - if node.special == '_G' then - break - end - current = node - end - if index == 0 then - return nil - end - for i = index + 1, #IDList do - IDList[i] = nil - end - local mode = checkMode(current) - if mode then - IDList[#IDList+1] = mode - end - util.revertTable(IDList) - local id = table.concat(IDList, '|') - if index > 1 then - local lastID = table.concat(IDList, '|', 1, index) - pushLastID(id, lastID) - end - do - local lastID = id:gsub(':%d+$', '') - if id ~= lastID then - pushLastID(id, lastID) - end - end - return id -end - ---@class link -- 当前节点的id ---@field id string @@ -285,8 +304,6 @@ end ---@field forward parser.guide.object[] -- 后退的关联单元 ---@field backward parser.guide.object[] --- 缓存的特殊数据 ----@field special table ---创建source的链接信息 ---@param source parser.guide.object @@ -356,14 +373,7 @@ end ---@param source parser.guide.object ---@return string function m.getID(source) - if not source then - return nil - end - local link = m.getLink(source) - if not link then - return nil - end - return link.id + return getID(source) end ---获取source的special diff --git a/script/core/searcher.lua b/script/core/searcher.lua index 649b695d..c2cf253a 100644 --- a/script/core/searcher.lua +++ b/script/core/searcher.lua @@ -117,17 +117,7 @@ function m.searchRefsByID(status, uri, expect, mode) end end - local function searchSource(idOrObj, field) - local id - if type(idOrObj) == 'string' then - id = idOrObj - else - local link = linker.getLink(idOrObj) - if not link then - return - end - id = link.id - end + local function searchID(id, field) search(id, field) if field then id = id .. field @@ -183,8 +173,8 @@ function m.searchRefsByID(status, uri, expect, mode) if not link.forward then return end - for _, forwardSources in ipairs(link.forward) do - searchSource(forwardSources, field) + for _, id in ipairs(link.forward) do + searchID(id, field) end end @@ -192,31 +182,8 @@ function m.searchRefsByID(status, uri, expect, mode) if not link.backward then return end - for _, backSources in ipairs(link.backward) do - searchSource(backSources, field) - end - end - - local function checkSpecial(link, field) - local special = link.special - if not special then - return - end - if special.call then - local newStatus = m.status(status) - m.searchRefs(newStatus, special.call.node, 'def') - for _, res in ipairs(newStatus.results) do - local returns = linker.getSpecial(res, 'returns') - if returns and returns[special.index] then - for _, rtn in ipairs(returns[special.index]) do - searchSource(rtn, field) - end - end - end - end - if special.returns then - local newStatus = m.status(status) - --m.searchRefs(newStatus, special.call.node, 'def') + for _, id in ipairs(link.backward) do + searchID(id, field) end end @@ -242,7 +209,6 @@ function m.searchRefsByID(status, uri, expect, mode) end checkForward(eachLink, field) checkBackward(eachLink, field) - --checkSpecial(eachLink, field) end end checkLastID(id, field) |