* Slight complexity reduction

Do not loop twice over sources table, instead keep a table mapping names
to sources

* Update lua/cmp/core.lua

Co-authored-by: hrsh7th <hrsh7th@gmail.com>

* Fix multiple sources per name

* fix as suggested

Co-authored-by: hrsh7th <hrsh7th@gmail.com>
This commit is contained in:
tzachar
2021-09-14 08:58:54 +03:00
committed by GitHub
parent 2c92fef040
commit 13d4989c38

View File

@@ -72,6 +72,9 @@ end
---@type table<number, cmp.Source> ---@type table<number, cmp.Source>
core.sources = {} core.sources = {}
---@type table<string, cmp.Source[]>
core.sources_by_name = {}
---@type cmp.Context ---@type cmp.Context
core.context = context.new() core.context = context.new()
@@ -79,11 +82,20 @@ core.context = context.new()
---@param s cmp.Source ---@param s cmp.Source
core.register_source = function(s) core.register_source = function(s)
core.sources[s.id] = s core.sources[s.id] = s
if not core.sources_by_name[s.name] then
core.sources_by_name[s.name] = {}
end
table.insert(core.sources_by_name[s.name], s)
end end
---Unregister source ---Unregister source
---@param source_id string ---@param source_id string
core.unregister_source = function(source_id) core.unregister_source = function(source_id)
local name = core.sources[source_id].name
core.sources_by_name[name] = vim.tbl_filter(
function (source)
return source.id ~= source_id
end, core.sources_by_name[name])
core.sources[source_id] = nil core.sources[source_id] = nil
end end
@@ -118,12 +130,10 @@ end
core.get_sources = function(statuses) core.get_sources = function(statuses)
local sources = {} local sources = {}
for _, c in pairs(config.get().sources) do for _, c in pairs(config.get().sources) do
for _, s in pairs(core.sources) do for _, s in ipairs(core.sources_by_name[c.name] or {}) do
if c.name == s.name then if not statuses or vim.tbl_contains(statuses, s.status) then
if not statuses or vim.tbl_contains(statuses, s.status) then if s:is_available() then
if s:is_available() then table.insert(sources, s)
table.insert(sources, s)
end
end end
end end
end end