Refactor: better handle how default options are applied

This commit is contained in:
shadmansaleh 2021-09-03 18:04:48 +06:00
parent b3c5c4403c
commit c0f53e4acd
5 changed files with 92 additions and 105 deletions

View File

@ -9,66 +9,55 @@ local modules = lualine_require.lazy_require{
local Diagnostics = lualine_require.require('lualine.component'):new() local Diagnostics = lualine_require.require('lualine.component'):new()
-- LuaFormatter off local default_symbols = {
Diagnostics.default_colors = { icons = {
error = '#e32636',
warn = '#ffdf00',
info = '#ffffff',
hint = '#d7afaf',
}
-- LuaFormatter on
-- Initializer
Diagnostics.new = function(self, options, child)
local new_diagnostics = self._parent:new(options, child or Diagnostics)
local default_symbols = new_diagnostics.options.icons_enabled and {
error = '', -- xf659 error = '', -- xf659
warn = '', -- xf529 warn = '', -- xf529
info = '', -- xf7fc info = '', -- xf7fc
hint = '' -- xf838 hint = '' -- xf838
} or {error = 'E:', warn = 'W:', info = 'I:', hint = 'H:'} },
new_diagnostics.symbols = vim.tbl_extend('force', default_symbols, no_icons = {error = 'E:', warn = 'W:', info = 'I:', hint = 'H:'},
new_diagnostics.options.symbols or {}) }
if new_diagnostics.options.sources == nil then
print('no sources for diagnostics configured')
return ''
end
if new_diagnostics.options.sections == nil then
new_diagnostics.options.sections = {'error', 'warn', 'info', 'hint'}
end
if new_diagnostics.options.colored == nil then
new_diagnostics.options.colored = true
end
if new_diagnostics.options.update_in_insert == nil then
new_diagnostics.options.update_in_insert = false
end
new_diagnostics.last_update = ''
-- apply colors
if not new_diagnostics.options.color_error then
new_diagnostics.options.color_error = {fg =
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultError', 'fg') or
modules.utils.extract_highlight_colors('DiffDelete', 'fg') or
Diagnostics.default_colors.error }
end
if not new_diagnostics.options.color_warn then
new_diagnostics.options.color_warn = {fg =
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultWarning', 'fg') or
modules.utils.extract_highlight_colors('DiffText', 'fg') or
Diagnostics.default_colors.warn }
end
if not new_diagnostics.options.color_info then
new_diagnostics.options.color_info = {fg =
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultInformation', 'fg') or
modules.utils.extract_highlight_colors('Normal', 'fg') or
Diagnostics.default_colors.info}
end
if not new_diagnostics.options.color_hint then
new_diagnostics.options.color_hint = {fg =
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultHint', 'fg') or
modules.utils.extract_highlight_colors('DiffChange', 'fg') or
Diagnostics.default_colors.hint}
end
local default_options = {
colored = true,
update_in_insert = false,
sources = nil,
sections = {'error', 'warn', 'info', 'hint'},
color_error = {
fg = modules.utils.extract_highlight_colors('LspDiagnosticsDefaultError', 'fg')
or modules.utils.extract_highlight_colors('DiffDelete', 'fg')
or '#e32636',
},
color_warn = {
fg = modules.utils.extract_highlight_colors('LspDiagnosticsDefaultWarning', 'fg')
or modules.utils.extract_highlight_colors('DiffText', 'fg')
or '#ffdf00',
},
color_info = {
fg = modules.utils.extract_highlight_colors('LspDiagnosticsDefaultInformation', 'fg')
or modules.utils.extract_highlight_colors('Normal', 'fg')
or '#ffffff',
},
color_hint = {
fg = modules.utils.extract_highlight_colors('LspDiagnosticsDefaultHint', 'fg')
or modules.utils.extract_highlight_colors('DiffChange', 'fg')
or '#d7afaf',
},
}
-- Initializer
Diagnostics.new = function(self, options, child)
-- Run super()
local new_diagnostics = self._parent:new(options, child or Diagnostics)
-- Apply default options
new_diagnostics.options =
vim.tbl_deep_extend('keep', new_diagnostics.options or {}, default_options)
-- Apply default symbols
new_diagnostics.symbols =
vim.tbl_extend('keep', new_diagnostics.options.symbols or {},
new_diagnostics.options.icons_enabled ~= false
and default_symbols.icons or default_symbols.no_icons)
-- Initialize highlight groups
if new_diagnostics.options.colored then if new_diagnostics.options.colored then
new_diagnostics.highlight_groups = { new_diagnostics.highlight_groups = {
error = modules.highlight.create_component_highlight_group( error = modules.highlight.create_component_highlight_group(
@ -86,6 +75,14 @@ Diagnostics.new = function(self, options, child)
} }
end end
-- Error out no source
if new_diagnostics.options.sources == nil then
print('no sources for diagnostics configured')
return ''
end
-- Initialize variable to store last update so we can use it in insert
-- mode for no update_in_insert
new_diagnostics.last_update = ''
return new_diagnostics return new_diagnostics
end end

View File

@ -17,42 +17,32 @@ Diff.diff_output_cache = {}
-- variable to store git_diff job -- variable to store git_diff job
Diff.diff_job = nil Diff.diff_job = nil
Diff.active_bufnr = '0' Diff.active_bufnr = '0'
-- default colors
Diff.default_colors = {
added = '#f0e130',
removed = '#90ee90',
modified = '#ff0038'
}
local diff_cache = {} -- Stores last known value of diff of a buffer local diff_cache = {} -- Stores last known value of diff of a buffer
local default_options = {
colored = true,
symbols = {added = '+', modified = '~', removed = '-'},
color_added = {
fg = modules.utils.extract_highlight_colors('DiffAdd', 'fg')
or '#f0e130',
},
color_modified = {
fg = modules.utils.extract_highlight_colors('DiffChange', 'fg')
or '#ff0038',
},
color_removed = {
fg = modules.utils.extract_highlight_colors('DiffDelete', 'fg')
or '#ff0038',
},
}
-- Initializer -- Initializer
Diff.new = function(self, options, child) Diff.new = function(self, options, child)
local new_instance = self._parent:new(options, child or Diff) local new_instance = self._parent:new(options, child or Diff)
local default_symbols = {added = '+', modified = '~', removed = '-'} new_instance.options = vim.tbl_deep_extend('keep',
new_instance.options.symbols = vim.tbl_extend('force', default_symbols, new_instance.options or {},
new_instance.options.symbols or default_options)
{})
if new_instance.options.colored == nil then
new_instance.options.colored = true
end
-- apply colors
if not new_instance.options.color_added then
new_instance.options.color_added = {fg =
modules.utils.extract_highlight_colors('DiffAdd', 'fg') or
Diff.default_colors.added}
end
if not new_instance.options.color_modified then
new_instance.options.color_modified = {fg =
modules.utils.extract_highlight_colors('DiffChange', 'fg') or
Diff.default_colors.modified}
end
if not new_instance.options.color_removed then
new_instance.options.color_removed = {fg =
modules.utils.extract_highlight_colors('DiffDelete', 'fg') or
Diff.default_colors.removed}
end
-- create highlights and save highlight_name in highlights table -- create highlights and save highlight_name in highlights table
if new_instance.options.colored then if new_instance.options.colored then
new_instance.highlights = { new_instance.highlights = {

View File

@ -2,6 +2,13 @@
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local FileName = require('lualine.component'):new() local FileName = require('lualine.component'):new()
local default_options = {
symbols = {modified = '[+]', readonly = '[-]'},
file_status = true,
path = 0,
shorting_target = 40,
}
local function count(base, pattern) local function count(base, pattern)
return select(2, string.gsub(base, pattern, '')) return select(2, string.gsub(base, pattern, ''))
end end
@ -14,20 +21,9 @@ end
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 = '[-]'} new_instance.options = vim.tbl_deep_extend('keep',
new_instance.options.symbols = vim.tbl_extend('force', default_symbols, new_instance.options or {},
new_instance.options.symbols or default_options)
{})
-- setting defaults
if new_instance.options.file_status == nil then
new_instance.options.file_status = true
end
if new_instance.options.path == nil then new_instance.options.path = 0 end
if new_instance.options.shorting_target == nil then
new_instance.options.shorting_target = 40
end
return new_instance return new_instance
end end

View File

@ -7,14 +7,16 @@ local modules = lualine_require.lazy_require{
} }
local FileType = lualine_require.require('lualine.component'):new() local FileType = lualine_require.require('lualine.component'):new()
local default_options = {
colored = true,
disable_text = false,
}
function FileType:new(options, child) function FileType:new(options, child)
local new_instance = self._parent:new(options, child or FileType) local new_instance = self._parent:new(options, child or FileType)
if new_instance.options.colored == nil then new_instance.options = vim.tbl_deep_extend('keep',
new_instance.options.colored = true new_instance.options or {},
end default_options)
if new_instance.options.disable_text == nil then
new_instance.options.disable_text = false
end
return new_instance return new_instance
end end

View File

@ -297,6 +297,8 @@ describe('Filetype component', function()
local opts = build_component_opts({ local opts = build_component_opts({
component_separators = {'', ''}, component_separators = {'', ''},
padding = 0, padding = 0,
colored=true,
disable_text = false,
}) })
assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua') assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua')
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg') assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')