perf: lazy load modules and dalay config loading till status redraw

This should make require'lualine' less expensive and make lualine
have less impact in startuptime
This commit is contained in:
shadmansaleh 2021-08-16 23:19:56 +06:00
parent d360039969
commit 0126ac0cc0
2 changed files with 73 additions and 52 deletions

View File

@ -1,20 +1,22 @@
-- Copyright (c) 2020-2021 hoob3rt -- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local highlight = require('lualine.highlight') local modules = require('lualine.utils.lazy_require'){
local loader = require('lualine.utils.loader') highlight = 'lualine.highlight',
local utils_section = require('lualine.utils.section') loader = 'lualine.utils.loader',
local utils = require('lualine.utils.utils') utils_section = 'lualine.utils.section',
local utils_notices = require('lualine.utils.notices') utils = 'lualine.utils.utils',
local config_module = require('lualine.config') utils_notices = 'lualine.utils.notices',
config_module = 'lualine.config',
local config = config_module.config }
local config -- Stores cureently applied config
local new_config = {} -- Stores config that will be applied
local function apply_transitional_separators(previous_section, current_section, local function apply_transitional_separators(previous_section, current_section,
next_section) next_section)
local function fill_section_separator(prev, next, sep, reverse) local function fill_section_separator(prev, next, sep, reverse)
if #sep == 0 then return 0 end if #sep == 0 then return 0 end
local transitional_highlight = highlight.get_transitional_highlights(prev, local transitional_highlight = modules.highlight.get_transitional_highlights(prev,
next, next,
reverse) reverse)
if transitional_highlight and #transitional_highlight > 0 then if transitional_highlight and #transitional_highlight > 0 then
@ -92,7 +94,7 @@ local function statusline(sections, is_focused)
for _, section_name in ipairs(section_sequence) do for _, section_name in ipairs(section_sequence) do
if sections['lualine_' .. section_name] then if sections['lualine_' .. section_name] then
-- insert highlight+components of this section to status_builder -- insert highlight+components of this section to status_builder
local section_data = utils_section.draw_section( local section_data = modules.utils_section.draw_section(
sections['lualine_' .. section_name], sections['lualine_' .. section_name],
section_name, is_focused) section_name, is_focused)
if #section_data > 0 then if #section_data > 0 then
@ -108,7 +110,7 @@ local function statusline(sections, is_focused)
-- midsection divider -- midsection divider
if not half_passed and status_builder[i].name > 'c' then if not half_passed and status_builder[i].name > 'c' then
table.insert(status, table.insert(status,
highlight.format_highlight(is_focused, 'lualine_c') .. '%=') modules.highlight.format_highlight(is_focused, 'lualine_c') .. '%=')
half_passed = true half_passed = true
end end
-- component separator needs to have fg = current_section.bg -- component separator needs to have fg = current_section.bg
@ -125,7 +127,7 @@ local function statusline(sections, is_focused)
-- incase none of x,y,z was configured lets not fill whole statusline with a,b,c section -- incase none of x,y,z was configured lets not fill whole statusline with a,b,c section
if not half_passed then if not half_passed then
table.insert(status, table.insert(status,
highlight.format_highlight(is_focused, 'lualine_c') .. '%=') modules.highlight.format_highlight(is_focused, 'lualine_c') .. '%=')
end end
return table.concat(status) return table.concat(status)
end end
@ -145,30 +147,6 @@ local function get_extension_sections(current_ft, is_focused)
return nil return nil
end end
local function status_dispatch()
-- disable on specific filetypes
local current_ft = vim.bo.filetype
local is_focused = utils.is_focused()
for _, ft in pairs(config.options.disabled_filetypes) do
if ft == current_ft then
vim.wo.statusline = ''
return ''
end
end
local extension_sections = get_extension_sections(current_ft, is_focused)
if is_focused then
if extension_sections ~= nil then
return statusline(extension_sections, is_focused)
end
return statusline(config.sections, is_focused)
else
if extension_sections ~= nil then
return statusline(extension_sections, is_focused)
end
return statusline(config.inactive_sections, is_focused)
end
end
local function tabline() return statusline(config.tabline, true) end local function tabline() return statusline(config.tabline, true) end
local function check_theme_name_deprecation(theme_name) local function check_theme_name_deprecation(theme_name)
@ -181,7 +159,7 @@ local function check_theme_name_deprecation(theme_name)
} }
if deprection_table[theme_name] then if deprection_table[theme_name] then
local correct_name = deprection_table[theme_name] local correct_name = deprection_table[theme_name]
utils_notices.add_notice(string.format([[ modules.utils_notices.add_notice(string.format([[
### options.theme ### options.theme
You're using `%s` as theme name . You're using `%s` as theme name .
It has recently been renamed to `%s`. It has recently been renamed to `%s`.
@ -218,7 +196,7 @@ This shouldn't happen.
Please report the issue at https://github.com/shadmansaleh/lualine.nvim/issues . Please report the issue at https://github.com/shadmansaleh/lualine.nvim/issues .
Also provide what colorscheme you're using. Also provide what colorscheme you're using.
]] ]]
utils_notices.add_notice(string.format(message_template, theme_name)) modules.utils_notices.add_notice(string.format(message_template, theme_name))
end end
local function setup_theme() local function setup_theme()
@ -226,7 +204,7 @@ local function setup_theme()
local theme_name = config.options.theme local theme_name = config.options.theme
if type(theme_name) == 'string' then if type(theme_name) == 'string' then
theme_name = check_theme_name_deprecation(theme_name) theme_name = check_theme_name_deprecation(theme_name)
local ok, theme = pcall(loader.load_theme, theme_name) local ok, theme = pcall(modules.loader.load_theme, theme_name)
if ok and theme then return theme end if ok and theme then return theme end
elseif type(theme_name) == 'table' then elseif type(theme_name) == 'table' then
-- use the provided theme as-is -- use the provided theme as-is
@ -234,14 +212,14 @@ local function setup_theme()
end end
if theme_name ~= 'auto' then if theme_name ~= 'auto' then
notify_theme_error(theme_name) notify_theme_error(theme_name)
local ok, theme = pcall(loader.load_theme, 'auto') local ok, theme = pcall(modules.loader.load_theme, 'auto')
if ok and theme then return theme end if ok and theme then return theme end
end end
notify_theme_error('auto') notify_theme_error('auto')
return loader.load_theme('gruvbox') return modules.loader.load_theme('gruvbox')
end end
local theme = get_theme_from_config() local theme = get_theme_from_config()
highlight.create_highlight_groups(theme) modules.highlight.create_highlight_groups(theme)
vim.cmd [[ vim.cmd [[
autocmd lualine ColorScheme * lua require'lualine.utils.utils'.reload_highlights() autocmd lualine ColorScheme * lua require'lualine.utils.utils'.reload_highlights()
autocmd lualine OptionSet background lua require'lualine'.setup() autocmd lualine OptionSet background lua require'lualine'.setup()
@ -257,10 +235,9 @@ end
local function set_statusline() local function set_statusline()
if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then
vim.go.statusline = "%{%v:lua.require'lualine'.statusline()%}" vim.cmd('autocmd lualine VimResized * redrawstatus')
vim.cmd([[ else
autocmd lualine VimResized * redrawstatus vim.go.statusline = nil
]])
end end
end end
@ -272,20 +249,51 @@ local function setup_augroup()
]] ]]
end end
local function setup(user_config) local function reset_lualine()
utils_notices.clear_notices() modules.utils_notices.clear_notices()
config = config_module.apply_configuration(user_config) config = modules.config_module.apply_configuration(new_config)
setup_augroup() setup_augroup()
setup_theme() setup_theme()
loader.load_all(config) modules.loader.load_all(config)
set_statusline() set_statusline()
set_tabline() set_tabline()
utils_notices.notice_message_startup() modules.utils_notices.notice_message_startup()
new_config = nil
end
local function status_dispatch()
-- disable on specific filetypes
if new_config then reset_lualine() end
local current_ft = vim.bo.filetype
local is_focused = modules.utils.is_focused()
for _, ft in pairs(config.options.disabled_filetypes) do
if ft == current_ft then
vim.wo.statusline = ''
return ''
end
end
local extension_sections = get_extension_sections(current_ft, is_focused)
if is_focused then
if extension_sections ~= nil then
return statusline(extension_sections, is_focused)
end
return statusline(config.sections, is_focused)
else
if extension_sections ~= nil then
return statusline(extension_sections, is_focused)
end
return statusline(config.inactive_sections, is_focused)
end
end
local function setup(user_config)
new_config = user_config or {}
vim.go.statusline = "%{%v:lua.require'lualine'.statusline()%}"
end end
return { return {
setup = setup, setup = setup,
statusline = status_dispatch, statusline = status_dispatch,
tabline = tabline, tabline = tabline,
get_config = config_module.get_config, get_config = function() return modules.config_module.get_config() end,
} }

View File

@ -0,0 +1,13 @@
return function(modules)
return setmetatable({}, {
__index = function(self, key)
local loaded = rawget(self, key)
if loaded ~= nil then return loaded end
local module_location = modules[key]
if module_location == nil then return nil end
local module = require(module_location)
rawset(self, key, module)
return module
end
})
end