-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,7 @@ | ||
-- Use an immutable table to only automatically load the preset that's are being used | ||
local M = setmetatable({}, { | ||
__index = function(_, key) | ||
local ok, result = pcall(require, 'feline.presets.' .. key) | ||
if ok then return result else return nil end | ||
end, | ||
|
||
__newindex = function(_, _, _) | ||
end, | ||
|
||
__metatable = false | ||
}) | ||
|
||
return M | ||
-- Use lazy_require to only load the preset that's are being used | ||
local lazy_require = require('feline.utils').lazy_require | ||
|
||
return { | ||
default = lazy_require('feline.presets.default'), | ||
noicon = lazy_require('feline.presets.noicon') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,77 @@ | ||
local vi_mode = require('feline.providers.vi_mode') | ||
local cursor = require('feline.providers.cursor') | ||
local file = require('feline.providers.file') | ||
local lsp = require('feline.providers.lsp') | ||
local git = require('feline.providers.git') | ||
|
||
local M = { | ||
vi_mode = vi_mode.vi_mode, | ||
|
||
position = cursor.position, | ||
line_percentage = cursor.line_percentage, | ||
scroll_bar = cursor.scroll_bar, | ||
|
||
file_info = file.file_info, | ||
file_size = file.file_size, | ||
file_type = file.file_type, | ||
file_encoding = file.file_encoding, | ||
|
||
git_branch = git.git_branch, | ||
git_diff_added = git.git_diff_added, | ||
git_diff_removed = git.git_diff_removed, | ||
git_diff_changed = git.git_diff_changed, | ||
|
||
lsp_client_names = lsp.lsp_client_names, | ||
diagnostic_errors = lsp.diagnostic_errors, | ||
diagnostic_warnings = lsp.diagnostic_warnings, | ||
diagnostic_hints = lsp.diagnostic_hints, | ||
diagnostic_info = lsp.diagnostic_info, | ||
local lazy_require = require('feline.utils').lazy_require | ||
|
||
-- All available provider categories (lazy loaded) | ||
local provider_categories = { | ||
vi_mode = lazy_require('feline.providers.vi_mode'), | ||
cursor = lazy_require('feline.providers.cursor'), | ||
file = lazy_require('feline.providers.file'), | ||
lsp = lazy_require('feline.providers.lsp'), | ||
git = lazy_require('feline.providers.git'), | ||
custom = {} | ||
} | ||
|
||
-- Categories that each provider belongs to | ||
local get_provider_category = { | ||
vi_mode = 'vi_mode', | ||
|
||
position = 'cursor', | ||
line_percentage = 'cursor', | ||
scroll_bar = 'cursor', | ||
|
||
file_info = 'file', | ||
file_size = 'file', | ||
file_type = 'file', | ||
file_encoding = 'file', | ||
|
||
git_branch = 'git', | ||
git_diff_added = 'git', | ||
git_diff_removed = 'git', | ||
git_diff_changed = 'git', | ||
|
||
lsp_client_names = 'lsp', | ||
diagnostic_errors = 'lsp', | ||
diagnostic_warnings = 'lsp', | ||
diagnostic_hints = 'lsp', | ||
diagnostic_info = 'lsp' | ||
} | ||
|
||
function M.add_provider(name, provider) | ||
if M[name] then | ||
vim.api.nvim_err_writeln(string.format( | ||
"Provider %s already exists! Please try using another name", | ||
name | ||
)) | ||
else | ||
M[name] = provider | ||
-- 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 | ||
end | ||
} | ||
|
||
return M | ||
-- Return a metatable that automatically loads and returns providers when their name is indexed | ||
return setmetatable({}, { | ||
__index = function(_, key) | ||
-- If a provider hasn't been loaded, load it by accessing its category and indexing the name | ||
-- which causes lazy_require to actually load the provider category | ||
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
-- Utility functions used across Feline | ||
local cmd = vim.api.nvim_command | ||
|
||
local M = {} | ||
|
||
-- Utility function to create augroups | ||
function M.create_augroup(autocmds, name) | ||
cmd('augroup ' .. name) | ||
cmd('autocmd!') | ||
|
||
for _, autocmd in ipairs(autocmds) do | ||
cmd('autocmd ' .. table.concat(autocmd, ' ')) | ||
end | ||
|
||
cmd('augroup END') | ||
end | ||
|
||
-- Lazy require function | ||
-- Only actually `require()`s a module when it gets used | ||
function M.lazy_require(module) | ||
return setmetatable({}, { | ||
__index = function(_, key) | ||
return require(module)[key] | ||
end, | ||
|
||
__newindex = function(_, _, _) | ||
end, | ||
|
||
__metatable = false | ||
}) | ||
end | ||
|
||
return M |