diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-10-12 19:16:05 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-10-12 19:16:05 +0800 |
commit | 0ccf3647a2c8213be6d53fa343f89fd656af9565 (patch) | |
tree | 4ac757fa784d7e5c0e03c8457f1bbf33a4e70da1 /script/workspace/require-path.lua | |
parent | ea2795520e68db8737bd6d0ab7a93d5e5a482346 (diff) | |
download | lua-language-server-0ccf3647a2c8213be6d53fa343f89fd656af9565.zip |
resolve performance issue by cache require-path
fix #1595
Diffstat (limited to 'script/workspace/require-path.lua')
-rw-r--r-- | script/workspace/require-path.lua | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/script/workspace/require-path.lua b/script/workspace/require-path.lua index 02b1996d..968511f2 100644 --- a/script/workspace/require-path.lua +++ b/script/workspace/require-path.lua @@ -13,6 +13,7 @@ local m = {} ---@field scp scope ---@field nameMap table<string, string> ---@field visibleCache table<string, require-manager.visibleResult[]> +---@field requireCache table<string, table> local mt = {} mt.__index = mt @@ -25,6 +26,7 @@ local function createRequireManager(scp) scp = scp, nameMap = {}, visibleCache = {}, + requireCache = {}, }, mt) end @@ -153,14 +155,10 @@ function mt:getVisiblePath(path) end --- 查找符合指定require name的所有uri ----@param suri uri ---@param name string ---@return uri[] ----@return table<uri, string>? -function mt:findUrisByRequireName(suri, name) - if type(name) ~= 'string' then - return {} - end +---@return table<uri, string> +function mt:searchUrisByRequireName(name) local searchers = config.get(self.scp.uri, 'Lua.runtime.path') local strict = config.get(self.scp.uri, 'Lua.runtime.pathStrict') local separator = config.get(self.scp.uri, 'Lua.completion.requireSeparator') @@ -174,7 +172,6 @@ function mt:findUrisByRequireName(suri, name) local tail = '/' .. furi.encode(fspath):gsub('^file:[/]*', '') for uri in files.eachFile(self.scp.uri) do if not searcherMap[uri] - and suri ~= uri and util.stringEndWith(uri, tail) then local parentUri = files.getLibraryUri(self.scp.uri, uri) or self.scp.uri if parentUri == nil or parentUri == '' then @@ -203,6 +200,35 @@ function mt:findUrisByRequireName(suri, name) return results, searcherMap end +--- 查找符合指定require name的所有uri,并排除当前文件 +---@param suri uri +---@param name string +---@return uri[] +---@return table<uri, string>? +function mt:findUrisByRequireName(suri, name) + if type(name) ~= 'string' then + return {} + end + local cache = self.requireCache[name] + if not cache then + local results, searcherMap = self:searchUrisByRequireName(name) + cache = { + results = results, + searcherMap = searcherMap, + } + self.requireCache[name] = cache + end + local results = {} + local searcherMap = {} + for _, uri in ipairs(cache.results) do + if uri ~= suri then + results[#results+1] = uri + searcherMap[uri] = cache.searcherMap[uri] + end + end + return results, searcherMap +end + ---@param uri uri ---@param path string ---@return require-manager.visibleResult[] |