summaryrefslogtreecommitdiff
path: root/script-beta/vm/getGlobals.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-11-19 16:50:08 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-11-19 16:50:08 +0800
commit719c434de917b7ee77173a2a265ed31581f61bb9 (patch)
treed81fa38169cd3544302507e1894d65992e4d996f /script-beta/vm/getGlobals.lua
parentb841b2668dd7d36cbf42c3f261e69e2a70358c6b (diff)
downloadlua-language-server-719c434de917b7ee77173a2a265ed31581f61bb9.zip
优化性能
Diffstat (limited to 'script-beta/vm/getGlobals.lua')
-rw-r--r--script-beta/vm/getGlobals.lua132
1 files changed, 112 insertions, 20 deletions
diff --git a/script-beta/vm/getGlobals.lua b/script-beta/vm/getGlobals.lua
index e5165130..ccc86f7b 100644
--- a/script-beta/vm/getGlobals.lua
+++ b/script-beta/vm/getGlobals.lua
@@ -6,7 +6,12 @@ local util = require 'utility'
local config = require 'config'
local function getGlobalsOfFile(uri)
+ local cache = files.getCache(uri)
+ if cache.globals then
+ return cache.globals
+ end
local globals = {}
+ cache.globals = globals
local ast = files.getAst(uri)
if not ast then
return globals
@@ -30,6 +35,38 @@ local function getGlobalsOfFile(uri)
return globals
end
+local function getGlobalSetsOfFile(uri)
+ local cache = files.getCache(uri)
+ if cache.globalSets then
+ return cache.globalSets
+ end
+ local globals = {}
+ cache.globalSets = globals
+ local ast = files.getAst(uri)
+ if not ast then
+ return globals
+ end
+ local results = guide.findGlobals(ast.ast)
+ local mark = {}
+ for _, res in ipairs(results) do
+ if mark[res] then
+ goto CONTINUE
+ end
+ mark[res] = true
+ if vm.isSet(res) then
+ local name = guide.getSimpleName(res)
+ if name then
+ if not globals[name] then
+ globals[name] = {}
+ end
+ globals[name][#globals[name]+1] = res
+ end
+ end
+ ::CONTINUE::
+ end
+ return globals
+end
+
local function insertLibrary(results, name)
if name:sub(1, 2) == 's|' then
local libname = name:sub(3)
@@ -46,17 +83,16 @@ 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)
+ local globals = getGlobalsOfFile(uri)
if name == '*' then
- for _, sources in util.sortPairs(cache.globals) do
+ for _, sources in util.sortPairs(globals) do
for _, source in ipairs(sources) do
results[#results+1] = source
end
end
else
- if cache.globals[name] then
- for _, source in ipairs(cache.globals[name]) do
+ if globals[name] then
+ for _, source in ipairs(globals[name]) do
results[#results+1] = source
end
end
@@ -66,7 +102,29 @@ local function getGlobals(name)
return results
end
-local function getAnyGlobalsFast()
+local function getGlobalSets(name)
+ local results = {}
+ for uri in files.eachFile() do
+ local globals = getGlobalSetsOfFile(uri)
+ if name == '*' then
+ for _, sources in util.sortPairs(globals) do
+ for _, source in ipairs(sources) do
+ results[#results+1] = source
+ end
+ end
+ else
+ if globals[name] then
+ for _, source in ipairs(globals[name]) do
+ results[#results+1] = source
+ end
+ end
+ end
+ end
+ insertLibrary(results, name)
+ return results
+end
+
+local function fastGetAnyGlobals()
local results = {}
local mark = {}
for uri in files.eachFile() do
@@ -83,31 +141,65 @@ local function getAnyGlobalsFast()
return results
end
+local function fastGetAnyGlobalSets()
+ local results = {}
+ local mark = {}
+ for uri in files.eachFile() do
+ local cache = files.getCache(uri)
+ cache.globalSets = cache.globalSets or getGlobalSetsOfFile(uri)
+ for destName, sources in util.sortPairs(cache.globalSets) do
+ if not mark[destName] then
+ mark[destName] = true
+ results[#results+1] = sources[1]
+ end
+ end
+ end
+ insertLibrary(results, '*')
+ return results
+end
+
function vm.getGlobals(key)
if key == '*' and config.config.intelliSense.fastGlobal then
- return getAnyGlobalsFast()
- end
- local cache = vm.getCache('getGlobals')[key]
- if cache ~= nil then
+ local cache = vm.getCache('fastGetAnyGlobals')[key]
+ if cache ~= nil then
+ return cache
+ end
+ cache = fastGetAnyGlobals()
+ vm.getCache('fastGetAnyGlobals')[key] = cache
+ return cache
+ else
+ local cache = vm.getCache('getGlobals')[key]
+ if cache ~= nil then
+ return cache
+ end
+ cache = getGlobals(key)
+ vm.getCache('getGlobals')[key] = cache
return cache
end
- cache = getGlobals(key)
- vm.getCache('getGlobals')[key] = cache
- return cache
end
function vm.getGlobalSets(key)
+ if key == '*' and config.config.intelliSense.fastGlobal then
+ local cache = vm.getCache('fastGetAnyGlobalSets')[key]
+ if cache ~= nil then
+ return cache
+ end
+ cache = fastGetAnyGlobalSets()
+ vm.getCache('fastGetAnyGlobalSets')[key] = cache
+ return cache
+ end
local cache = vm.getCache('getGlobalSets')[key]
if cache ~= nil then
return cache
end
- cache = {}
- local refs = getGlobals(key)
- for _, source in ipairs(refs) do
- if vm.isSet(source) then
- cache[#cache+1] = source
- end
- end
+ cache = getGlobalSets(key)
vm.getCache('getGlobalSets')[key] = cache
return cache
end
+
+files.watch(function (ev, uri)
+ if ev == 'update' then
+ getGlobalsOfFile(uri)
+ getGlobalSetsOfFile(uri)
+ end
+end)