Преглед изворни кода

[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
Luc Sinet пре 3 година
родитељ
комит
00b895d9e9

+ 3 - 26
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 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
+local config = require "config"
+config:init()
+config:load()
 
-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"

+ 0 - 18
lua/default-config.lua → 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()

+ 44 - 0
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

+ 1 - 3
lua/settings.lua → 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

+ 2 - 1
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",

+ 29 - 0
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

+ 2 - 2
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,
       },
     },
 

+ 4 - 2
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, "│")),
     })

+ 4 - 2
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"