summaryrefslogtreecommitdiff
path: root/server-beta/src/searcher
diff options
context:
space:
mode:
Diffstat (limited to 'server-beta/src/searcher')
-rw-r--r--server-beta/src/searcher/eachRef.lua20
-rw-r--r--server-beta/src/searcher/getGlobals.lua7
-rw-r--r--server-beta/src/searcher/getLinks.lua32
-rw-r--r--server-beta/src/searcher/init.lua1
-rw-r--r--server-beta/src/searcher/searcher.lua41
5 files changed, 61 insertions, 40 deletions
diff --git a/server-beta/src/searcher/eachRef.lua b/server-beta/src/searcher/eachRef.lua
index e5877ae7..21e9b401 100644
--- a/server-beta/src/searcher/eachRef.lua
+++ b/server-beta/src/searcher/eachRef.lua
@@ -73,18 +73,14 @@ local function ofSpecialCall(call, func, index, callback)
end
elseif name == 'require' then
if index == 1 then
- local args = call.args
- if args[1] then
- local literal = guide.getLiteral(args[1])
- if type(literal) == 'string' then
- local result = workspace.findUrisByRequirePath(literal, true)
- local myUri = guide.getRoot(call).uri
- for _, uri in ipairs(result) do
- if not files.eq(uri, myUri) then
- local ast = files.getAst(uri)
- if ast then
- searcher.eachRef(ast.ast, callback)
- end
+ local result = searcher.getLinkUris(call)
+ if result then
+ local myUri = guide.getRoot(call).uri
+ for _, uri in ipairs(result) do
+ if not files.eq(uri, myUri) then
+ local ast = files.getAst(uri)
+ if ast then
+ searcher.eachRef(ast.ast, callback)
end
end
end
diff --git a/server-beta/src/searcher/getGlobals.lua b/server-beta/src/searcher/getGlobals.lua
index 4519985b..c5737881 100644
--- a/server-beta/src/searcher/getGlobals.lua
+++ b/server-beta/src/searcher/getGlobals.lua
@@ -1,8 +1,7 @@
local guide = require 'parser.guide'
local searcher = require 'searcher.searcher'
-local function getGlobals(source)
- local root = guide.getRoot(source)
+local function getGlobals(root)
local env = guide.getENV(root)
local cache = {}
local mark = {}
@@ -32,7 +31,7 @@ end
function searcher.getGlobals(source)
source = guide.getRoot(source)
local cache = searcher.cache.getGlobals[source]
- if cache then
+ if cache ~= nil then
return cache
end
local unlock = searcher.lock('getGlobals', source)
@@ -40,7 +39,7 @@ function searcher.getGlobals(source)
return nil
end
cache = getGlobals(source)
- searcher.cache.getGlobals[source] = cache
+ searcher.cache.getGlobals[source] = cache or false
unlock()
return cache
end
diff --git a/server-beta/src/searcher/getLinks.lua b/server-beta/src/searcher/getLinks.lua
new file mode 100644
index 00000000..32b29be1
--- /dev/null
+++ b/server-beta/src/searcher/getLinks.lua
@@ -0,0 +1,32 @@
+local guide = require 'parser.guide'
+local searcher = require 'searcher.searcher'
+
+local function getLinks(root)
+ local cache = {}
+ guide.eachSourceType(root, 'call', function (info)
+ local uris = searcher.getLinkUris(info.source)
+ if uris then
+ for i = 1, #uris do
+ local uri = uris[i]
+ cache[uri] = true
+ end
+ end
+ end)
+ return cache
+end
+
+function searcher.getLinks(source)
+ source = guide.getRoot(source)
+ local cache = searcher.cache.getLinks[source]
+ if cache ~= nil then
+ return cache
+ end
+ local unlock = searcher.lock('getLinks', source)
+ if not unlock then
+ return nil
+ end
+ cache = getLinks(source)
+ searcher.cache.getLinks[source] = cache or false
+ unlock()
+ return cache
+end
diff --git a/server-beta/src/searcher/init.lua b/server-beta/src/searcher/init.lua
index cd59fa4a..a43c8d96 100644
--- a/server-beta/src/searcher/init.lua
+++ b/server-beta/src/searcher/init.lua
@@ -2,6 +2,7 @@ local searcher = require 'searcher.searcher'
require 'searcher.eachField'
require 'searcher.eachRef'
require 'searcher.getGlobals'
+require 'searcher.getLinks'
require 'searcher.isGlobal'
require 'searcher.getLibrary'
return searcher
diff --git a/server-beta/src/searcher/searcher.lua b/server-beta/src/searcher/searcher.lua
index c7355f93..a17db8df 100644
--- a/server-beta/src/searcher/searcher.lua
+++ b/server-beta/src/searcher/searcher.lua
@@ -3,6 +3,8 @@ local util = require 'utility'
local setmetatable = setmetatable
local assert = assert
+local require = require
+local type = type
_ENV = nil
@@ -73,32 +75,21 @@ function m.getSpecialName(source)
return spName
end
---- 遍历特殊对象
----@param callback fun(name:string, source:table)
-function m.eachSpecial(callback)
- local cache = m.cache.specials
- if cache then
- for i = 1, #cache do
- callback(cache[i][1], cache[i][2])
+--- 获取link的uri
+function m.getLinkUris(call)
+ local workspace = require 'workspace'
+ local func = call.node
+ local name = m.getSpecialName(func)
+ if name == 'require' then
+ local args = call.args
+ if not args[1] then
+ return nil
end
- return
- end
- cache = {}
- m.cache.specials = cache
- guide.eachSource(m.ast, function (source)
- if source.type == 'getlocal'
- or source.type == 'getglobal'
- or source.type == 'local'
- or source.type == 'field'
- or source.type == 'string' then
- local name = m.getSpecialName(source)
- if name then
- cache[#cache+1] = { name, source }
- end
+ local literal = guide.getLiteral(args[1])
+ if type(literal) ~= 'string' then
+ return nil
end
- end)
- for i = 1, #cache do
- callback(cache[i][1], cache[i][2])
+ return workspace.findUrisByRequirePath(literal, true)
end
end
@@ -113,6 +104,7 @@ function m.refreshCache()
eachRef = {},
eachField = {},
getGlobals = {},
+ getLinks = {},
isGlobal = {},
specialName = {},
getLibrary = {},
@@ -122,6 +114,7 @@ function m.refreshCache()
eachRef = {},
eachField = {},
getGlobals = {},
+ getLinks = {},
getLibrary = {},
}
m.cacheTracker[m.cache] = true