summaryrefslogtreecommitdiff
path: root/server-beta/src
diff options
context:
space:
mode:
Diffstat (limited to 'server-beta/src')
-rw-r--r--server-beta/src/files.lua10
-rw-r--r--server-beta/src/parser/guide.lua2
-rw-r--r--server-beta/src/searcher/eachRef.lua76
-rw-r--r--server-beta/src/searcher/getGlobals.lua4
-rw-r--r--server-beta/src/searcher/getLinks.lua13
5 files changed, 67 insertions, 38 deletions
diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua
index a3cf3e8e..076c491b 100644
--- a/server-beta/src/files.lua
+++ b/server-beta/src/files.lua
@@ -224,15 +224,19 @@ function m.findLinkTo(uri)
local result = {}
for _, file in pairs(m.fileMap) do
if file.links == nil then
- local ast = m.getAst(uri)
+ local ast = m.getAst(file.uri)
if ast then
file.links = searcher.getLinks(ast.ast)
else
file.links = false
end
end
- if file.links and file.links[uri] then
- result[#result+1] = file.uri
+ if file.links then
+ for linkUri in pairs(file.links) do
+ if m.eq(uri, linkUri) then
+ result[#result+1] = file.uri
+ end
+ end
end
end
return result
diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua
index 1cc82143..9c0a6794 100644
--- a/server-beta/src/parser/guide.lua
+++ b/server-beta/src/parser/guide.lua
@@ -101,7 +101,7 @@ function m.getLiteral(obj)
return nil
end
---- 寻找所在函数
+--- 寻找父函数
function m.getParentFunction(obj)
for _ = 1, 1000 do
obj = obj.parent
diff --git a/server-beta/src/searcher/eachRef.lua b/server-beta/src/searcher/eachRef.lua
index 21e9b401..82ed4269 100644
--- a/server-beta/src/searcher/eachRef.lua
+++ b/server-beta/src/searcher/eachRef.lua
@@ -6,10 +6,16 @@ local searcher = require 'searcher.searcher'
local function ofCall(func, index, callback)
searcher.eachRef(func, function (info)
local src = info.source
- local funcDef = src.value
- if funcDef and funcDef.returns then
+ local returns
+ if info.mode == 'main' then
+ returns = src.returns
+ else
+ local funcDef = src.value
+ returns = funcDef and funcDef.returns
+ end
+ if returns then
-- 搜索函数第 index 个返回值
- for _, rtn in ipairs(funcDef.returns) do
+ for _, rtn in ipairs(returns) do
local val = rtn[index]
if val then
callback {
@@ -23,6 +29,23 @@ local function ofCall(func, index, callback)
end)
end
+local function ofCallSelect(call, index, callback)
+ local slc = call.parent
+ if slc.index == index then
+ searcher.eachRef(slc.parent, callback)
+ return
+ end
+ if call.extParent then
+ for i = 1, #call.extParent do
+ slc = call.extParent[i]
+ if slc.index == index then
+ searcher.eachRef(slc.parent, callback)
+ return
+ end
+ end
+ end
+end
+
local function ofReturn(rtn, index, callback)
local func = guide.getParentFunction(rtn)
if not func then
@@ -35,20 +58,7 @@ local function ofReturn(rtn, index, callback)
if not call or call.type ~= 'call' then
return
end
- local slc = call.parent
- if slc.index == index then
- searcher.eachRef(slc.parent, callback)
- return
- end
- if call.extParent then
- for i = 1, #call.extParent do
- slc = call.extParent[i]
- if slc.index == index then
- searcher.eachRef(slc.parent, callback)
- return
- end
- end
- end
+ ofCallSelect(call, index, callback)
end)
end
@@ -80,7 +90,7 @@ local function ofSpecialCall(call, func, index, callback)
if not files.eq(uri, myUri) then
local ast = files.getAst(uri)
if ast then
- searcher.eachRef(ast.ast, callback)
+ ofCall(ast.ast, 1, callback)
end
end
end
@@ -369,16 +379,28 @@ local function ofGoTo(source, callback)
end
local function ofMain(source, callback)
- if source.returns then
- for _, rtn in ipairs(source.returns) do
- local val = rtn[1]
- if val then
- callback {
- source = val,
- mode = 'return',
- }
- searcher.eachRef(val, callback)
+ callback {
+ source = source,
+ mode = 'main',
+ }
+ local myUri = source.uri
+ local uris = files.findLinkTo(myUri)
+ if not uris then
+ return
+ end
+ for _, uri in ipairs(uris) do
+ local ast = files.getAst(uri)
+ if ast then
+ local links = searcher.getLinks(ast.ast)
+ if links then
+ for linkUri, calls in pairs(links) do
+ if files.eq(linkUri, myUri) then
+ for i = 1, #calls do
+ ofCallSelect(calls[i], 1, callback)
+ end
+ end
end
+ end
end
end
end
diff --git a/server-beta/src/searcher/getGlobals.lua b/server-beta/src/searcher/getGlobals.lua
index c5737881..ea6a6f6c 100644
--- a/server-beta/src/searcher/getGlobals.lua
+++ b/server-beta/src/searcher/getGlobals.lua
@@ -38,8 +38,8 @@ function searcher.getGlobals(source)
if not unlock then
return nil
end
- cache = getGlobals(source)
- searcher.cache.getGlobals[source] = cache or false
+ cache = getGlobals(source) or false
+ searcher.cache.getGlobals[source] = cache
unlock()
return cache
end
diff --git a/server-beta/src/searcher/getLinks.lua b/server-beta/src/searcher/getLinks.lua
index 32b29be1..ba436f86 100644
--- a/server-beta/src/searcher/getLinks.lua
+++ b/server-beta/src/searcher/getLinks.lua
@@ -3,12 +3,15 @@ local searcher = require 'searcher.searcher'
local function getLinks(root)
local cache = {}
- guide.eachSourceType(root, 'call', function (info)
- local uris = searcher.getLinkUris(info.source)
+ guide.eachSourceType(root, 'call', function (source)
+ local uris = searcher.getLinkUris(source)
if uris then
for i = 1, #uris do
local uri = uris[i]
- cache[uri] = true
+ if not cache[uri] then
+ cache[uri] = {}
+ end
+ cache[uri][#cache[uri]+1] = source
end
end
end)
@@ -25,8 +28,8 @@ function searcher.getLinks(source)
if not unlock then
return nil
end
- cache = getLinks(source)
- searcher.cache.getLinks[source] = cache or false
+ cache = getLinks(source) or false
+ searcher.cache.getLinks[source] = cache
unlock()
return cache
end