summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-04-21 21:42:52 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-04-21 21:42:52 +0800
commit7a9530ffc1e9b79ba1fc54653435952b21e34d05 (patch)
tree9736cd60bf8512b60e230026a7ce02b92106e4dd
parent1dcd60d2dc139cf4c5a07e3cae440807c254d897 (diff)
downloadlua-language-server-7a9530ffc1e9b79ba1fc54653435952b21e34d05.zip
cleanup
-rw-r--r--script/core/linker.lua82
-rw-r--r--script/core/searcher.lua4
-rw-r--r--test/basic/linker.lua31
3 files changed, 56 insertions, 61 deletions
diff --git a/script/core/linker.lua b/script/core/linker.lua
index 5255b20f..bb81709d 100644
--- a/script/core/linker.lua
+++ b/script/core/linker.lua
@@ -3,10 +3,9 @@ local guide = require 'parser.guide'
local vm = require 'vm.vm'
local Linkers
-
-local function pushLastID(id, lastID)
- Linkers.lastIDMap[id] = lastID
-end
+local LastIDCache = {}
+local SPLIT_CHAR = '\x1F'
+local SPLIT_REGEX = SPLIT_CHAR .. '.-$'
---是否是全局变量(包括 _G.XXX 形式)
---@param source parser.guide.object
@@ -85,7 +84,7 @@ local function getKey(source)
elseif source.type == 'function' then
return source.start, nil
elseif source.type == 'select' then
- return ('%d:%d'):format(source.start, source.index)
+ return ('%d%s%d'):format(source.start, SPLIT_CHAR, source.index)
elseif source.type == 'doc.class.name'
or source.type == 'doc.type.name'
or source.type == 'doc.alias.name' then
@@ -100,33 +99,33 @@ end
local function checkMode(source)
if source.type == 'table' then
- return 't'
+ return 't:'
end
if source.type == 'select' then
- return 's'
+ return 's:'
end
if source.type == 'function' then
- return 'f'
+ return 'f:'
end
if source.type == 'doc.class.name'
or source.type == 'doc.type.name'
or source.type == 'doc.alias.name'
or source.type == 'doc.extends.name' then
- return 'dn'
+ return 'dn:'
end
if source.type == 'doc.class' then
- return 'dc'
+ return 'dc:'
end
if source.type == 'doc.type' then
- return 'dt'
+ return 'dt:'
end
if source.type == 'doc.alias' then
- return 'da'
+ return 'da:'
end
if isGlobal(source) then
- return 'g'
+ return 'g:'
end
- return 'l'
+ return 'l:'
end
local IDList = {}
@@ -170,21 +169,12 @@ local function getID(source)
IDList[i] = nil
end
local mode = checkMode(current)
- if mode then
- IDList[#IDList+1] = mode
+ if not mode then
+ source._id = false
+ return nil
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
+ local id = mode .. table.concat(IDList, SPLIT_CHAR)
source._id = id
return id
end
@@ -275,7 +265,8 @@ local function checkBackward(source, id)
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)
+ local fullID = ('%s%s%s'):format(getID(src), SPLIT_CHAR, source.returnIndex)
+ list[#list+1] = fullID
end
end
end
@@ -284,7 +275,7 @@ local function checkBackward(source, id)
if parent.type == 'call' and parent.node == source then
local sel = parent.parent
if sel.type == 'select' then
- list[#list+1] = ('s|%d'):format(sel.start)
+ list[#list+1] = ('s:%d'):format(sel.start)
end
end
if #list == 0 then
@@ -323,16 +314,17 @@ end
---@param link link
local function insertLinker(link)
- local idMap = Linkers.idMap
- local id = link.id
- if not idMap[id] then
- idMap[id] = {}
+ local id = link.id
+ if not Linkers[id] then
+ Linkers[id] = {}
end
- idMap[id][#idMap[id]+1] = link
+ Linkers[id][#Linkers[id]+1] = link
end
local m = {}
+m.SPLIT_CHAR = SPLIT_CHAR
+
---根据ID来获取所有的link
---@param root parser.guide.object
---@param id string
@@ -343,20 +335,23 @@ function m.getLinksByID(root, id)
if not linkers then
return nil
end
- return linkers.idMap[id]
+ return linkers[id]
end
---根据ID来获取上个节点的ID
----@param root parser.guide.object
---@param id string
---@return string
-function m.getLastID(root, id)
- root = guide.getRoot(root)
- local linkers = root._linkers
- if not linkers then
+function m.getLastID(id)
+ if LastIDCache[id] then
+ return LastIDCache[id] or nil
+ end
+ local lastID, count = id:gsub(SPLIT_REGEX, '')
+ if count == 0 then
+ LastIDCache[id] = false
return nil
end
- return linkers.lastIDMap[id]
+ LastIDCache[id] = lastID
+ return lastID
end
---获取source的链接信息
@@ -402,10 +397,7 @@ function m.compileLinks(source)
if root._linkers then
return root._linkers
end
- Linkers = {
- idMap = {},
- lastIDMap = {},
- }
+ Linkers = {}
root._linkers = Linkers
guide.eachSource(root, function (src)
local link = m.getLink(src)
diff --git a/script/core/searcher.lua b/script/core/searcher.lua
index c2cf253a..069def94 100644
--- a/script/core/searcher.lua
+++ b/script/core/searcher.lua
@@ -107,7 +107,7 @@ function m.searchRefsByID(status, uri, expect, mode)
local search
local function checkLastID(id, field)
- local lastID = linker.getLastID(root, id)
+ local lastID = linker.getLastID(id)
if lastID then
local newField = id:sub(#lastID + 1)
if field then
@@ -166,7 +166,7 @@ function m.searchRefsByID(status, uri, expect, mode)
if not parentID then
return
end
- search(parentID, ':' .. returnIndex)
+ search(parentID, linker.SPLIT_CHAR .. returnIndex)
end
local function checkForward(link, field)
diff --git a/test/basic/linker.lua b/test/basic/linker.lua
index 14894aaf..c4655191 100644
--- a/test/basic/linker.lua
+++ b/test/basic/linker.lua
@@ -39,6 +39,9 @@ local function TEST(script)
assert(source)
linker.compileLinks(source)
local result = linker.getLink(source)
+
+ expect['id'] = expect['id']:gsub('|', '\x1F')
+
for key in pairs(CARE) do
assert(result[key] == expect[key])
end
@@ -49,59 +52,59 @@ CARE['id'] = true
TEST [[
local <?x?>
]] {
- id = 'l|9',
+ id = 'l:9',
}
TEST [[
local x
print(<?x?>)
]] {
- id = 'l|7',
+ id = 'l:7',
}
TEST [[
local x
<?x?> = 1
]] {
- id = 'l|7',
+ id = 'l:7',
}
TEST [[
print(<?X?>)
]] {
- id = 'g|"X"',
+ id = 'g:"X"',
}
TEST [[
print(<?X?>)
]] {
- id = 'g|"X"',
+ id = 'g:"X"',
}
TEST [[
local x
print(x.y.<?z?>)
]] {
- id = 'l|7|"y"|"z"',
+ id = 'l:7|"y"|"z"',
}
TEST [[
local x
function x:<?f?>() end
]] {
- id = 'l|7|"f"',
+ id = 'l:7|"f"',
}
TEST [[
print(X.Y.<?Z?>)
]] {
- id = 'g|"X"|"Y"|"Z"',
+ id = 'g:"X"|"Y"|"Z"',
}
TEST [[
function x:<?f?>() end
]] {
- id = 'g|"x"|"f"',
+ id = 'g:"x"|"f"',
}
TEST [[
@@ -109,13 +112,13 @@ TEST [[
<?x?> = 1,
}
]] {
- id = 't|1|"x"',
+ id = 't:1|"x"',
}
TEST [[
return <?X?>
]] {
- id = 'g|"X"',
+ id = 'g:"X"',
}
TEST [[
@@ -123,7 +126,7 @@ function f()
return <?X?>
end
]] {
- id = 'g|"X"',
+ id = 'g:"X"',
freturn = 1,
}
@@ -131,12 +134,12 @@ TEST [[
::<?label?>::
goto label
]] {
- id = 'l|5',
+ id = 'l:5',
}
TEST [[
::label::
goto <?label?>
]] {
- id = 'l|3',
+ id = 'l:3',
}