summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-05-08 19:04:29 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-05-08 19:04:29 +0800
commitacca4a929950cca9a92541733abc4662dc8a3e55 (patch)
treeb3caf5688c1f2f25931481829c65a4cd40e58350 /script
parentca6a5b1ab080b9bebb850da56dcc37b6d17db144 (diff)
downloadlua-language-server-acca4a929950cca9a92541733abc4662dc8a3e55.zip
stash
Diffstat (limited to 'script')
-rw-r--r--script/core/linker.lua26
-rw-r--r--script/core/searcher.lua54
2 files changed, 58 insertions, 22 deletions
diff --git a/script/core/linker.lua b/script/core/linker.lua
index f8c102d1..9644f36e 100644
--- a/script/core/linker.lua
+++ b/script/core/linker.lua
@@ -3,8 +3,10 @@ local guide = require 'parser.guide'
local Linkers
local LastIDCache = {}
+local FirstIDCache = {}
local SPLIT_CHAR = '\x1F'
-local SPLIT_REGEX = SPLIT_CHAR .. '[^' .. SPLIT_CHAR .. ']*$'
+local LAST_REGEX = SPLIT_CHAR .. '[^' .. SPLIT_CHAR .. ']*$'
+local FIRST_REGEX = '^[^' .. SPLIT_CHAR .. ']*'
local RETURN_INDEX_CHAR = '#'
local PARAM_INDEX_CHAR = '@'
@@ -447,6 +449,7 @@ local function compileLink(source)
)
pushForward(id, callID)
pushBackward(callID, id)
+ getLink(id).call = source.vararg
end
end
if source.type == 'doc.type.function' then
@@ -542,6 +545,9 @@ local function compileLink(source)
end
end
end
+ if doc.type == 'doc.generic' then
+ source._isGeneric = true
+ end
end
end
end
@@ -578,6 +584,22 @@ function m.getLinkByID(root, id)
return linkers[id]
end
+---根据ID来获取第一个节点的ID
+---@param id string
+---@return string
+function m.getFirstID(id)
+ if FirstIDCache[id] then
+ return FirstIDCache[id] or nil
+ end
+ local firstID, count = id:match(FIRST_REGEX)
+ if count == 0 then
+ FirstIDCache[id] = false
+ return nil
+ end
+ FirstIDCache[id] = firstID
+ return firstID
+end
+
---根据ID来获取上个节点的ID
---@param id string
---@return string
@@ -585,7 +607,7 @@ function m.getLastID(id)
if LastIDCache[id] then
return LastIDCache[id] or nil
end
- local lastID, count = id:gsub(SPLIT_REGEX, '')
+ local lastID, count = id:gsub(LAST_REGEX, '')
if count == 0 then
LastIDCache[id] = false
return nil
diff --git a/script/core/searcher.lua b/script/core/searcher.lua
index aea13ed5..db581e9c 100644
--- a/script/core/searcher.lua
+++ b/script/core/searcher.lua
@@ -2,8 +2,6 @@ local linker = require 'core.linker'
local guide = require 'parser.guide'
local files = require 'files'
-local MARK_CHAR = '\x1E'
-
local function checkFunctionReturn(source)
if source.parent
and source.parent.type == 'return' then
@@ -179,6 +177,7 @@ function m.searchRefsByID(status, uri, expect, mode)
-- 缓存过程中的泛型,以泛型关联表为key
local genericStashMap = {}
+ local idStack = {}
local function search(id, field)
local fieldLen
@@ -241,12 +240,30 @@ function m.searchRefsByID(status, uri, expect, mode)
search(parentID, linker.SPLIT_CHAR .. linker.RETURN_INDEX_CHAR .. returnIndex)
end
- local callStash = {}
+ local function findCallParam(key, index)
+ for i = #idStack, 1, -1 do
+ local id = idStack[i]
+ local link = linker.getLinkByID(root, id)
+ if not link then
+ goto CONTINUE
+ end
+ local call = link.call
+ if not call then
+ goto CONTINUE
+ end
+ local args = call.args
+ if not args then
+ goto CONTINUE
+ end
+ do return args[index] end
+ ::CONTINUE::
+ end
+ end
- local function getGenericID(typeUnit, call, index)
+ local function getGenericID(typeUnit, index)
local key = typeUnit[1]
local generics = typeUnit.typeGeneric[key]
- local callParam = call.args[index]
+ local callParam = findCallParam(key, index)
if not callParam then
return nil
end
@@ -260,10 +277,13 @@ function m.searchRefsByID(status, uri, expect, mode)
return nil
end
- local function genericStashParam(docType, call, index)
+ local function genericStashParam(docType, index)
+ if #idStack == 0 then
+ return
+ end
for _, typeUnit in ipairs(docType.types) do
if typeUnit.typeGeneric then
- local generics, id = getGenericID(typeUnit, call, index)
+ local generics, id = getGenericID(typeUnit, index)
if id then
genericStashMap[generics] = id
end
@@ -271,7 +291,7 @@ function m.searchRefsByID(status, uri, expect, mode)
-- 支持 V[]
if typeUnit.type == 'doc.type.array' then
if typeUnit.node.typeGeneric then
- local generics, id = getGenericID(typeUnit.node, call, index)
+ local generics, id = getGenericID(typeUnit.node, index)
if id then
genericStashMap[generics] = id .. linker.SPLIT_CHAR
end
@@ -282,7 +302,7 @@ function m.searchRefsByID(status, uri, expect, mode)
if typeUnit.value then
for _, typeUnit2 in ipairs(typeUnit.value.types) do
if typeUnit2.typeGeneric then
- local generics, id = getGenericID(typeUnit2, call, index)
+ local generics, id = getGenericID(typeUnit2, index)
if id then
genericStashMap[generics] = id .. linker.SPLIT_CHAR
end
@@ -299,15 +319,6 @@ function m.searchRefsByID(status, uri, expect, mode)
and source.type ~= 'doc.type.function' then
return
end
- local top = #callStash
- if top == 0 then
- return
- end
- local call = callStash[top]
- callStash[top] = nil
- if not call.args then
- return
- end
if source.type == 'function' then
if not source.docParamMap then
return
@@ -315,13 +326,13 @@ function m.searchRefsByID(status, uri, expect, mode)
for index, param in ipairs(source.args) do
local docParam = param.docParam
if docParam then
- genericStashParam(docParam.extends, call, index)
+ genericStashParam(docParam.extends, index)
end
end
end
if source.type == 'doc.type.function' then
for index, param in ipairs(source.args) do
- genericStashParam(param.extends, call, index)
+ genericStashParam(param.extends, index)
end
end
end
@@ -346,6 +357,7 @@ function m.searchRefsByID(status, uri, expect, mode)
end
local link = linker.getLinkByID(root, id)
if link then
+ idStack[#idStack+1] = id
if field == nil and link.sources then
for _, source in ipairs(link.sources) do
m.pushResult(status, mode, source)
@@ -366,6 +378,8 @@ function m.searchRefsByID(status, uri, expect, mode)
genericStash(link.sources[1])
genericResolve(link.sources[1], field)
end
+
+ idStack[#idStack] = nil
end
checkLastID(id, field)
end