refactor: moved loading to new module (#182)
* refactor: moved loading to new module * refactor: check_single_separator * refactor: simplified separator fixing * refactor: tests/ -> lua/tests/ * refactor: moved to nvim_err_writeln for errors * feat: moved config parsing to config.lua * feat: get config directly from config module * feat: added load_all function * tests: config parsing tests * added assert to luacheck globals
This commit is contained in:
parent
dc2c711a53
commit
82826ef661
|
@ -1,6 +1,6 @@
|
||||||
name: Tests
|
name: Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [master]
|
branches: [master]
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
name: Linting
|
name: Linting
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [master]
|
branches: [master]
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|
|
@ -44,4 +44,3 @@ tags
|
||||||
|
|
||||||
# helptags
|
# helptags
|
||||||
doc/tags
|
doc/tags
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
globals = {
|
globals = {
|
||||||
"vim",
|
"vim",
|
||||||
"vimp",
|
"vimp",
|
||||||
|
"assert"
|
||||||
}
|
}
|
||||||
|
|
6
Makefile
6
Makefile
|
@ -2,12 +2,12 @@
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
@luacheck lua/lualine
|
@luacheck lua/lualine
|
||||||
@luacheck tests
|
@luacheck lua/tests
|
||||||
|
|
||||||
format:
|
format:
|
||||||
@for file in `find -name '*.lua'`;do lua-format $$file -i; done;
|
@for file in `find . -name '*.lua'`;do lua-format $$file -i; done;
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@nvim --headless -u tests/minimal_init.lua -c "PlenaryBustedDirectory tests/ { minimal_init = './tests/minimal_init.lua' }"
|
@nvim --headless -u lua/tests/minimal_init.lua -c "PlenaryBustedDirectory lua/tests/ { minimal_init = './lua/tests/minimal_init.lua' }"
|
||||||
|
|
||||||
check: lint test
|
check: lint test
|
||||||
|
|
|
@ -296,7 +296,7 @@ option (default_value)
|
||||||
Supported components: all
|
Supported components: all
|
||||||
|
|
||||||
• disabled_filetypes ({})
|
• disabled_filetypes ({})
|
||||||
Disables lualine for specific filetypes. It works on entire
|
Disables lualine for specific filetypes. It works on entire
|
||||||
statusline instead of on a single component.
|
statusline instead of on a single component.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
@ -325,7 +325,7 @@ option (default_value)
|
||||||
`lua color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'}`
|
`lua color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'}`
|
||||||
the members of color table are optional and default to theme
|
the members of color table are optional and default to theme
|
||||||
Color option can also be a string containing highlight group name >
|
Color option can also be a string containing highlight group name >
|
||||||
color = "WarningMsg"`
|
color = "WarningMsg"`
|
||||||
< One neat trick set the color to highlight group name then change
|
< One neat trick set the color to highlight group name then change
|
||||||
that highlight with :hi command to change color of that component
|
that highlight with :hi command to change color of that component
|
||||||
at runtime.
|
at runtime.
|
||||||
|
@ -410,7 +410,7 @@ In addition, some components have unique options.
|
||||||
Displays relative path if set to `true`, absolute path if set to `false`
|
Displays relative path if set to `true`, absolute path if set to `false`
|
||||||
|
|
||||||
• shorten (true)
|
• shorten (true)
|
||||||
if `full_path` is `true` and `shorten` is `false` it shortens
|
if `full_path` is `true` and `shorten` is `false` it shortens
|
||||||
absolute path `aaa/bbb/ccc/file` to `a/b/c/file`
|
absolute path `aaa/bbb/ccc/file` to `a/b/c/file`
|
||||||
|
|
||||||
• symbols (`{modified = '[+]', readonly = '[-]'}`)
|
• symbols (`{modified = '[+]', readonly = '[-]'}`)
|
||||||
|
|
|
@ -5,8 +5,9 @@ local FileName = require('lualine.component'):new()
|
||||||
FileName.new = function(self, options, child)
|
FileName.new = function(self, options, child)
|
||||||
local new_instance = self._parent:new(options, child or FileName)
|
local new_instance = self._parent:new(options, child or FileName)
|
||||||
local default_symbols = {modified = '[+]', readonly = '[-]'}
|
local default_symbols = {modified = '[+]', readonly = '[-]'}
|
||||||
new_instance.options.symbols =
|
new_instance.options.symbols = vim.tbl_extend('force', default_symbols,
|
||||||
vim.tbl_extend('force', default_symbols, new_instance.options.symbols or {})
|
new_instance.options.symbols or
|
||||||
|
{})
|
||||||
|
|
||||||
-- setting defaults
|
-- setting defaults
|
||||||
if new_instance.options.file_status == nil then
|
if new_instance.options.file_status == nil then
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
-- Copyright (c) 2020-2021 hoob3rt
|
||||||
|
-- MIT license, see LICENSE for more details.
|
||||||
|
local config = {
|
||||||
|
options = {
|
||||||
|
icons_enabled = true,
|
||||||
|
theme = 'gruvbox',
|
||||||
|
component_separators = {'', ''},
|
||||||
|
section_separators = {'', ''},
|
||||||
|
disabled_filetypes = {}
|
||||||
|
},
|
||||||
|
sections = {
|
||||||
|
lualine_a = {'mode'},
|
||||||
|
lualine_b = {'branch'},
|
||||||
|
lualine_c = {'filename'},
|
||||||
|
lualine_x = {'encoding', 'fileformat', 'filetype'},
|
||||||
|
lualine_y = {'progress'},
|
||||||
|
lualine_z = {'location'}
|
||||||
|
},
|
||||||
|
inactive_sections = {
|
||||||
|
lualine_a = {},
|
||||||
|
lualine_b = {},
|
||||||
|
lualine_c = {'filename'},
|
||||||
|
lualine_x = {'location'},
|
||||||
|
lualine_y = {},
|
||||||
|
lualine_z = {}
|
||||||
|
},
|
||||||
|
tabline = {},
|
||||||
|
extensions = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- change separator format 'x' or {'x'} to {'x', 'x'}
|
||||||
|
local function fix_separators(separators)
|
||||||
|
if separators ~= nil then
|
||||||
|
if type(separators) == 'string' then
|
||||||
|
return {separators, separators}
|
||||||
|
elseif type(separators) == 'table' and #separators == 1 then
|
||||||
|
return {separators[1], separators[1]}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return separators
|
||||||
|
end
|
||||||
|
|
||||||
|
local function apply_configuration(config_table)
|
||||||
|
local function parse_sections(section_group_name)
|
||||||
|
if not config_table[section_group_name] then return end
|
||||||
|
for section_name, section in pairs(config_table[section_group_name]) do
|
||||||
|
config[section_group_name][section_name] =
|
||||||
|
config_table[section_group_name][section_name]
|
||||||
|
if type(section) == 'table' then
|
||||||
|
for _, component in pairs(section) do
|
||||||
|
if type(component) == 'table' and type(component[2]) == 'table' then
|
||||||
|
local options = component[2]
|
||||||
|
component[2] = nil
|
||||||
|
for key, val in pairs(options) do component[key] = val end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
parse_sections('options')
|
||||||
|
parse_sections('sections')
|
||||||
|
parse_sections('inactive_sections')
|
||||||
|
parse_sections('tabline')
|
||||||
|
if config_table.extensions then config.extensions = config_table.extensions end
|
||||||
|
config.options.section_separators = fix_separators(
|
||||||
|
config.options.section_separators)
|
||||||
|
config.options.component_separators = fix_separators(
|
||||||
|
config.options.component_separators)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {config = config, apply_configuration = apply_configuration}
|
|
@ -1,33 +0,0 @@
|
||||||
local M = {}
|
|
||||||
|
|
||||||
M.options = {
|
|
||||||
icons_enabled = true,
|
|
||||||
theme = 'gruvbox',
|
|
||||||
component_separators = {'', ''},
|
|
||||||
section_separators = {'', ''},
|
|
||||||
disabled_filetypes = {},
|
|
||||||
}
|
|
||||||
|
|
||||||
M.sections = {
|
|
||||||
lualine_a = {'mode'},
|
|
||||||
lualine_b = {'branch'},
|
|
||||||
lualine_c = {'filename'},
|
|
||||||
lualine_x = {'encoding', 'fileformat', 'filetype'},
|
|
||||||
lualine_y = {'progress'},
|
|
||||||
lualine_z = {'location'}
|
|
||||||
}
|
|
||||||
|
|
||||||
M.inactive_sections = {
|
|
||||||
lualine_a = {},
|
|
||||||
lualine_b = {},
|
|
||||||
lualine_c = {'filename'},
|
|
||||||
lualine_x = {'location'},
|
|
||||||
lualine_y = {},
|
|
||||||
lualine_z = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
M.tabline = {}
|
|
||||||
|
|
||||||
M.extensions = {}
|
|
||||||
|
|
||||||
return M
|
|
|
@ -1,118 +1,11 @@
|
||||||
-- 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 utils_section = require('lualine.utils.section')
|
|
||||||
local highlight = require('lualine.highlight')
|
local highlight = require('lualine.highlight')
|
||||||
local config = {}
|
local loader = require('lualine.utils.loader')
|
||||||
|
local utils_section = require('lualine.utils.section')
|
||||||
|
local config_module = require('lualine.config')
|
||||||
|
|
||||||
local function apply_configuration(config_table)
|
local config = config_module.config
|
||||||
local function parse_sections(section_group_name)
|
|
||||||
if not config_table[section_group_name] then return end
|
|
||||||
for section_name, section in pairs(config_table[section_group_name]) do
|
|
||||||
config[section_group_name][section_name] =
|
|
||||||
config_table[section_group_name][section_name]
|
|
||||||
if type(section) == 'table' then
|
|
||||||
for _, component in pairs(section) do
|
|
||||||
if type(component) == 'table' and type(component[2]) == 'table' then
|
|
||||||
local options = component[2]
|
|
||||||
component[2] = nil
|
|
||||||
for key, val in pairs(options) do component[key] = val end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
parse_sections('options')
|
|
||||||
parse_sections('sections')
|
|
||||||
parse_sections('inactive_sections')
|
|
||||||
parse_sections('tabline')
|
|
||||||
if config_table.extensions then config.extensions = config_table.extensions end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function check_single_separator()
|
|
||||||
local compoennt_separator = config.options.component_separators
|
|
||||||
local section_separator = config.options.section_separators
|
|
||||||
if config.options.component_separators ~= nil then
|
|
||||||
if type(config.options.component_separators) == 'string' then
|
|
||||||
config.options.component_separators =
|
|
||||||
{compoennt_separator, compoennt_separator}
|
|
||||||
elseif #config.options.component_separators == 1 then
|
|
||||||
config.options.component_separators =
|
|
||||||
{
|
|
||||||
config.options.component_separators[1],
|
|
||||||
config.options.component_separators[1]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if config.options.section_separators ~= nil then
|
|
||||||
if type(config.options.section_separators) == 'string' then
|
|
||||||
config.options.section_separators = {section_separator, section_separator}
|
|
||||||
elseif #config.options.section_separators == 1 then
|
|
||||||
config.options.section_separators =
|
|
||||||
{
|
|
||||||
config.options.section_separators[1],
|
|
||||||
config.options.section_separators[1]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function component_loader(component)
|
|
||||||
if type(component[1]) == 'function' then
|
|
||||||
return
|
|
||||||
require 'lualine.components.special.function_component':new(component)
|
|
||||||
end
|
|
||||||
if type(component[1]) == 'string' then
|
|
||||||
-- load the component
|
|
||||||
local ok, loaded_component = pcall(require,
|
|
||||||
'lualine.components.' .. component[1])
|
|
||||||
if ok then
|
|
||||||
component.component_name = component[1]
|
|
||||||
loaded_component = loaded_component:new(component)
|
|
||||||
elseif component[1]:find('[gvtwb]?o?:') == 1 then
|
|
||||||
loaded_component =
|
|
||||||
require 'lualine.components.special.vim_var_component':new(component)
|
|
||||||
else
|
|
||||||
loaded_component =
|
|
||||||
require 'lualine.components.special.eval_func_component':new(component)
|
|
||||||
end
|
|
||||||
return loaded_component
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function load_sections(sections)
|
|
||||||
local async_loader
|
|
||||||
async_loader = vim.loop.new_async(vim.schedule_wrap(function()
|
|
||||||
for section_name, section in pairs(sections) do
|
|
||||||
for index, component in pairs(section) do
|
|
||||||
if type(component) == 'string' or type(component) == 'function' then
|
|
||||||
component = {component}
|
|
||||||
end
|
|
||||||
component.self = {}
|
|
||||||
component.self.section = section_name
|
|
||||||
-- apply default args
|
|
||||||
component = vim.tbl_extend('keep', component, config.options)
|
|
||||||
section[index] = component_loader(component)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
async_loader:close()
|
|
||||||
end))
|
|
||||||
async_loader:send()
|
|
||||||
end
|
|
||||||
|
|
||||||
local function load_components()
|
|
||||||
load_sections(config.sections)
|
|
||||||
load_sections(config.inactive_sections)
|
|
||||||
load_sections(config.tabline)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function load_extensions()
|
|
||||||
for index, extension in pairs(config.extensions) do
|
|
||||||
local local_extension = require('lualine.extensions.' .. extension)
|
|
||||||
load_sections(local_extension.sections)
|
|
||||||
load_sections(local_extension.inactive_sections)
|
|
||||||
config.extensions[index] = local_extension
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function statusline(sections, is_focused)
|
local function statusline(sections, is_focused)
|
||||||
local function create_status_builder()
|
local function create_status_builder()
|
||||||
|
@ -239,33 +132,30 @@ local function tabline() return statusline(config.tabline, true) end
|
||||||
|
|
||||||
local function setup_theme()
|
local function setup_theme()
|
||||||
local async_loader
|
local async_loader
|
||||||
async_loader = vim.loop.new_async(vim.schedule_wrap(function()
|
async_loader = vim.loop.new_async(vim.schedule_wrap(
|
||||||
local function get_theme_from_config()
|
function()
|
||||||
local theme_name = config.options.theme
|
local function get_theme_from_config()
|
||||||
if type(theme_name) == 'string' then
|
local theme_name = config.options.theme
|
||||||
local ok, theme = pcall(require, 'lualine.themes.' .. theme_name)
|
if type(theme_name) == 'string' then
|
||||||
if ok then return theme end
|
local ok, theme = pcall(require, 'lualine.themes.' .. theme_name)
|
||||||
elseif type(theme_name) == 'table' then
|
if ok then return theme end
|
||||||
-- use the provided theme as-is
|
elseif type(theme_name) == 'table' then
|
||||||
return config.options.theme
|
-- use the provided theme as-is
|
||||||
end
|
return config.options.theme
|
||||||
vim.api.nvim_echo({
|
end
|
||||||
{
|
vim.api.nvim_err_writeln('theme ' .. tostring(theme_name) ..
|
||||||
'theme ' .. tostring(theme_name) .. ' not found, defaulting to gruvbox',
|
' not found, defaulting to gruvbox')
|
||||||
'ErrorMsg'
|
return require 'lualine.themes.gruvbox'
|
||||||
}
|
end
|
||||||
}, true, {})
|
local theme = get_theme_from_config()
|
||||||
return require 'lualine.themes.gruvbox'
|
highlight.create_highlight_groups(theme)
|
||||||
end
|
vim.api.nvim_exec([[
|
||||||
local theme = get_theme_from_config()
|
augroup lualine
|
||||||
highlight.create_highlight_groups(theme)
|
autocmd ColorScheme * lua require'lualine.utils.utils'.reload_highlights()
|
||||||
vim.api.nvim_exec([[
|
augroup END
|
||||||
augroup lualine
|
]], false)
|
||||||
autocmd ColorScheme * lua require'lualine.utils.utils'.reload_highlights()
|
async_loader:close()
|
||||||
augroup END
|
end))
|
||||||
]], false)
|
|
||||||
async_loader:close()
|
|
||||||
end))
|
|
||||||
async_loader:send()
|
async_loader:send()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -280,23 +170,23 @@ 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.o.statusline = '%!v:lua.require\'lualine\'.statusline()'
|
vim.o.statusline = '%!v:lua.require\'lualine\'.statusline()'
|
||||||
vim.api.nvim_exec([[
|
vim.api.nvim_exec([[
|
||||||
augroup lualine
|
augroup lualine
|
||||||
autocmd!
|
autocmd!
|
||||||
autocmd WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline()
|
autocmd WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline()
|
||||||
autocmd WinEnter,BufEnter * set statusline<
|
autocmd WinEnter,BufEnter * set statusline<
|
||||||
augroup END
|
augroup END
|
||||||
]], false)
|
]], false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function setup(user_config)
|
local function setup(user_config)
|
||||||
config = vim.deepcopy(require'lualine.defaults')
|
if user_config then
|
||||||
if user_config then apply_configuration(user_config)
|
config_module.apply_configuration(user_config)
|
||||||
elseif vim.g.lualine then apply_configuration(vim.g.lualine) end
|
elseif vim.g.lualine then
|
||||||
check_single_separator()
|
config_module.apply_configuration(vim.g.lualine)
|
||||||
|
end
|
||||||
setup_theme()
|
setup_theme()
|
||||||
load_components()
|
loader.load_all(config)
|
||||||
load_extensions()
|
|
||||||
set_statusline()
|
set_statusline()
|
||||||
set_tabline()
|
set_tabline()
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,41 +17,41 @@ local colors = {
|
||||||
-- LuaFormatter on
|
-- LuaFormatter on
|
||||||
|
|
||||||
modus_vivendi.normal = {
|
modus_vivendi.normal = {
|
||||||
a = {bg = colors.blue, fg = colors.lightgray, gui = 'bold'},
|
a = {bg = colors.blue, fg = colors.lightgray, gui = 'bold'},
|
||||||
b = {bg = colors.lightgray, fg = colors.blue},
|
b = {bg = colors.lightgray, fg = colors.blue},
|
||||||
c = {bg = colors.gray, fg = colors.white}
|
c = {bg = colors.gray, fg = colors.white}
|
||||||
}
|
}
|
||||||
|
|
||||||
modus_vivendi.insert = {
|
modus_vivendi.insert = {
|
||||||
a = {bg = colors.cyan, fg = colors.lightgray, gui = 'bold'},
|
a = {bg = colors.cyan, fg = colors.lightgray, gui = 'bold'},
|
||||||
b = {bg = colors.lightgray, fg = colors.cyan},
|
b = {bg = colors.lightgray, fg = colors.cyan},
|
||||||
c = {bg = colors.gray, fg = colors.white}
|
c = {bg = colors.gray, fg = colors.white}
|
||||||
}
|
}
|
||||||
|
|
||||||
modus_vivendi.visual = {
|
modus_vivendi.visual = {
|
||||||
a = {bg = colors.magenta, fg = colors.lightgray, gui = 'bold'},
|
a = {bg = colors.magenta, fg = colors.lightgray, gui = 'bold'},
|
||||||
b = {bg = colors.lightgray, fg = colors.magenta},
|
b = {bg = colors.lightgray, fg = colors.magenta},
|
||||||
c = {bg = colors.gray, fg = colors.white}
|
c = {bg = colors.gray, fg = colors.white}
|
||||||
}
|
}
|
||||||
|
|
||||||
modus_vivendi.replace = {
|
modus_vivendi.replace = {
|
||||||
a = {bg = colors.red, fg = colors.lightgray, gui = 'bold'},
|
a = {bg = colors.red, fg = colors.lightgray, gui = 'bold'},
|
||||||
b = {bg = colors.lightgray, fg = colors.red},
|
b = {bg = colors.lightgray, fg = colors.red},
|
||||||
c = {bg = colors.gray, fg = colors.white}
|
c = {bg = colors.gray, fg = colors.white}
|
||||||
}
|
}
|
||||||
|
|
||||||
modus_vivendi.command = {
|
modus_vivendi.command = {
|
||||||
a = {bg = colors.green, fg = colors.lightgray, gui = 'bold'},
|
a = {bg = colors.green, fg = colors.lightgray, gui = 'bold'},
|
||||||
b = {bg = colors.lightgray, fg = colors.green},
|
b = {bg = colors.lightgray, fg = colors.green},
|
||||||
c = {bg = colors.gray, fg = colors.white}
|
c = {bg = colors.gray, fg = colors.white}
|
||||||
}
|
}
|
||||||
|
|
||||||
modus_vivendi.terminal = modus_vivendi.normal
|
modus_vivendi.terminal = modus_vivendi.normal
|
||||||
|
|
||||||
modus_vivendi.inactive = {
|
modus_vivendi.inactive = {
|
||||||
a = {bg = colors.darkgray, fg = colors.lightgray, gui = 'bold'},
|
a = {bg = colors.darkgray, fg = colors.lightgray, gui = 'bold'},
|
||||||
b = {bg = colors.darkgray, fg = colors.lightgray},
|
b = {bg = colors.darkgray, fg = colors.lightgray},
|
||||||
c = {bg = colors.darkgray, fg = colors.lightgray}
|
c = {bg = colors.darkgray, fg = colors.lightgray}
|
||||||
}
|
}
|
||||||
|
|
||||||
return modus_vivendi
|
return modus_vivendi
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
-- Copyright (c) 2020-2021 hoob3rt
|
||||||
|
-- MIT license, see LICENSE for more details.
|
||||||
|
local function component_loader(component)
|
||||||
|
if type(component[1]) == 'function' then
|
||||||
|
return
|
||||||
|
require 'lualine.components.special.function_component':new(component)
|
||||||
|
end
|
||||||
|
if type(component[1]) == 'string' then
|
||||||
|
-- load the component
|
||||||
|
local ok, loaded_component = pcall(require,
|
||||||
|
'lualine.components.' .. component[1])
|
||||||
|
if ok then
|
||||||
|
component.component_name = component[1]
|
||||||
|
loaded_component = loaded_component:new(component)
|
||||||
|
elseif component[1]:find('[gvtwb]?o?:') == 1 then
|
||||||
|
loaded_component =
|
||||||
|
require 'lualine.components.special.vim_var_component':new(component)
|
||||||
|
else
|
||||||
|
loaded_component =
|
||||||
|
require 'lualine.components.special.eval_func_component':new(component)
|
||||||
|
end
|
||||||
|
return loaded_component
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function load_sections(sections, options)
|
||||||
|
local async_loader
|
||||||
|
async_loader = vim.loop.new_async(vim.schedule_wrap(
|
||||||
|
function()
|
||||||
|
for section_name, section in pairs(sections) do
|
||||||
|
for index, component in pairs(section) do
|
||||||
|
if type(component) == 'string' or type(component) == 'function' then
|
||||||
|
component = {component}
|
||||||
|
end
|
||||||
|
component.self = {}
|
||||||
|
component.self.section = section_name
|
||||||
|
-- apply default args
|
||||||
|
component = vim.tbl_extend('keep', component, options)
|
||||||
|
section[index] = component_loader(component)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
async_loader:close()
|
||||||
|
end))
|
||||||
|
async_loader:send()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function load_components(config)
|
||||||
|
load_sections(config.sections, config.options)
|
||||||
|
load_sections(config.inactive_sections, config.options)
|
||||||
|
load_sections(config.tabline, config.options)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function load_extensions(config)
|
||||||
|
for index, extension in pairs(config.extensions) do
|
||||||
|
local local_extension = require('lualine.extensions.' .. extension)
|
||||||
|
load_sections(local_extension.sections, config.options)
|
||||||
|
load_sections(local_extension.inactive_sections, config.options)
|
||||||
|
config.extensions[index] = local_extension
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function load_all(config)
|
||||||
|
load_components(config)
|
||||||
|
load_extensions(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {load_all = load_all}
|
|
@ -1,9 +1,7 @@
|
||||||
-- Copyright (c) 2020-2021 hoob3rt
|
-- Copyright (c) 2020-2021 hoob3rt
|
||||||
-- MIT license, see LICENSE for more details.
|
-- MIT license, see LICENSE for more details.
|
||||||
-- LuaFormatter off
|
|
||||||
|
|
||||||
local Mode = {}
|
local Mode = {}
|
||||||
|
-- LuaFormatter off
|
||||||
Mode.map = {
|
Mode.map = {
|
||||||
['n'] = 'NORMAL',
|
['n'] = 'NORMAL',
|
||||||
['no'] = 'O-PENDING',
|
['no'] = 'O-PENDING',
|
||||||
|
|
|
@ -7,7 +7,8 @@ function M.draw_section(section, highlight_name)
|
||||||
local status = {}
|
local status = {}
|
||||||
for _, component in pairs(section) do
|
for _, component in pairs(section) do
|
||||||
-- load components into status table
|
-- load components into status table
|
||||||
if type(component) ~= 'table' or (type(component) == 'table' and not component.component_no) then
|
if type(component) ~= 'table' or
|
||||||
|
(type(component) == 'table' and not component.component_no) then
|
||||||
return '' -- unknown element in section. section posibly not yet loaded
|
return '' -- unknown element in section. section posibly not yet loaded
|
||||||
end
|
end
|
||||||
table.insert(status, component:draw(highlight_name))
|
table.insert(status, component:draw(highlight_name))
|
||||||
|
@ -33,7 +34,7 @@ function M.draw_section(section, highlight_name)
|
||||||
-- Remove component separator when color option is used to color background
|
-- Remove component separator when color option is used to color background
|
||||||
if (type(section[component_no].options.color) == 'table' and
|
if (type(section[component_no].options.color) == 'table' and
|
||||||
section[component_no].options.color.bg) or
|
section[component_no].options.color.bg) or
|
||||||
type(section[component_no].options.color) == 'string' then
|
type(section[component_no].options.color) == 'string' then
|
||||||
next_component_colored = true
|
next_component_colored = true
|
||||||
status[component_no] = section[component_no]:strip_separator()
|
status[component_no] = section[component_no]:strip_separator()
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
local luassert = require 'luassert'
|
local eq = assert.are.same
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.eq = luassert.are.same
|
|
||||||
M.neq = luassert.are_not.same
|
|
||||||
M.meths = setmetatable({}, {
|
M.meths = setmetatable({}, {
|
||||||
__index = function(_, key) return vim.api['nvim_' .. key] end
|
__index = function(_, key) return vim.api['nvim_' .. key] end
|
||||||
})
|
})
|
||||||
|
@ -12,7 +11,7 @@ M.assert_component = function(component, opts, result)
|
||||||
-- for testing global options
|
-- for testing global options
|
||||||
if component == nil then component = 'special.function_component' end
|
if component == nil then component = 'special.function_component' end
|
||||||
local comp = require('lualine.components.' .. component):new(opts)
|
local comp = require('lualine.components.' .. component):new(opts)
|
||||||
M.eq(result, comp:draw(opts.hl))
|
eq(result, comp:draw(opts.hl))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- sets defaults for component options
|
-- sets defaults for component options
|
|
@ -1,7 +1,7 @@
|
||||||
local helpers = require 'tests.helpers'
|
local helpers = require 'tests.helpers'
|
||||||
|
|
||||||
local eq = helpers.eq
|
local eq = assert.are.same
|
||||||
local neq = helpers.neq
|
local neq = assert.are_not.same
|
||||||
local assert_component = helpers.assert_component
|
local assert_component = helpers.assert_component
|
||||||
local build_component_opts = helpers.build_component_opts
|
local build_component_opts = helpers.build_component_opts
|
||||||
local stub = require 'luassert.stub'
|
local stub = require 'luassert.stub'
|
||||||
|
@ -9,24 +9,24 @@ local stub = require 'luassert.stub'
|
||||||
describe('Component:', function()
|
describe('Component:', function()
|
||||||
it('can select separators', function()
|
it('can select separators', function()
|
||||||
local opts = build_component_opts()
|
local opts = build_component_opts()
|
||||||
local comp =
|
local comp = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(opts)
|
opts)
|
||||||
-- correct for lualine_c
|
-- correct for lualine_c
|
||||||
eq('', comp.options.separator)
|
eq('', comp.options.separator)
|
||||||
local opts2 = build_component_opts({self = {section = 'lualine_y'}})
|
local opts2 = build_component_opts({self = {section = 'lualine_y'}})
|
||||||
local comp2 =
|
local comp2 = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(opts2)
|
opts2)
|
||||||
-- correct for lualine_u
|
-- correct for lualine_u
|
||||||
eq('', comp2.options.separator)
|
eq('', comp2.options.separator)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('can provide unique identifier', function()
|
it('can provide unique identifier', function()
|
||||||
local opts1 = build_component_opts()
|
local opts1 = build_component_opts()
|
||||||
local comp1 =
|
local comp1 = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(opts1)
|
opts1)
|
||||||
local opts2 = build_component_opts()
|
local opts2 = build_component_opts()
|
||||||
local comp2 =
|
local comp2 = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(opts2)
|
opts2)
|
||||||
neq(comp1.component_no, comp2.component_no)
|
neq(comp1.component_no, comp2.component_no)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -36,8 +36,8 @@ describe('Component:', function()
|
||||||
local hl = require 'lualine.highlight'
|
local hl = require 'lualine.highlight'
|
||||||
stub(hl, 'create_component_highlight_group')
|
stub(hl, 'create_component_highlight_group')
|
||||||
hl.create_component_highlight_group.returns('MyCompHl')
|
hl.create_component_highlight_group.returns('MyCompHl')
|
||||||
local comp1 =
|
local comp1 = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(opts1)
|
opts1)
|
||||||
eq('MyCompHl', comp1.options.color_highlight)
|
eq('MyCompHl', comp1.options.color_highlight)
|
||||||
-- color highlight wan't in options when create_comp_hl was
|
-- color highlight wan't in options when create_comp_hl was
|
||||||
-- called so remove it before assert
|
-- called so remove it before assert
|
||||||
|
@ -48,8 +48,8 @@ describe('Component:', function()
|
||||||
comp1.options)
|
comp1.options)
|
||||||
hl.create_component_highlight_group:revert()
|
hl.create_component_highlight_group:revert()
|
||||||
local opts2 = build_component_opts({color = 'MyHl'})
|
local opts2 = build_component_opts({color = 'MyHl'})
|
||||||
local comp2 =
|
local comp2 = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(opts2)
|
opts2)
|
||||||
eq('MyHl', comp2.options.color_highlight_link)
|
eq('MyHl', comp2.options.color_highlight_link)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -197,7 +197,7 @@ describe('Component:', function()
|
||||||
stub(hl, 'component_format_highlight')
|
stub(hl, 'component_format_highlight')
|
||||||
hl.component_format_highlight.returns('%#MyCompHl#')
|
hl.component_format_highlight.returns('%#MyCompHl#')
|
||||||
local comp2 =
|
local comp2 =
|
||||||
require('lua.lualine.components.special.function_component'):new(opts2)
|
require('lualine.components.special.function_component'):new(opts2)
|
||||||
assert_component(nil, opts2, '%#MyCompHl#test')
|
assert_component(nil, opts2, '%#MyCompHl#test')
|
||||||
assert.stub(hl.component_format_highlight).was_called_with(
|
assert.stub(hl.component_format_highlight).was_called_with(
|
||||||
comp2.options.color_highlight)
|
comp2.options.color_highlight)
|
|
@ -0,0 +1,179 @@
|
||||||
|
local eq = assert.are.same
|
||||||
|
|
||||||
|
describe('config parsing', function()
|
||||||
|
local config_module = require 'lualine.config'
|
||||||
|
|
||||||
|
describe('options', function()
|
||||||
|
describe('icons_enabled', function()
|
||||||
|
it('default', function()
|
||||||
|
config_module.apply_configuration({})
|
||||||
|
eq(config_module.config.options.icons_enabled, true)
|
||||||
|
end)
|
||||||
|
it('custom', function()
|
||||||
|
local config = {options = {icons_enabled = false}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.icons_enabled, false)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('theme', function()
|
||||||
|
it('default', function()
|
||||||
|
config_module.apply_configuration({})
|
||||||
|
eq(config_module.config.options.theme, 'gruvbox')
|
||||||
|
end)
|
||||||
|
it('custom', function()
|
||||||
|
local config = {options = {theme = 'nord'}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.theme, 'nord')
|
||||||
|
config = {options = {theme = {}}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.theme, {})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('separators', function()
|
||||||
|
it('default', function()
|
||||||
|
config_module.apply_configuration({})
|
||||||
|
eq(config_module.config.options.component_separators, {'', ''})
|
||||||
|
eq(config_module.config.options.section_separators, {'', ''})
|
||||||
|
end)
|
||||||
|
it('double separators', function()
|
||||||
|
local config = {
|
||||||
|
options = {
|
||||||
|
component_separators = {'a', 'b'},
|
||||||
|
section_separators = {'c', 'd'}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.component_separators, {'a', 'b'})
|
||||||
|
eq(config_module.config.options.section_separators, {'c', 'd'})
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('single separator', function()
|
||||||
|
it('string', function()
|
||||||
|
local config = {
|
||||||
|
options = {component_separators = 'a', section_separators = 'b'}
|
||||||
|
}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.component_separators, {'a', 'a'})
|
||||||
|
eq(config_module.config.options.section_separators, {'b', 'b'})
|
||||||
|
end)
|
||||||
|
it('table', function()
|
||||||
|
local config = {
|
||||||
|
options = {component_separators = {'a'}, section_separators = {'b'}}
|
||||||
|
}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.component_separators, {'a', 'a'})
|
||||||
|
eq(config_module.config.options.section_separators, {'b', 'b'})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
it('no seprarators', function()
|
||||||
|
local config = {
|
||||||
|
options = {component_separators = {}, section_separators = {}}
|
||||||
|
}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.component_separators, {})
|
||||||
|
eq(config_module.config.options.section_separators, {})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('disabled filetypes', function()
|
||||||
|
it('default', function()
|
||||||
|
config_module.apply_configuration({})
|
||||||
|
eq(config_module.config.options.disabled_filetypes, {})
|
||||||
|
end)
|
||||||
|
it('custom', function()
|
||||||
|
local config = {options = {disabled_filetypes = {'lua'}}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.disabled_filetypes, {'lua'})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('non default global option', function()
|
||||||
|
it('default', function()
|
||||||
|
local config = {options = {}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.non_default_global_option, nil)
|
||||||
|
end)
|
||||||
|
it('custom', function()
|
||||||
|
local config = {options = {non_default_global_option = 1}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.options.non_default_global_option, 1)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('sections', function()
|
||||||
|
it('default', function()
|
||||||
|
local config = {}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
local lualine_default_sections = {
|
||||||
|
lualine_a = {'mode'},
|
||||||
|
lualine_b = {'branch'},
|
||||||
|
lualine_c = {'filename'},
|
||||||
|
lualine_x = {'encoding', 'fileformat', 'filetype'},
|
||||||
|
lualine_y = {'progress'},
|
||||||
|
lualine_z = {'location'}
|
||||||
|
}
|
||||||
|
eq(config_module.config.sections, lualine_default_sections)
|
||||||
|
end)
|
||||||
|
it('custom', function()
|
||||||
|
local custom_sections = {
|
||||||
|
lualine_a = {{'mode', lower = true}},
|
||||||
|
lualine_b = {'branch', {'branch', lower = true}},
|
||||||
|
lualine_c = nil,
|
||||||
|
lualine_x = {}
|
||||||
|
}
|
||||||
|
local expected_sections = {
|
||||||
|
lualine_a = {{'mode', lower = true}},
|
||||||
|
lualine_b = {'branch', {'branch', lower = true}},
|
||||||
|
lualine_c = {'filename'},
|
||||||
|
lualine_x = {},
|
||||||
|
lualine_y = {'progress'},
|
||||||
|
lualine_z = {'location'}
|
||||||
|
}
|
||||||
|
local config = {sections = custom_sections}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.sections, expected_sections)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('inactive_sections', function() end)
|
||||||
|
|
||||||
|
describe('tabline', function()
|
||||||
|
it('default', function()
|
||||||
|
local config = {}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.tabline, {})
|
||||||
|
end)
|
||||||
|
it('custom', function()
|
||||||
|
local custom_sections = {
|
||||||
|
lualine_a = {{'mode', lower = true}},
|
||||||
|
lualine_b = {'branch', {'branch', lower = true}},
|
||||||
|
lualine_c = nil,
|
||||||
|
lualine_x = {}
|
||||||
|
}
|
||||||
|
local expected_sections = {
|
||||||
|
lualine_a = {{'mode', lower = true}},
|
||||||
|
lualine_b = {'branch', {'branch', lower = true}},
|
||||||
|
lualine_x = {}
|
||||||
|
}
|
||||||
|
local config = {tabline = custom_sections}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.tabline, expected_sections)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('extensions', function()
|
||||||
|
it('default', function()
|
||||||
|
local config = {options = {}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.extensions, {})
|
||||||
|
end)
|
||||||
|
it('custom', function()
|
||||||
|
local config = {extensions = {'fugitive'}}
|
||||||
|
config_module.apply_configuration(config)
|
||||||
|
eq(config_module.config.extensions, {'fugitive'})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
|
@ -1,6 +1,6 @@
|
||||||
local helpers = require 'tests.helpers'
|
local helpers = require 'tests.helpers'
|
||||||
|
|
||||||
local eq = helpers.eq
|
local eq = assert.are.same
|
||||||
local meths = helpers.meths
|
local meths = helpers.meths
|
||||||
local build_component_opts = helpers.build_component_opts
|
local build_component_opts = helpers.build_component_opts
|
||||||
|
|
||||||
|
@ -73,8 +73,8 @@ describe('Section genarator', function()
|
||||||
it('can draw', function()
|
it('can draw', function()
|
||||||
local opts = build_component_opts()
|
local opts = build_component_opts()
|
||||||
local section = {
|
local section = {
|
||||||
require('lua.lualine.components.special.function_component'):new(opts),
|
require('lualine.components.special.function_component'):new(opts),
|
||||||
require('lua.lualine.components.special.function_component'):new(opts)
|
require('lualine.components.special.function_component'):new(opts)
|
||||||
}
|
}
|
||||||
eq('%#MyHl# test %#MyHl# test ', sec.draw_section(section, '%#MyHl#'))
|
eq('%#MyHl# test %#MyHl# test ', sec.draw_section(section, '%#MyHl#'))
|
||||||
end)
|
end)
|
||||||
|
@ -87,25 +87,22 @@ describe('Section genarator', function()
|
||||||
require'lualine.highlight'.create_highlight_groups(
|
require'lualine.highlight'.create_highlight_groups(
|
||||||
require 'lualine.themes.gruvbox')
|
require 'lualine.themes.gruvbox')
|
||||||
local section = {
|
local section = {
|
||||||
require('lua.lualine.components.special.function_component'):new(opts),
|
require('lualine.components.special.function_component'):new(opts),
|
||||||
require('lua.lualine.components.special.function_component'):new(
|
require('lualine.components.special.function_component'):new(opts_colored),
|
||||||
opts_colored),
|
require('lualine.components.special.function_component'):new(opts)
|
||||||
require('lua.lualine.components.special.function_component'):new(opts)
|
|
||||||
}
|
}
|
||||||
-- Removes separator on string color
|
-- Removes separator on string color
|
||||||
eq('%#MyHl# test %#MyHl#%#MyColor# test %#MyHl# test ',
|
eq('%#MyHl# test %#MyHl#%#MyColor# test %#MyHl# test ',
|
||||||
sec.draw_section(section, '%#MyHl#'))
|
sec.draw_section(section, '%#MyHl#'))
|
||||||
section[2] =
|
section[2] = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(
|
opts_colored2)
|
||||||
opts_colored2)
|
|
||||||
local highlight_name =
|
local highlight_name =
|
||||||
'%#lualine_c_' .. section[2].options.component_name .. '_normal#'
|
'%#lualine_c_' .. section[2].options.component_name .. '_normal#'
|
||||||
-- Removes separator on color with bg
|
-- Removes separator on color with bg
|
||||||
eq('%#MyHl# test %#MyHl#' .. highlight_name .. ' test %#MyHl# test ',
|
eq('%#MyHl# test %#MyHl#' .. highlight_name .. ' test %#MyHl# test ',
|
||||||
sec.draw_section(section, '%#MyHl#'))
|
sec.draw_section(section, '%#MyHl#'))
|
||||||
section[2] =
|
section[2] = require('lualine.components.special.function_component'):new(
|
||||||
require('lua.lualine.components.special.function_component'):new(
|
opts_colored3)
|
||||||
opts_colored3)
|
|
||||||
local highlight_name2 =
|
local highlight_name2 =
|
||||||
'%#lualine_c_' .. section[2].options.component_name .. '_normal#'
|
'%#lualine_c_' .. section[2].options.component_name .. '_normal#'
|
||||||
-- Doesn't remove separator on color without bg
|
-- Doesn't remove separator on color without bg
|
Loading…
Reference in New Issue