summaryrefslogtreecommitdiff
path: root/server-beta/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-11 20:46:18 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-11 20:46:18 +0800
commit025b80013795760483e75fc2487e439a0f62cfeb (patch)
tree0634c74606de42f6bdb03535678f8173205410ed /server-beta/src
parentb6ca9b20db4a7b28a4e0adb7f8b24da4356e45a7 (diff)
downloadlua-language-server-025b80013795760483e75fc2487e439a0f62cfeb.zip
link管理
Diffstat (limited to 'server-beta/src')
-rw-r--r--server-beta/src/files.lua47
-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
6 files changed, 84 insertions, 64 deletions
diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua
index 0f3d721f..a3cf3e8e 100644
--- a/server-beta/src/files.lua
+++ b/server-beta/src/files.lua
@@ -73,6 +73,7 @@ function m.setText(uri, text)
file.lines = nil
file.ast = nil
file.globals = nil
+ file.links = nil
m.globalVersion = m.globalVersion + 1
searcher.refreshCache()
@@ -194,30 +195,6 @@ function m.getOriginUri(uri)
return file.uri
end
---- 获取全局变量
-function m.getGlobals(uri)
- if platform.OS == 'Windows' then
- uri = uri:lower()
- end
- local file = m.fileMap[uri]
- if not file then
- return nil
- end
- if file.globals then
- return file.globals
- end
- local ast = m.getAst(uri)
- if not ast then
- return nil
- end
- file.globals = {}
- local globals = searcher.getGlobals(ast.ast)
- for name in pairs(globals) do
- file.globals[name] = true
- end
- return file.globals
-end
-
--- 寻找全局变量
function m.findGlobals(name)
local uris = {}
@@ -239,6 +216,28 @@ function m.findGlobals(name)
return uris
end
+--- 寻找link自己的其他文件
+function m.findLinkTo(uri)
+ if platform.OS == 'Windows' then
+ uri = uri:lower()
+ end
+ local result = {}
+ for _, file in pairs(m.fileMap) do
+ if file.links == nil then
+ local ast = m.getAst(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
+ end
+ end
+ return result
+end
+
--- 判断文件名相等
function m.eq(a, b)
if platform.OS == 'Windows' then
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