Procházet zdrojové kódy

[Refactor/Bugfix] move on_config_done callbacks to relevant setup() (#1175)

* Make autopairs config consistent with others

* Fix two typos for autopairs

* Remove extranous autopairs code. Return on setup

* Remove extranous else for autopairs completion confirmation

* Move on_config_done callbacks to setup functions.

* Add on_config_done completion for builtins lacking a config function

* enables galaxyline callbacks to work properly

* Add modules for more builtins

* Finish streamline of config function in plugin setup

* Fix double use of which_key/wk

* Fix erroneous remove of functionality in autopairs completion

* consistency fixes

* Work around telescope not found at config time

* Match plugin definition of project and lualine with others

* fix: restore config callback syntax

Co-authored-by: Johan Melin <johan.melin@paradoxinteractive.com>
Co-authored-by: rebuilt <memoryman51@hotmail.com>
Co-authored-by: Luc Sinet <luc.sinet@gmail.com>
Co-authored-by: kylo252 <59826753+kylo252@users.noreply.github.com>
devtoi před 3 roky
rodič
revize
d85584d09f

+ 11 - 6
lua/core/autopairs.lua

@@ -3,6 +3,7 @@ local M = {}
 function M.config()
   lvim.builtin.autopairs = {
     active = true,
+    on_config_done = nil,
     ---@usage  map <CR> on insert mode
     map_cr = true,
     ---@usage auto insert after select function or method item
@@ -21,19 +22,19 @@ end
 M.setup = function()
   -- skip it, if you use another global object
   _G.MUtils = {}
-  local npairs = require "nvim-autopairs"
+  local autopairs = require "nvim-autopairs"
   local Rule = require "nvim-autopairs.rule"
 
   vim.g.completion_confirm_key = ""
   MUtils.completion_confirm = function()
     if vim.fn.pumvisible() ~= 0 then
       if vim.fn.complete_info()["selected"] ~= -1 then
-        return vim.fn["compe#confirm"](npairs.esc "<cr>")
+        return vim.fn["compe#confirm"](autopairs.esc "<cr>")
       else
-        return npairs.esc "<cr>"
+        return autopairs.esc "<cr>"
       end
     else
-      return npairs.autopairs_cr()
+      return autopairs.autopairs_cr()
     end
   end
 
@@ -44,7 +45,7 @@ M.setup = function()
     }
   end
 
-  npairs.setup {
+  autopairs.setup {
     check_ts = lvim.builtin.autopairs.check_ts,
     ts_config = lvim.builtin.autopairs.ts_config,
   }
@@ -55,10 +56,14 @@ M.setup = function()
 
   -- TODO: can these rules be safely added from "config.lua" ?
   -- press % => %% is only inside comment or string
-  npairs.add_rules {
+  autopairs.add_rules {
     Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
     Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
   }
+
+  if lvim.builtin.autopairs.on_config_done then
+    lvim.builtin.autopairs.on_config_done(autopairs)
+  end
 end
 
 return M

+ 5 - 0
lua/core/bufferline.lua

@@ -3,6 +3,7 @@ local M = {}
 M.config = function()
   lvim.builtin.bufferline = {
     active = true,
+    on_config_done = nil,
     keymap = {
       normal_mode = {
         ["<S-l>"] = ":BufferNext<CR>",
@@ -15,6 +16,10 @@ end
 M.setup = function()
   local keymap = require "keymappings"
   keymap.append_to_defaults(lvim.builtin.bufferline.keymap)
+
+  if lvim.builtin.bufferline.on_config_done then
+    lvim.builtin.bufferline.on_config_done()
+  end
 end
 
 return M

+ 7 - 1
lua/core/comment.lua

@@ -3,6 +3,7 @@ local M = {}
 function M.config()
   lvim.builtin.comment = {
     active = true,
+    on_config_done = nil,
     -- Linters prefer comment and line to have a space in between markers
     marker_padding = true,
     -- should comment out empty or whitespace only lines
@@ -19,7 +20,12 @@ function M.config()
 end
 
 function M.setup()
-  require("nvim_comment").setup(lvim.builtin.comment)
+  local nvim_comment = require "nvim_comment"
+
+  nvim_comment.setup(lvim.builtin.comment)
+  if lvim.builtin.comment.on_config_done then
+    lvim.builtin.comment.on_config_done(nvim_comment)
+  end
 end
 
 return M

+ 6 - 0
lua/core/compe.lua

@@ -1,7 +1,9 @@
 local M = {}
+
 M.config = function()
   lvim.builtin.compe = {
     active = true,
+    on_config_done = nil,
     autocomplete = true,
     debug = false,
     min_length = 1,
@@ -122,6 +124,10 @@ M.setup = function()
   vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", { expr = true })
   vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", { expr = true })
   vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", { expr = true })
+
+  if lvim.builtin.compe.on_config_done then
+    lvim.builtin.compe.on_config_done(compe)
+  end
 end
 
 return M

+ 6 - 0
lua/core/dap.lua

@@ -1,7 +1,9 @@
 local M = {}
+
 M.config = function()
   lvim.builtin.dap = {
     active = false,
+    on_config_done = nil,
     breakpoint = {
       text = "",
       texthl = "LspDiagnosticsSignError",
@@ -33,6 +35,10 @@ M.setup = function()
     s = { "<cmd>lua require'dap'.continue()<cr>", "Start" },
     q = { "<cmd>lua require'dap'.close()<cr>", "Quit" },
   }
+
+  if lvim.builtin.dap.on_config_done then
+    lvim.builtin.dap.on_config_done(dap)
+  end
 end
 
 -- TODO put this up there ^^^ call in ftplugin

+ 6 - 0
lua/core/dashboard.lua

@@ -1,7 +1,9 @@
 local M = {}
+
 M.config = function()
   lvim.builtin.dashboard = {
     active = false,
+    on_config_done = nil,
     search_handler = "telescope",
     disable_at_vim_enter = 0,
     session_directory = os.getenv "HOME" .. "/.cache/lvim/sessions",
@@ -91,6 +93,10 @@ M.setup = function()
       { "FileType", "dashboard", "nnoremap <silent> <buffer> q :q<CR>" },
     },
   }
+
+  if lvim.builtin.dashboard.on_config_done then
+    lvim.builtin.dashboard.on_config_done()
+  end
 end
 
 return M

+ 8 - 1
lua/core/gitsigns.lua

@@ -1,7 +1,9 @@
 local M = {}
+
 M.config = function()
   lvim.builtin.gitsigns = {
     active = true,
+    on_config_done = nil,
     opts = {
       signs = {
         add = {
@@ -51,7 +53,12 @@ M.config = function()
 end
 
 M.setup = function()
-  require("gitsigns").setup(lvim.builtin.gitsigns.opts)
+  local gitsigns = require "gitsigns"
+
+  gitsigns.setup(lvim.builtin.gitsigns.opts)
+  if lvim.builtin.gitsigns.on_config_done then
+    lvim.builtin.gitsigns.on_config_done(gitsigns)
+  end
 end
 
 return M

+ 19 - 0
lua/core/lspinstall.lua

@@ -0,0 +1,19 @@
+local M = {}
+
+M.config = function()
+  lvim.builtin.lspinstall = {
+    active = true,
+    on_config_done = nil,
+  }
+end
+
+M.setup = function()
+  local lspinstall = require "lspinstall"
+  lspinstall.setup()
+
+  if lvim.builtin.lspinstall.on_config_done then
+    lvim.builtin.lspinstall.on_config_done(lspinstall)
+  end
+end
+
+return M

+ 9 - 4
lua/core/nvimtree.lua

@@ -1,9 +1,10 @@
 local M = {}
 local Log = require "core.log"
 
-M.config = function()
+function M.config()
   lvim.builtin.nvimtree = {
     active = true,
+    on_config_done = nil,
     side = "left",
     width = 30,
     show_icons = {
@@ -48,7 +49,7 @@ M.config = function()
   }
 end
 
-M.setup = function()
+function M.setup()
   local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
   if not status_ok then
     Log:get_default().error "Failed to load nvim-tree.config"
@@ -88,15 +89,19 @@ M.setup = function()
   end
 
   vim.cmd "au WinClosed * lua require('core.nvimtree').on_close()"
+
+  if lvim.builtin.nvimtree.on_config_done then
+    lvim.builtin.nvimtree.on_config_done(nvim_tree_config)
+  end
 end
 
-M.on_open = function()
+function M.on_open()
   if package.loaded["bufferline.state"] and lvim.builtin.nvimtree.side == "left" then
     require("bufferline.state").set_offset(lvim.builtin.nvimtree.width + 1, "")
   end
 end
 
-M.on_close = function()
+function M.on_close()
   local buf = tonumber(vim.fn.expand "<abuf>")
   local ft = vim.api.nvim_buf_get_option(buf, "filetype")
   if ft == "NvimTree" and package.loaded["bufferline.state"] then

+ 11 - 4
lua/core/project.lua

@@ -1,11 +1,13 @@
 local M = {}
---
+
 function M.config()
   lvim.builtin.project = {
     ---@usage set to false to disable project.nvim.
     --- This is on by default since it's currently the expected behavior.
     active = true,
 
+    on_config_done = nil,
+
     ---@usage set to true to disable setting the current-woriking directory
     --- Manual mode doesn't automatically change your root directory, so you have
     --- the option to manually do so using `:ProjectRoot` command.
@@ -36,9 +38,14 @@ function M.config()
     datapath = CACHE_PATH,
   }
 end
---
+
 function M.setup()
-  require("project_nvim").setup(lvim.builtin.project)
+  local project = require "project_nvim"
+
+  project.setup(lvim.builtin.project)
+  if lvim.builtin.project.on_config_done then
+    lvim.builtin.project.on_config_done(project)
+  end
 end
---
+
 return M

+ 17 - 11
lua/core/telescope.lua

@@ -1,13 +1,19 @@
 local M = {}
+
 function M.config()
+  -- Define this minimal config so that it's available if telescope is not yet available.
+  lvim.builtin.telescope = {
+    ---@usage disable telescope completely [not recommeded]
+    active = true,
+    on_config_done = nil,
+  }
+
   local status_ok, actions = pcall(require, "telescope.actions")
   if not status_ok then
     return
   end
 
-  lvim.builtin.telescope = {
-    ---@usage disable telescope completely [not recommeded]
-    active = true,
+  lvim.builtin.telescope = vim.tbl_extend("force", lvim.builtin.telescope, {
     defaults = {
       prompt_prefix = " ",
       selection_caret = " ",
@@ -74,7 +80,7 @@ function M.config()
         override_file_sorter = true,
       },
     },
-  }
+  })
 end
 
 function M.find_lunarvim_files(opts)
@@ -112,15 +118,15 @@ function M.grep_lunarvim_files(opts)
 end
 
 function M.setup()
-  local status_ok, telescope = pcall(require, "telescope")
-  if not status_ok then
-    local Log = require "core.log"
-    Log:get_default().error "Failed to load telescope"
-    return
-  end
+  local telescope = require "telescope"
+
   telescope.setup(lvim.builtin.telescope)
   if lvim.builtin.project.active then
-    pcall(require("telescope").load_extension, "projects")
+    telescope.load_extension "projects"
+  end
+
+  if lvim.builtin.telescope.on_config_done then
+    lvim.builtin.telescope.on_config_done(telescope)
   end
 end
 

+ 5 - 0
lua/core/terminal.lua

@@ -3,6 +3,7 @@ local utils = require "utils"
 
 M.config = function()
   lvim.builtin["terminal"] = {
+    on_config_done = nil,
     -- size can be a number or function which is passed the current terminal
     size = 20,
     -- open_mapping = [[<c-\>]],
@@ -50,6 +51,10 @@ M.setup = function()
     require("core.terminal").add_exec(exec[1], exec[2], exec[3])
   end
   terminal.setup(lvim.builtin.terminal)
+
+  if lvim.builtin.terminal.on_config_done then
+    lvim.builtin.terminal.on_config_done(terminal)
+  end
 end
 
 M.add_exec = function(exec, keymap, name)

+ 6 - 0
lua/core/treesitter.lua

@@ -1,7 +1,9 @@
 local M = {}
 local Log = require "core.log"
+
 M.config = function()
   lvim.builtin.treesitter = {
+    on_config_done = nil,
     ensure_installed = {}, -- one of "all", "maintained" (parsers with maintainers), or a list of languages
     ignore_install = {},
     matchup = {
@@ -70,6 +72,10 @@ M.setup = function()
   end
 
   treesitter_configs.setup(lvim.builtin.treesitter)
+
+  if lvim.builtin.treesitter.on_config_done then
+    lvim.builtin.treesitter.on_config_done(treesitter_configs)
+  end
 end
 
 return M

+ 6 - 0
lua/core/which-key.lua

@@ -1,8 +1,10 @@
 local M = {}
+
 M.config = function()
   lvim.builtin.which_key = {
     ---@usage disable which-key completely [not recommeded]
     active = true,
+    on_config_done = nil,
     setup = {
       plugins = {
         marks = true, -- shows a list of your marks on ' and `
@@ -241,6 +243,10 @@ M.setup = function()
 
   which_key.register(mappings, opts)
   which_key.register(vmappings, vopts)
+
+  if lvim.builtin.which_key.on_config_done then
+    lvim.builtin.which_key.on_config_done(which_key)
+  end
 end
 
 return M

+ 3 - 18
lua/default-config.lua

@@ -15,24 +15,7 @@ lvim = {
   database = { save_location = "~/.config/lunarvim_db", auto_execute = 1 },
   keys = {},
 
-  -- TODO why do we need this?
-  builtin = {
-    lspinstall = {},
-    telescope = {},
-    compe = {},
-    autopairs = {},
-    treesitter = {},
-    nvimtree = {},
-    gitsigns = {},
-    which_key = {},
-    comment = {},
-    project = {},
-    lualine = {},
-    bufferline = {},
-    dap = {},
-    dashboard = {},
-    terminal = {},
-  },
+  builtin = {},
 
   log = {
     ---@usage can be { "trace", "debug", "info", "warn", "error", "fatal" },
@@ -1178,6 +1161,7 @@ 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()
@@ -1192,4 +1176,5 @@ require("core.project").config()
 require("core.bufferline").config()
 require("core.autopairs").config()
 require("core.comment").config()
+require("core.lspinstall").config()
 require("core.lualine").config()

+ 1 - 43
lua/plugins.lua

@@ -8,11 +8,8 @@ return {
     "kabouzeid/nvim-lspinstall",
     event = "VimEnter",
     config = function()
-      local lspinstall = require "lspinstall"
+      local lspinstall = require "core.lspinstall"
       lspinstall.setup()
-      if lvim.builtin.lspinstall.on_config_done then
-        lvim.builtin.lspinstall.on_config_done(lspinstall)
-      end
     end,
   },
 
@@ -23,9 +20,6 @@ return {
     "nvim-telescope/telescope.nvim",
     config = function()
       require("core.telescope").setup()
-      if lvim.builtin.telescope.on_config_done then
-        lvim.builtin.telescope.on_config_done(require "telescope")
-      end
     end,
     disable = not lvim.builtin.telescope.active,
   },
@@ -36,9 +30,6 @@ return {
     event = "InsertEnter",
     config = function()
       require("core.compe").setup()
-      if lvim.builtin.compe.on_config_done then
-        lvim.builtin.compe.on_config_done(require "compe")
-      end
     end,
     disable = not lvim.builtin.compe.active,
     -- wants = "vim-vsnip",
@@ -73,9 +64,6 @@ return {
     after = "nvim-compe",
     config = function()
       require("core.autopairs").setup()
-      if lvim.builtin.autopairs.on_config_done then
-        lvim.builtin.autopairs.on_config_done(require "nvim-autopairs")
-      end
     end,
     disable = not lvim.builtin.autopairs.active or not lvim.builtin.compe.active,
   },
@@ -87,9 +75,6 @@ return {
     -- run = ":TSUpdate",
     config = function()
       require("core.treesitter").setup()
-      if lvim.builtin.treesitter.on_config_done then
-        lvim.builtin.treesitter.on_config_done(require "nvim-treesitter.configs")
-      end
     end,
   },
 
@@ -101,9 +86,6 @@ return {
     -- commit = "fd7f60e242205ea9efc9649101c81a07d5f458bb",
     config = function()
       require("core.nvimtree").setup()
-      if lvim.builtin.nvimtree.on_config_done then
-        lvim.builtin.nvimtree.on_config_done(require "nvim-tree.config")
-      end
     end,
     disable = not lvim.builtin.nvimtree.active,
   },
@@ -113,9 +95,6 @@ return {
 
     config = function()
       require("core.gitsigns").setup()
-      if lvim.builtin.gitsigns.on_config_done then
-        lvim.builtin.gitsigns.on_config_done(require "gitsigns")
-      end
     end,
     event = "BufRead",
     disable = not lvim.builtin.gitsigns.active,
@@ -126,9 +105,6 @@ return {
     "folke/which-key.nvim",
     config = function()
       require("core.which-key").setup()
-      if lvim.builtin.which_key.on_config_done then
-        lvim.builtin.which_key.on_config_done(require "which-key")
-      end
     end,
     event = "BufWinEnter",
     disable = not lvim.builtin.which_key.active,
@@ -140,9 +116,6 @@ return {
     event = "BufRead",
     config = function()
       require("nvim_comment").setup()
-      if lvim.builtin.comment.on_config_done then
-        lvim.builtin.comment.on_config_done(require "nvim_comment")
-      end
     end,
     disable = not lvim.builtin.comment.active,
   },
@@ -152,9 +125,6 @@ return {
     "ahmedkhalf/project.nvim",
     config = function()
       require("core.project").setup()
-      if lvim.builtin.project.on_config_done then
-        lvim.builtin.project.on_config_done()
-      end
     end,
     disable = not lvim.builtin.project.active,
   },
@@ -177,9 +147,6 @@ return {
     "romgrk/barbar.nvim",
     config = function()
       require("core.bufferline").setup()
-      if lvim.builtin.bufferline.on_config_done then
-        lvim.builtin.bufferline.on_config_done()
-      end
     end,
     event = "BufWinEnter",
     disable = not lvim.builtin.bufferline.active,
@@ -191,9 +158,6 @@ return {
     -- event = "BufWinEnter",
     config = function()
       require("core.dap").setup()
-      if lvim.builtin.dap.on_config_done then
-        lvim.builtin.dap.on_config_done(require "dap")
-      end
     end,
     disable = not lvim.builtin.dap.active,
   },
@@ -212,9 +176,6 @@ return {
     event = "BufWinEnter",
     config = function()
       require("core.dashboard").setup()
-      if lvim.builtin.dashboard.on_config_done then
-        lvim.builtin.dashboard.on_config_done(require "dashboard")
-      end
     end,
     disable = not lvim.builtin.dashboard.active,
   },
@@ -225,9 +186,6 @@ return {
     event = "BufWinEnter",
     config = function()
       require("core.terminal").setup()
-      if lvim.builtin.terminal.on_config_done then
-        lvim.builtin.terminal.on_config_done(require "toggleterm")
-      end
     end,
     disable = not lvim.builtin.terminal.active,
   },