Skip to content

Commit

Permalink
refactor: modify how providers are added
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `require('feline.providers').add_provider()` no longer
works.
  • Loading branch information
famiu committed Sep 26, 2021
1 parent a678974 commit ce8ea32
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lua/feline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function M.setup(config)
M.providers = require('feline.providers')

for k, v in pairs(parse_config(config, 'custom_providers', 'table', {})) do
M.providers.add_provider(k, v)
M.providers[k] = v
end

local components = parse_config(config, 'components', 'table')
Expand Down
35 changes: 14 additions & 21 deletions lua/feline/providers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,6 @@ local get_provider_category = {
-- Providers that have been loaded
local loaded_providers = {}

-- Utility functions to manage providers
local utilities = {
-- Add a custom provider with specified name
add_provider = function(name, provider)
if get_provider_category[name] then
vim.api.nvim_err_writeln(string.format(
"Feline: error while adding provider: " ..
"Provider %s already exists! Please try using another name",
name
))
else
provider_categories.custom[name] = provider
get_provider_category[name] = 'custom'
end
end
}

-- Return a metatable that automatically loads and returns providers when their name is indexed
return setmetatable({}, {
__index = function(_, key)
Expand All @@ -63,15 +46,25 @@ return setmetatable({}, {
if not loaded_providers[key] then
local category = get_provider_category[key]

-- If a provider with that name isn't found, assume it's a utility function
-- and return that instead
if category then
loaded_providers[key] = provider_categories[category][key]
else
return utilities[key]
end
end

return loaded_providers[key]
end,
-- Add new custom providers by appending their value to the custom provider category and setting
-- the category of their name to 'custom'
__newindex = function(_, name, provider)
if get_provider_category[name] then
vim.api.nvim_err_writeln(string.format(
"Feline: error while adding provider: " ..
"Provider '%s' already exists! Please try using another name",
name
))
else
provider_categories.custom[name] = provider
get_provider_category[name] = 'custom'
end
end
})

0 comments on commit ce8ea32

Please sign in to comment.