From c0f53e4acd1f437e27493f9e55c8f2a42dfdefc9 Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Fri, 3 Sep 2021 18:04:48 +0600 Subject: [PATCH] Refactor: better handle how default options are applied --- lua/lualine/components/diagnostics.lua | 107 ++++++++++++------------- lua/lualine/components/diff.lua | 50 +++++------- lua/lualine/components/filename.lua | 24 +++--- lua/lualine/components/filetype.lua | 14 ++-- lua/tests/spec/component_spec.lua | 2 + 5 files changed, 92 insertions(+), 105 deletions(-) diff --git a/lua/lualine/components/diagnostics.lua b/lua/lualine/components/diagnostics.lua index 228eac8..1fa5826 100644 --- a/lua/lualine/components/diagnostics.lua +++ b/lua/lualine/components/diagnostics.lua @@ -9,66 +9,55 @@ local modules = lualine_require.lazy_require{ local Diagnostics = lualine_require.require('lualine.component'):new() --- LuaFormatter off -Diagnostics.default_colors = { - 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 { +local default_symbols = { + icons = { error = ' ', -- xf659 warn = ' ', -- xf529 info = ' ', -- xf7fc hint = ' ' -- xf838 - } or {error = 'E:', warn = 'W:', info = 'I:', hint = 'H:'} - new_diagnostics.symbols = vim.tbl_extend('force', default_symbols, - 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 + }, + no_icons = {error = 'E:', warn = 'W:', info = 'I:', hint = 'H:'}, +} +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 new_diagnostics.highlight_groups = { error = modules.highlight.create_component_highlight_group( @@ -86,6 +75,14 @@ Diagnostics.new = function(self, options, child) } 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 end diff --git a/lua/lualine/components/diff.lua b/lua/lualine/components/diff.lua index 8a946d3..9337caa 100644 --- a/lua/lualine/components/diff.lua +++ b/lua/lualine/components/diff.lua @@ -17,42 +17,32 @@ Diff.diff_output_cache = {} -- variable to store git_diff job Diff.diff_job = nil 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 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 Diff.new = function(self, options, child) local new_instance = self._parent:new(options, child or Diff) - local default_symbols = {added = '+', modified = '~', removed = '-'} - new_instance.options.symbols = vim.tbl_extend('force', default_symbols, - new_instance.options.symbols or - {}) - 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 - + new_instance.options = vim.tbl_deep_extend('keep', + new_instance.options or {}, + default_options) -- create highlights and save highlight_name in highlights table if new_instance.options.colored then new_instance.highlights = { diff --git a/lua/lualine/components/filename.lua b/lua/lualine/components/filename.lua index ad74a78..64410bd 100644 --- a/lua/lualine/components/filename.lua +++ b/lua/lualine/components/filename.lua @@ -2,6 +2,13 @@ -- MIT license, see LICENSE for more details. 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) return select(2, string.gsub(base, pattern, '')) end @@ -14,20 +21,9 @@ end FileName.new = function(self, options, child) local new_instance = self._parent:new(options, child or FileName) - local default_symbols = {modified = '[+]', readonly = '[-]'} - new_instance.options.symbols = vim.tbl_extend('force', default_symbols, - new_instance.options.symbols or - {}) - - -- 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 - + new_instance.options = vim.tbl_deep_extend('keep', + new_instance.options or {}, + default_options) return new_instance end diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua index 0e9f648..31f7f65 100644 --- a/lua/lualine/components/filetype.lua +++ b/lua/lualine/components/filetype.lua @@ -7,14 +7,16 @@ local modules = lualine_require.lazy_require{ } local FileType = lualine_require.require('lualine.component'):new() +local default_options = { + colored = true, + disable_text = false, +} + function FileType:new(options, child) local new_instance = self._parent:new(options, child or FileType) - if new_instance.options.colored == nil then - new_instance.options.colored = true - end - if new_instance.options.disable_text == nil then - new_instance.options.disable_text = false - end + new_instance.options = vim.tbl_deep_extend('keep', + new_instance.options or {}, + default_options) return new_instance end diff --git a/lua/tests/spec/component_spec.lua b/lua/tests/spec/component_spec.lua index 9394b36..4c93939 100644 --- a/lua/tests/spec/component_spec.lua +++ b/lua/tests/spec/component_spec.lua @@ -297,6 +297,8 @@ describe('Filetype component', function() local opts = build_component_opts({ component_separators = {'', ''}, padding = 0, + colored=true, + disable_text = false, }) assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua') assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')