summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-04-14 21:50:06 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-04-14 21:50:06 +0800
commitea2967268c4a6e1aae2b0688177451c91dee982a (patch)
tree50349eda44b25aa9e866a6e150a27ca2054d1235 /script/core
parent337a90407fd11ede8f184cdd6e223ee47c8e1193 (diff)
downloadlua-language-server-ea2967268c4a6e1aae2b0688177451c91dee982a.zip
stash
Diffstat (limited to 'script/core')
-rw-r--r--script/core/guide.lua64
-rw-r--r--script/core/linker.lua68
2 files changed, 109 insertions, 23 deletions
diff --git a/script/core/guide.lua b/script/core/guide.lua
index fe6071bd..eba1301b 100644
--- a/script/core/guide.lua
+++ b/script/core/guide.lua
@@ -85,17 +85,67 @@ end
---搜索对象的引用
---@param status guide.status
----@param obj parser.guide.object
+---@param source parser.guide.object
---@param mode guide.searchmode
-function m.searchRefs(status, obj, mode)
- local root = guide.getRoot(obj)
+function m.searchRefs(status, source, mode)
+ if source.type == 'field'
+ or source.type == 'method' then
+ source = source.parent
+ end
+ local root = guide.getRoot(source)
linker.compileLinks(root)
- local links = linker.getLinkersBySource(obj)
- if links then
- for _, link in ipairs(links) do
- m.pushResult(status, mode, link.source)
+ if not linker.getLink(source) then
+ return
+ end
+
+ ---@type link[]
+ local queue = {}
+ ---@type string
+ local expects = {}
+ local index = 0
+
+ local function pushQueue(expect, link)
+ index = index + 1
+ queue[index] = link
+ expects[index] = expect
+ end
+
+ local function pushQueueWithID(obj)
+ local link = linker.getLink(obj)
+ if not link then
+ return
+ end
+ pushQueue(link.id, link)
+ end
+
+ pushQueueWithID(source)
+
+ for _ = 1, 1000 do
+ if index <= 0 then
+ break
+ end
+ local link = queue[index]
+ local expect = expects[index]
+ index = index - 1
+ local links = linker.getLinkersBySource(link.source)
+ if not links then
+ goto CONTINUE
+ end
+ for _, eachLink in ipairs(links) do
+ m.pushResult(status, mode, eachLink.source)
+ if eachLink.forward then
+ for _, forwardSources in ipairs(eachLink.forward) do
+ pushQueueWithID(forwardSources)
+ end
+ end
+ if eachLink.backward then
+ for _, backSources in ipairs(eachLink.backward) do
+ pushQueueWithID(backSources)
+ end
+ end
end
+ ::CONTINUE::
end
end
diff --git a/script/core/linker.lua b/script/core/linker.lua
index 14582cc0..a064deaa 100644
--- a/script/core/linker.lua
+++ b/script/core/linker.lua
@@ -14,9 +14,6 @@ local function getKey(source)
elseif source.type == 'setglobal'
or source.type == 'getglobal' then
return ('%q'):format(source[1] or ''), nil
- elseif source.type == 'field'
- or source.type == 'method' then
- return ('%q'):format(source[1] or ''), source.parent.node
elseif source.type == 'getfield'
or source.type == 'setfield' then
return ('%q'):format(source.field and source.field[1] or ''), source.node
@@ -82,6 +79,41 @@ local function checkFunctionReturn(source)
return nil
end
+local TempList = {}
+
+---前进
+---@param source parser.guide.object
+---@return parser.guide.object[]
+local function checkForward(source)
+ local list = TempList
+ if source.value then
+ list[#list+1] = source.value
+ end
+ if #list == 0 then
+ return nil
+ else
+ TempList = {}
+ return list
+ end
+end
+
+---后退
+---@param source parser.guide.object
+---@return parser.guide.object[]
+local function checkBackward(source)
+ local list = TempList
+ local parent = source.parent
+ if parent.value == source then
+ list[#list+1] = parent
+ end
+ if #list == 0 then
+ return nil
+ else
+ TempList = {}
+ return list
+ end
+end
+
local IDList = {}
---获取语法树单元的字符串ID
---@param source parser.guide.object
@@ -90,7 +122,7 @@ local IDList = {}
local function getID(source)
if source.type == 'field'
or source.type == 'method' then
- source = source.parent
+ return nil, nil
end
local current = source
local index = 0
@@ -130,6 +162,12 @@ end
---@field tfield boolean
-- 返回值,文件返回值总是0,函数返回值为第几个返回值
---@field freturn integer
+-- 前进的关联单元
+---@field forward parser.guide.object[]
+-- 后退的关联单元
+---@field backward parser.guide.object[]
+-- 缓存的关联links
+---@field _links link[]
---创建source的链接信息
---@param source parser.guide.object
@@ -140,16 +178,14 @@ local function createLink(source)
return nil
end
return {
- id = id,
- source = source,
- -- 局部变量
- loc = checkLocal(node),
- -- 全局变量
- global = checkGlobal(node),
- -- 字面量表中的字段
- tfield = checkTableField(node),
- -- 返回值,文件返回值总是0,函数返回值为第几个返回值
- freturn = checkFunctionReturn(node),
+ id = id,
+ source = source,
+ loc = checkLocal(node),
+ global = checkGlobal(node),
+ tfield = checkTableField(node),
+ freturn = checkFunctionReturn(node),
+ forward = checkForward(source),
+ backward = checkBackward(source),
}
end
@@ -160,7 +196,7 @@ local function insertLinker(linkers, tp, link)
list[id] = {}
end
list[id][#list[id]+1] = link
- link._linker = list[id]
+ link._links = list[id]
end
local m = {}
@@ -172,7 +208,7 @@ function m.getLinkersBySource(source)
if not source._link then
source._link = createLink(source)
end
- return source._link and source._link._linker
+ return source._link and source._link._links
end
---获取source的链接信息