summaryrefslogtreecommitdiff
path: root/script-beta/vm/getGlobals.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-07-23 15:11:03 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-07-23 15:11:03 +0800
commita27677a6d4beca0e898d691e36d94df498ffb1d1 (patch)
tree14e8d23d91fb7f4436b86534ecd0a4449a01fc73 /script-beta/vm/getGlobals.lua
parentd60ad2c5826162c480dba8f1bc810758bb090327 (diff)
downloadlua-language-server-a27677a6d4beca0e898d691e36d94df498ffb1d1.zip
getGlobals
Diffstat (limited to 'script-beta/vm/getGlobals.lua')
-rw-r--r--script-beta/vm/getGlobals.lua58
1 files changed, 31 insertions, 27 deletions
diff --git a/script-beta/vm/getGlobals.lua b/script-beta/vm/getGlobals.lua
index 0ff92e3a..ec575e8e 100644
--- a/script-beta/vm/getGlobals.lua
+++ b/script-beta/vm/getGlobals.lua
@@ -1,41 +1,45 @@
local guide = require 'parser.guide'
local vm = require 'vm.vm'
+local files = require 'files'
-local function getGlobals(root)
- local env = guide.getENV(root)
- if not env then
- return nil
+local function getGlobalsOfFile(uri)
+ local globals = {}
+ local ast = files.getAst(uri)
+ if not ast then
+ return globals
end
- local cache = {}
- local fields = guide.requestFields(env)
- for _, src in ipairs(fields) do
- local name = vm.getKeyName(src)
- if not name then
- return
+ local env = guide.getENV(ast.ast)
+ local results = guide.requestFields(env)
+ for _, res in ipairs(results) do
+ local name = guide.getName(res)
+ if not globals[name] then
+ globals[name] = {}
end
- if not cache[name] then
- cache[name] = {
- key = name,
- }
+ globals[name][#globals[name]+1] = res
+ end
+ return globals
+end
+
+local function getGlobals(name)
+ local results = {}
+ for uri in files:eachFile() do
+ local cache = files.getCache(uri)
+ cache.globals = cache.globals or getGlobalsOfFile(uri)
+ if cache.globals[name] then
+ for _, source in ipairs(cache.globals[name]) do
+ results[#results+1] = source
+ end
end
- cache[name][#cache[name]+1] = src
- vm.cache.getGlobal[src] = name
end
- return cache
+ return results
end
-function vm.getGlobals(source)
- source = guide.getRoot(source)
- local cache = vm.cache.getGlobals[source]
+function vm.getGlobals(name)
+ local cache = vm.cache.getGlobals[name]
if cache ~= nil then
return cache
end
- local unlock = vm.lock('getGlobals', source)
- if not unlock then
- return nil
- end
- cache = getGlobals(source) or false
- vm.cache.getGlobals[source] = cache
- unlock()
+ cache = getGlobals(name)
+ vm.cache.getGlobals[name] = cache
return cache
end