diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-04-21 21:11:54 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-04-21 21:11:54 +0800 |
commit | 1dcd60d2dc139cf4c5a07e3cae440807c254d897 (patch) | |
tree | 88b2be84b26076390f045b8ebb277f69e9747ea3 /script/core/linker.lua | |
parent | d4b7f8e774ffea6abc5050fd61c2d1b5a033437c (diff) | |
download | lua-language-server-1dcd60d2dc139cf4c5a07e3cae440807c254d897.zip |
cleanup
Diffstat (limited to 'script/core/linker.lua')
-rw-r--r-- | script/core/linker.lua | 164 |
1 files changed, 87 insertions, 77 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 |