Slight complexity reduction (#195)
* 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> Co-authored-by: hrsh7th <hrsh7th@gmail.com>
This commit is contained in:
@@ -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,13 @@ 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
|
||||||
|
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)
|
||||||
|
core.sources_by_name[core.sources[source_id]] = nil
|
||||||
core.sources[source_id] = nil
|
core.sources[source_id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -118,8 +123,8 @@ 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
|
local s = core.sources_by_name[c.name]
|
||||||
if c.name == s.name then
|
if s 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)
|
||||||
@@ -127,7 +132,6 @@ core.get_sources = function(statuses)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
return sources
|
return sources
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user