From 6d153dac1feac85b23c9ef2c0a03ddd1ea7e6f7d Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Mon, 2 Aug 2021 12:40:11 +0600 Subject: [PATCH] |BEAKING_CHNAGE| Fix :lualine disapearing on reload + Allow live updates Breaking Change: Now subsequent call to lualine.setup modifies old setup instead of configuring from scratch --- lua/lualine/config.lua | 7 +-- lua/lualine/init.lua | 6 ++- lua/tests/spec/config_spec.lua | 90 +++++++++++++++++----------------- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/lua/lualine/config.lua b/lua/lualine/config.lua index 41e24a1..0a73890 100644 --- a/lua/lualine/config.lua +++ b/lua/lualine/config.lua @@ -45,9 +45,9 @@ local function apply_configuration(config_table) 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] + vim.deepcopy(config_table[section_group_name][section_name]) if type(section) == 'table' then - for _, component in pairs(section) do + for _, component in pairs(config[section_group_name][section_name]) do if type(component) == 'table' and type(component[2]) == 'table' then local options = component[2] component[2] = nil @@ -66,6 +66,7 @@ local function apply_configuration(config_table) config.options.section_separators) config.options.component_separators = fix_separators( config.options.component_separators) + return vim.deepcopy(config) end -return {config = config, apply_configuration = apply_configuration} +return {config = vim.deepcopy(config), apply_configuration = apply_configuration} diff --git a/lua/lualine/init.lua b/lua/lualine/init.lua index 83f361c..5e33667 100644 --- a/lua/lualine/init.lua +++ b/lua/lualine/init.lua @@ -218,13 +218,15 @@ end local function setup(user_config) if user_config then - config_module.apply_configuration(user_config) + config = config_module.apply_configuration(user_config) elseif vim.g.lualine then vim.schedule(function() vim.api.nvim_err_writeln( [[Lualine: lualine will stop supporting vimscript soon, change your config to lua or wrap it around lua << EOF ... EOF]]) -- luacheck: ignore end) - config_module.apply_configuration(vim.g.lualine) + config = config_module.apply_configuration(vim.g.lualine) + else + config = config_module.apply_configuration({}) end setup_theme() loader.load_all(config) diff --git a/lua/tests/spec/config_spec.lua b/lua/tests/spec/config_spec.lua index 507fdb5..4050ce3 100644 --- a/lua/tests/spec/config_spec.lua +++ b/lua/tests/spec/config_spec.lua @@ -6,36 +6,36 @@ describe('config parsing', function() describe('options', function() describe('icons_enabled', function() it('default', function() - config_module.apply_configuration({}) - eq(config_module.config.options.icons_enabled, true) + local config = config_module.apply_configuration({}) + eq(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) + config = config_module.apply_configuration(config) + eq(config.options.icons_enabled, false) end) end) describe('theme', function() it('default', function() - config_module.apply_configuration({}) - eq(config_module.config.options.theme, 'gruvbox') + local config = config_module.apply_configuration({}) + eq(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 = config_module.apply_configuration(config) + eq(config.options.theme, 'nord') config = {options = {theme = {}}} - config_module.apply_configuration(config) - eq(config_module.config.options.theme, {}) + config = config_module.apply_configuration(config) + eq(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, {'', ''}) + local config = config_module.apply_configuration({}) + eq(config.options.component_separators, {'', ''}) + eq(config.options.section_separators, {'', ''}) end) it('double separators', function() local config = { @@ -44,9 +44,9 @@ describe('config parsing', function() 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'}) + config = config_module.apply_configuration(config) + eq(config.options.component_separators, {'a', 'b'}) + eq(config.options.section_separators, {'c', 'd'}) end) describe('single separator', function() @@ -54,51 +54,51 @@ describe('config parsing', 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'}) + config = config_module.apply_configuration(config) + eq(config.options.component_separators, {'a', 'a'}) + eq(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'}) + config = config_module.apply_configuration(config) + eq(config.options.component_separators, {'a', 'a'}) + eq(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, {}) + config = config_module.apply_configuration(config) + eq(config.options.component_separators, {}) + eq(config.options.section_separators, {}) end) end) describe('disabled filetypes', function() it('default', function() - config_module.apply_configuration({}) - eq(config_module.config.options.disabled_filetypes, {}) + local config = config_module.apply_configuration({}) + eq(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'}) + config = config_module.apply_configuration(config) + eq(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) + config = config_module.apply_configuration(config) + eq(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) + config = config_module.apply_configuration(config) + eq(config.options.non_default_global_option, 1) end) end) end) @@ -106,7 +106,7 @@ describe('config parsing', function() describe('sections', function() it('default', function() local config = {} - config_module.apply_configuration(config) + config = config_module.apply_configuration(config) local lualine_default_sections = { lualine_a = {'mode'}, lualine_b = {'branch'}, @@ -115,7 +115,7 @@ describe('config parsing', function() lualine_y = {'progress'}, lualine_z = {'location'} } - eq(config_module.config.sections, lualine_default_sections) + eq(config.sections, lualine_default_sections) end) it('custom', function() local custom_sections = { @@ -133,8 +133,8 @@ describe('config parsing', function() lualine_z = {'location'} } local config = {sections = custom_sections} - config_module.apply_configuration(config) - eq(config_module.config.sections, expected_sections) + config = config_module.apply_configuration(config) + eq(config.sections, expected_sections) end) end) @@ -143,8 +143,8 @@ describe('config parsing', function() describe('tabline', function() it('default', function() local config = {} - config_module.apply_configuration(config) - eq(config_module.config.tabline, {}) + config = config_module.apply_configuration(config) + eq(config.tabline, {}) end) it('custom', function() local custom_sections = { @@ -159,21 +159,21 @@ describe('config parsing', function() lualine_x = {} } local config = {tabline = custom_sections} - config_module.apply_configuration(config) - eq(config_module.config.tabline, expected_sections) + config = config_module.apply_configuration(config) + eq(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, {}) + config = config_module.apply_configuration(config) + eq(config.extensions, {}) end) it('custom', function() local config = {extensions = {'fugitive'}} - config_module.apply_configuration(config) - eq(config_module.config.extensions, {'fugitive'}) + config = config_module.apply_configuration(config) + eq(config.extensions, {'fugitive'}) end) end) end)