diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-06-17 20:30:42 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-06-17 20:30:42 +0800 |
commit | 77c15299948450fee84b42a5af55328dabf032e4 (patch) | |
tree | fccc0f76c01a654c57ba34461a4389dbbadb55b3 | |
parent | f05a68d38a0915fae1303a86229f4f4c9fd96ecb (diff) | |
download | lua-language-server-77c15299948450fee84b42a5af55328dabf032e4.zip |
improve
-rw-r--r-- | script/core/completion.lua | 2 | ||||
-rw-r--r-- | script/vm/getGlobals.lua | 270 | ||||
-rw-r--r-- | script/vm/getGlobals2.lua | 262 |
3 files changed, 282 insertions, 252 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua index d261b302..5e98aa15 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -324,7 +324,7 @@ local function checkModule(ast, word, offset, results) local fileName = path:match '[^/\\]*$' local stemName = fileName:gsub('%..+', '') if not locals[stemName] - and #vm.getGlobalSets(stemName) == 0 + and not vm.hasGlobalSets(stemName) and not config.config.diagnostics.globals[stemName] and stemName:match '^[%a_][%w_]*$' and matchKey(word, stemName) then diff --git a/script/vm/getGlobals.lua b/script/vm/getGlobals.lua index e5bcafc0..51cfe1ac 100644 --- a/script/vm/getGlobals.lua +++ b/script/vm/getGlobals.lua @@ -1,262 +1,30 @@ -local guide = require 'parser.guide' -local await = require "await" -local searcher = require "core.searcher" ----@type vm -local vm = require 'vm.vm' -local files = require 'files' -local util = require 'utility' -local config = require 'config' -local ws = require 'workspace' +local collector = require 'core.collector' +local vm = require 'vm.vm' -local function getGlobalsOfFile(uri) - if not files.exists(uri) then - return {} - end - local cache = files.getCache(uri) - if cache.globals then - return cache.globals - end - local globals = {} - cache.globals = globals - tracy.ZoneBeginN 'getGlobalsOfFile' - local results = searcher.findGlobals(uri, 'ref') - local subscribe = ws.getCache 'globalSubscribe' - subscribe[uri] = {} - local mark = {} - if not globals['*'] then - globals['*'] = {} - end - for _, res in ipairs(results) do - if mark[res] then - goto CONTINUE - end - mark[res] = true - local name = guide.getKeyName(res) - if name then - if not globals[name] then - globals[name] = {} - subscribe[uri][#subscribe[uri]+1] = name - end - globals[name][#globals[name]+1] = res - globals['*'][#globals['*']+1] = res - end - ::CONTINUE:: - end - tracy.ZoneEnd() - return globals -end - -local function getGlobalSetsOfFile(uri) - if not files.exists(uri) then - return {} - end - local cache = files.getCache(uri) - if cache.globalSets then - return cache.globalSets - end - local globals = {} - cache.globalSets = globals - tracy.ZoneBeginN 'getGlobalSetsOfFile' - local results = searcher.findGlobals(uri, 'def') - local subscribe = ws.getCache 'globalSetsSubscribe' - subscribe[uri] = {} - local mark = {} - if not globals['*'] then - globals['*'] = {} - end - for _, res in ipairs(results) do - if mark[res] then - goto CONTINUE - end - mark[res] = true - local name = guide.getKeyName(res) - if name then - if not globals[name] then - globals[name] = {} - subscribe[uri][#subscribe[uri]+1] = name - end - globals[name][#globals[name]+1] = res - globals['*'][#globals['*']+1] = res - end - ::CONTINUE:: - end - tracy.ZoneEnd() - return globals +function vm.hasGlobalSets(name) + local id = ('def:g:%q'):format(name) + return collector.has(id) end -local function getGlobals(name) - tracy.ZoneBeginN 'getGlobals #2' - local results = {} - local n = 0 - local uris = files.getAllUris() - for i = 1, #uris do - local globals = getGlobalsOfFile(uris[i])[name] - if globals then - for j = 1, #globals do - n = n + 1 - results[n] = globals[j] - end - end +function vm.getGlobalSets(name) + local cache = vm.getCache 'getGlobalSets' + if cache[name] then + return cache[name] end - local dummyCache = vm.getCache 'globalDummy' - for key in pairs(config.config.diagnostics.globals) do - if name == '*' or name == key then - if not dummyCache[key] then - dummyCache[key] = { - type = 'dummy', - start = 0, - finish = 0, - [1] = key - } - end - n = n + 1 - results[n] = dummyCache[key] - end - end - tracy.ZoneEnd() - return results -end - -local function getGlobalSets(name) - tracy.ZoneBeginN 'getGlobalSets #2' local results = {} - local n = 0 - local uris = files.getAllUris() - for i = 1, #uris do - local globals = getGlobalSetsOfFile(uris[i])[name] - if globals then - for j = 1, #globals do - n = n + 1 - results[n] = globals[j] - end - end + cache[name] = results + local id + if name == '*' then + id = 'def:g:' + else + id = ('def:g:%q'):format(name) end - local dummyCache = vm.getCache 'globalDummy' - for key in pairs(config.config.diagnostics.globals) do - if name == '*' or name == key then - if not dummyCache[key] then - dummyCache[key] = { - type = 'dummy', - start = 0, - finish = 0, - [1] = key - } + for node in collector.each(id) do + if node.sources then + for _, source in ipairs(node.sources) do + results[#results+1] = source end - n = n + 1 - results[n] = dummyCache[key] end end - tracy.ZoneEnd() return results end - -local function fastGetAnyGlobals() - local results = {} - local mark = {} - for uri in files.eachFile() do - --local globalSets = getGlobalsOfFile(uri) - --for destName, sources in util.sortPairs(globalSets) do - -- if not mark[destName] then - -- mark[destName] = true - -- results[#results+1] = sources[1] - -- end - --end - local globals = getGlobalsOfFile(uri) - for destName, sources in util.sortPairs(globals) do - if not mark[destName] then - mark[destName] = true - results[#results+1] = sources[1] - end - end - end - return results -end - -local function fastGetAnyGlobalSets() - local results = {} - local mark = {} - for uri in files.eachFile() do - local globals = getGlobalSetsOfFile(uri) - for destName, sources in util.sortPairs(globals) do - if not mark[destName] then - mark[destName] = true - results[#results+1] = sources[1] - end - end - end - return results -end - -local function checkNeedUpdate() - local getGlobalCache = ws.getCache 'getGlobals' - local getGlobalSetsCache = ws.getCache 'getGlobalSets' - local needUpdateGlobals = ws.getCache 'needUpdateGlobals' - local uris = {} - for uri in pairs(needUpdateGlobals) do - uris[#uris+1] = uri - end - for _, uri in ipairs(uris) do - if needUpdateGlobals[uri] then - needUpdateGlobals[uri] = nil - if files.exists(uri) then - for name in pairs(getGlobalsOfFile(uri)) do - getGlobalCache[name] = nil - end - for name in pairs(getGlobalSetsOfFile(uri)) do - getGlobalSetsCache[name] = nil - end - end - end - end -end - -function vm.getGlobals(key) - checkNeedUpdate() - local cache = ws.getCache('getGlobals')[key] - if cache ~= nil then - return cache - end - cache = getGlobals(key) - ws.getCache('getGlobals')[key] = cache - return cache -end - -function vm.getGlobalSets(key) - checkNeedUpdate() - local cache = ws.getCache('getGlobalSets')[key] - if cache ~= nil then - return cache - end - tracy.ZoneBeginN('getGlobalSets') - cache = getGlobalSets(key) - ws.getCache('getGlobalSets')[key] = cache - tracy.ZoneEnd() - return cache -end - -files.watch(function (ev, uri) - if ev == 'update' then - local globalSubscribe = ws.getCache 'globalSubscribe' - local globalSetsSubscribe = ws.getCache 'globalSetsSubscribe' - local getGlobalCache = ws.getCache 'getGlobals' - local getGlobalSetsCache = ws.getCache 'getGlobalSets' - local needUpdateGlobals = ws.getCache 'needUpdateGlobals' - uri = files.asKey(uri) - if globalSubscribe[uri] then - for _, name in ipairs(globalSubscribe[uri]) do - getGlobalCache[name] = nil - getGlobalCache['*'] = nil - end - end - if globalSetsSubscribe[uri] then - for _, name in ipairs(globalSetsSubscribe[uri]) do - getGlobalSetsCache[name] = nil - getGlobalSetsCache['*'] = nil - end - end - needUpdateGlobals[uri] = true - elseif ev == 'create' then - --getGlobalsOfFile(uri) - --getGlobalSetsOfFile(uri) - end -end) diff --git a/script/vm/getGlobals2.lua b/script/vm/getGlobals2.lua new file mode 100644 index 00000000..e5bcafc0 --- /dev/null +++ b/script/vm/getGlobals2.lua @@ -0,0 +1,262 @@ +local guide = require 'parser.guide' +local await = require "await" +local searcher = require "core.searcher" +---@type vm +local vm = require 'vm.vm' +local files = require 'files' +local util = require 'utility' +local config = require 'config' +local ws = require 'workspace' + +local function getGlobalsOfFile(uri) + if not files.exists(uri) then + return {} + end + local cache = files.getCache(uri) + if cache.globals then + return cache.globals + end + local globals = {} + cache.globals = globals + tracy.ZoneBeginN 'getGlobalsOfFile' + local results = searcher.findGlobals(uri, 'ref') + local subscribe = ws.getCache 'globalSubscribe' + subscribe[uri] = {} + local mark = {} + if not globals['*'] then + globals['*'] = {} + end + for _, res in ipairs(results) do + if mark[res] then + goto CONTINUE + end + mark[res] = true + local name = guide.getKeyName(res) + if name then + if not globals[name] then + globals[name] = {} + subscribe[uri][#subscribe[uri]+1] = name + end + globals[name][#globals[name]+1] = res + globals['*'][#globals['*']+1] = res + end + ::CONTINUE:: + end + tracy.ZoneEnd() + return globals +end + +local function getGlobalSetsOfFile(uri) + if not files.exists(uri) then + return {} + end + local cache = files.getCache(uri) + if cache.globalSets then + return cache.globalSets + end + local globals = {} + cache.globalSets = globals + tracy.ZoneBeginN 'getGlobalSetsOfFile' + local results = searcher.findGlobals(uri, 'def') + local subscribe = ws.getCache 'globalSetsSubscribe' + subscribe[uri] = {} + local mark = {} + if not globals['*'] then + globals['*'] = {} + end + for _, res in ipairs(results) do + if mark[res] then + goto CONTINUE + end + mark[res] = true + local name = guide.getKeyName(res) + if name then + if not globals[name] then + globals[name] = {} + subscribe[uri][#subscribe[uri]+1] = name + end + globals[name][#globals[name]+1] = res + globals['*'][#globals['*']+1] = res + end + ::CONTINUE:: + end + tracy.ZoneEnd() + return globals +end + +local function getGlobals(name) + tracy.ZoneBeginN 'getGlobals #2' + local results = {} + local n = 0 + local uris = files.getAllUris() + for i = 1, #uris do + local globals = getGlobalsOfFile(uris[i])[name] + if globals then + for j = 1, #globals do + n = n + 1 + results[n] = globals[j] + end + end + end + local dummyCache = vm.getCache 'globalDummy' + for key in pairs(config.config.diagnostics.globals) do + if name == '*' or name == key then + if not dummyCache[key] then + dummyCache[key] = { + type = 'dummy', + start = 0, + finish = 0, + [1] = key + } + end + n = n + 1 + results[n] = dummyCache[key] + end + end + tracy.ZoneEnd() + return results +end + +local function getGlobalSets(name) + tracy.ZoneBeginN 'getGlobalSets #2' + local results = {} + local n = 0 + local uris = files.getAllUris() + for i = 1, #uris do + local globals = getGlobalSetsOfFile(uris[i])[name] + if globals then + for j = 1, #globals do + n = n + 1 + results[n] = globals[j] + end + end + end + local dummyCache = vm.getCache 'globalDummy' + for key in pairs(config.config.diagnostics.globals) do + if name == '*' or name == key then + if not dummyCache[key] then + dummyCache[key] = { + type = 'dummy', + start = 0, + finish = 0, + [1] = key + } + end + n = n + 1 + results[n] = dummyCache[key] + end + end + tracy.ZoneEnd() + return results +end + +local function fastGetAnyGlobals() + local results = {} + local mark = {} + for uri in files.eachFile() do + --local globalSets = getGlobalsOfFile(uri) + --for destName, sources in util.sortPairs(globalSets) do + -- if not mark[destName] then + -- mark[destName] = true + -- results[#results+1] = sources[1] + -- end + --end + local globals = getGlobalsOfFile(uri) + for destName, sources in util.sortPairs(globals) do + if not mark[destName] then + mark[destName] = true + results[#results+1] = sources[1] + end + end + end + return results +end + +local function fastGetAnyGlobalSets() + local results = {} + local mark = {} + for uri in files.eachFile() do + local globals = getGlobalSetsOfFile(uri) + for destName, sources in util.sortPairs(globals) do + if not mark[destName] then + mark[destName] = true + results[#results+1] = sources[1] + end + end + end + return results +end + +local function checkNeedUpdate() + local getGlobalCache = ws.getCache 'getGlobals' + local getGlobalSetsCache = ws.getCache 'getGlobalSets' + local needUpdateGlobals = ws.getCache 'needUpdateGlobals' + local uris = {} + for uri in pairs(needUpdateGlobals) do + uris[#uris+1] = uri + end + for _, uri in ipairs(uris) do + if needUpdateGlobals[uri] then + needUpdateGlobals[uri] = nil + if files.exists(uri) then + for name in pairs(getGlobalsOfFile(uri)) do + getGlobalCache[name] = nil + end + for name in pairs(getGlobalSetsOfFile(uri)) do + getGlobalSetsCache[name] = nil + end + end + end + end +end + +function vm.getGlobals(key) + checkNeedUpdate() + local cache = ws.getCache('getGlobals')[key] + if cache ~= nil then + return cache + end + cache = getGlobals(key) + ws.getCache('getGlobals')[key] = cache + return cache +end + +function vm.getGlobalSets(key) + checkNeedUpdate() + local cache = ws.getCache('getGlobalSets')[key] + if cache ~= nil then + return cache + end + tracy.ZoneBeginN('getGlobalSets') + cache = getGlobalSets(key) + ws.getCache('getGlobalSets')[key] = cache + tracy.ZoneEnd() + return cache +end + +files.watch(function (ev, uri) + if ev == 'update' then + local globalSubscribe = ws.getCache 'globalSubscribe' + local globalSetsSubscribe = ws.getCache 'globalSetsSubscribe' + local getGlobalCache = ws.getCache 'getGlobals' + local getGlobalSetsCache = ws.getCache 'getGlobalSets' + local needUpdateGlobals = ws.getCache 'needUpdateGlobals' + uri = files.asKey(uri) + if globalSubscribe[uri] then + for _, name in ipairs(globalSubscribe[uri]) do + getGlobalCache[name] = nil + getGlobalCache['*'] = nil + end + end + if globalSetsSubscribe[uri] then + for _, name in ipairs(globalSetsSubscribe[uri]) do + getGlobalSetsCache[name] = nil + getGlobalSetsCache['*'] = nil + end + end + needUpdateGlobals[uri] = true + elseif ev == 'create' then + --getGlobalsOfFile(uri) + --getGlobalSetsOfFile(uri) + end +end) |