From 00b895d9e9577f084cf577a07f9d6d6e1f7a4cac Mon Sep 17 00:00:00 2001 From: Luc Sinet Date: Wed, 25 Aug 2021 07:47:48 +0200 Subject: [PATCH] [Feature] Encapsulate config logic (#1338) * Define core/builtins, streamline status_color interface * Encapsulate configuration in its own module * Add fallback to lv-config.lua * Rectify settings loading order to allow overriding vim options * Move default-config into config/ module * replace uv.fs_stat with utils.is_file --- init.lua | 29 ++---------- .../defaults.lua} | 18 -------- lua/config/init.lua | 44 +++++++++++++++++++ lua/{ => config}/settings.lua | 4 +- lua/core/autocmds.lua | 3 +- lua/core/builtins/init.lua | 29 ++++++++++++ lua/core/dashboard.lua | 4 +- lua/core/info.lua | 6 ++- lua/utils/init.lua | 6 ++- 9 files changed, 89 insertions(+), 54 deletions(-) rename lua/{default-config.lua => config/defaults.lua} (97%) create mode 100644 lua/config/init.lua rename lua/{ => config}/settings.lua (98%) create mode 100644 lua/core/builtins/init.lua diff --git a/init.lua b/init.lua index 1d6cdc0f..9724c2cc 100644 --- a/init.lua +++ b/init.lua @@ -17,34 +17,11 @@ vim.opt.rtp:append(home_dir .. "/.local/share/lunarvim/site/after") vim.cmd [[let &packpath = &runtimepath]] -- }}} -local function file_exists(name) - local f = io.open(name, "r") - if f ~= nil then - io.close(f) - return true - else - return false - end -end +local config = require "config" +config:init() +config:load() -local lvim_path = os.getenv "HOME" .. "/.config/lvim/" -USER_CONFIG_PATH = lvim_path .. "config.lua" -local config_exist = file_exists(USER_CONFIG_PATH) -if not config_exist then - USER_CONFIG_PATH = lvim_path .. "lv-config.lua" - print "Rename ~/.config/lvim/lv-config.lua to config.lua" -end - -require "default-config" local autocmds = require "core.autocmds" -require("settings").load_options() - -local status_ok, error = pcall(vim.cmd, "luafile " .. USER_CONFIG_PATH) -if not status_ok then - print("something is wrong with your " .. USER_CONFIG_PATH) - print(error) -end -require("settings").load_commands() autocmds.define_augroups(lvim.autocommands) local plugins = require "plugins" diff --git a/lua/default-config.lua b/lua/config/defaults.lua similarity index 97% rename from lua/default-config.lua rename to lua/config/defaults.lua index 9d84efaa..5dc98698 100644 --- a/lua/default-config.lua +++ b/lua/config/defaults.lua @@ -1160,21 +1160,3 @@ lvim.lang = { }, }, } - --- NOTE: which-key should be first because it defines lvim.builtin.which_key.mappings -require("keymappings").config() -require("core.which-key").config() -require("core.gitsigns").config() -require("core.compe").config() -require("core.dashboard").config() -require("core.dap").config() -require("core.terminal").config() -require("core.telescope").config() -require("core.treesitter").config() -require("core.nvimtree").config() -require("core.project").config() -require("core.bufferline").config() -require("core.autopairs").config() -require("core.comment").config() -require("core.lspinstall").config() -require("core.lualine").config() diff --git a/lua/config/init.lua b/lua/config/init.lua new file mode 100644 index 00000000..9833fe6b --- /dev/null +++ b/lua/config/init.lua @@ -0,0 +1,44 @@ +local M = { + path = string.format("%s/.config/lvim/config.lua", os.getenv "HOME"), +} + +--- Initialize lvim default configuration +-- Define lvim global variable +function M:init() + local utils = require "utils" + + require "config.defaults" + + local builtins = require "core.builtins" + builtins.config(self) + + local settings = require "config.settings" + settings.load_options() + + -- Fallback config.lua to lv-config.lua + if not utils.is_file(self.path) then + local lv_config = self.path:gsub("config.lua$", "lv-config.lua") + print(self.path, "not found, falling back to", lv_config) + + self.path = lv_config + end +end + +--- Override the configuration with a user provided one +-- @param config_path The path to the configuration overrides +function M:load(config_path) + config_path = config_path or self.path + local ok, err = pcall(vim.cmd, "luafile " .. config_path) + if not ok then + print("Invalid configuration", config_path) + print(err) + return + end + + self.path = config_path + + local settings = require "config.settings" + settings.load_commands() +end + +return M diff --git a/lua/settings.lua b/lua/config/settings.lua similarity index 98% rename from lua/settings.lua rename to lua/config/settings.lua index d96c1338..ba71a922 100644 --- a/lua/settings.lua +++ b/lua/config/settings.lua @@ -1,8 +1,6 @@ local M = {} M.load_options = function() - local opt = vim.opt - local default_options = { backup = false, -- creates a backup file clipboard = "unnamedplus", -- allows neovim to access the system clipboard @@ -51,7 +49,7 @@ M.load_options = function() --- SETTINGS --- - opt.shortmess:append "c" + vim.opt.shortmess:append "c" for k, v in pairs(default_options) do vim.opt[k] = v diff --git a/lua/core/autocmds.lua b/lua/core/autocmds.lua index 91278544..91ec70b5 100644 --- a/lua/core/autocmds.lua +++ b/lua/core/autocmds.lua @@ -1,4 +1,5 @@ local autocommands = {} +local config = require "config" lvim.autocommands = { _general_settings = { @@ -32,7 +33,7 @@ lvim.autocommands = { "*", "setlocal formatoptions-=c formatoptions-=r formatoptions-=o", }, - { "BufWritePost", USER_CONFIG_PATH, "lua require('utils').reload_lv_config()" }, + { "BufWritePost", config.path, "lua require('utils').reload_lv_config()" }, { "FileType", "qf", diff --git a/lua/core/builtins/init.lua b/lua/core/builtins/init.lua new file mode 100644 index 00000000..32f96af5 --- /dev/null +++ b/lua/core/builtins/init.lua @@ -0,0 +1,29 @@ +local M = {} + +local builtins = { + "keymappings", + "core.which-key", + "core.gitsigns", + "core.compe", + "core.dashboard", + "core.dap", + "core.terminal", + "core.telescope", + "core.treesitter", + "core.nvimtree", + "core.project", + "core.bufferline", + "core.autopairs", + "core.comment", + "core.lspinstall", + "core.lualine", +} + +function M.config(config) + for _, builtin_path in ipairs(builtins) do + local builtin = require(builtin_path) + builtin.config(config) + end +end + +return M diff --git a/lua/core/dashboard.lua b/lua/core/dashboard.lua index c76d55c9..ac6ee013 100644 --- a/lua/core/dashboard.lua +++ b/lua/core/dashboard.lua @@ -1,6 +1,6 @@ local M = {} -M.config = function() +M.config = function(config) lvim.builtin.dashboard = { active = false, on_config_done = nil, @@ -47,7 +47,7 @@ M.config = function() }, e = { description = { " Configuration " }, - command = ":e " .. USER_CONFIG_PATH, + command = ":e " .. config.path, }, }, diff --git a/lua/core/info.lua b/lua/core/info.lua index d9b348b5..67e45d1c 100644 --- a/lua/core/info.lua +++ b/lua/core/info.lua @@ -16,6 +16,7 @@ local function str_list(list) end local function get_formatter_suggestion_msg(ft) + local config = require "config" local null_formatters = require "lsp.null-ls.formatters" local supported_formatters = null_formatters.list_available(ft) local section = { @@ -27,7 +28,7 @@ local function get_formatter_suggestion_msg(ft) if not vim.tbl_isempty(supported_formatters) then vim.list_extend(section, { "* Configured formatter needs to be installed and executable.", - fmt("* Enable installed formatter(s) with following config in %s", USER_CONFIG_PATH), + fmt("* Enable installed formatter(s) with following config in %s", config.path), "", fmt(" lvim.lang.%s.formatters = { { exe = '%s' } }", ft, table.concat(supported_formatters, "│")), }) @@ -37,6 +38,7 @@ local function get_formatter_suggestion_msg(ft) end local function get_linter_suggestion_msg(ft) + local config = require "config" local null_linters = require "lsp.null-ls.linters" local supported_linters = null_linters.list_available(ft) local section = { @@ -48,7 +50,7 @@ local function get_linter_suggestion_msg(ft) if not vim.tbl_isempty(supported_linters) then vim.list_extend(section, { "* Configured linter needs to be installed and executable.", - fmt("* Enable installed linter(s) with following config in %s", USER_CONFIG_PATH), + fmt("* Enable installed linter(s) with following config in %s", config.path), "", fmt(" lvim.lang.%s.linters = { { exe = '%s' } }", ft, table.concat(supported_linters, "│")), }) diff --git a/lua/utils/init.lua b/lua/utils/init.lua index 446a1509..80a2dbe6 100644 --- a/lua/utils/init.lua +++ b/lua/utils/init.lua @@ -89,8 +89,10 @@ end function utils.reload_lv_config() require("core.lualine").config() - vim.cmd "source ~/.local/share/lunarvim/lvim/lua/settings.lua" - vim.cmd("source " .. USER_CONFIG_PATH) + + local config = require "config" + config:load() + require("keymappings").setup() -- this should be done before loading the plugins vim.cmd "source ~/.local/share/lunarvim/lvim/lua/plugins.lua" local plugins = require "plugins"