Skip to content

Commit

Permalink
feat: lazy load providers
Browse files Browse the repository at this point in the history
  • Loading branch information
famiu committed Sep 23, 2021
1 parent 662af72 commit fb14810
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 68 deletions.
14 changes: 2 additions & 12 deletions lua/feline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local fn = vim.fn
local api = vim.api
local cmd = api.nvim_command

local create_augroup = require('feline.utils').create_augroup

local M = {}

-- Parse configuration option with name config_name from config_dict and match its type
Expand All @@ -24,18 +26,6 @@ local function parse_config(config_dict, config_name, expected_type, default_val
end
end

-- Utility function to create augroups
local function 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

-- Update statusline of inactive windows on the current tabpage
function M.update_inactive_windows()
-- Uses vim.schedule to defer executing the function until after
Expand Down
20 changes: 6 additions & 14 deletions lua/feline/presets/init.lua
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')
}
111 changes: 73 additions & 38 deletions lua/feline/providers/init.lua
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
})
9 changes: 5 additions & 4 deletions lua/feline/providers/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
local M = {}

local api = vim.api
local lsp = vim.lsp

function M.is_lsp_attached(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()

return next(vim.lsp.buf_get_clients(bufnr)) ~= nil
return next(lsp.buf_get_clients(bufnr)) ~= nil
end

function M.get_diagnostics_count(severity, bufnr)
bufnr = bufnr or api.nvim_get_current_buf()

local active_clients = vim.lsp.buf_get_clients(bufnr)
local active_clients = lsp.buf_get_clients(bufnr)

if not active_clients then return nil end

local count = 0

for _, client in pairs(active_clients) do
count = count + vim.lsp.diagnostic.get_count(bufnr, severity, client.id)
count = count + lsp.diagnostic.get_count(bufnr, severity, client.id)
end

return count
Expand All @@ -34,7 +35,7 @@ end
function M.lsp_client_names(_, winid)
local clients = {}

for _, client in pairs(vim.lsp.buf_get_clients(api.nvim_win_get_buf(winid))) do
for _, client in pairs(lsp.buf_get_clients(api.nvim_win_get_buf(winid))) do
clients[#clients+1] = client.name
end

Expand Down
33 changes: 33 additions & 0 deletions lua/feline/utils.lua
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

0 comments on commit fb14810

Please sign in to comment.