neovimneovim-plugin

Lazyvim: Why do some neovim plugins need require() and setup() functions to be called and others don't?


I've been trying out LazyVim for the past couple days. I've been trying to wrap my head around how to configure plugins. One thing I'm confused by is that some modules seem to require that I do something like this before they are recognized:

  {
    "jonahgoldwastaken/copilot-status.nvim",
    lazy = false,
    dependencies = { "zbirenbaum/copilot.lua" }, -- or "zbirenbaum/copilot.lua"
    config = function()
      require("copilot_status").setup({ debug = true })
    end,
  },

However, others don't seem to need it at all:

  {
    "folke/which-key.nvim",
    opts = {
      defaults = {
        ["<leader>"] = {
          q = { "<cmd>q<cr>", "quit" },
          d = {
            function()
              require("notify").dismiss()
            end,
            "dismiss notifications",
          },
        },
        ["<leader><leader>"] = {
          w = { "<cmd>w<cr>", "write" },
        },
      },
    },
  },

I'm not sure if I'm just doing things wrong or what.


Solution

  • There are two possible reasons.

    First, another spec for the same plugin may already provided the config function, so LazyVim will call that to setup the plugin.

    As the When you import specs, you can override them by simply adding a spec for the same plugin to your local specs, adding any keys you want to override / merge.

    opts, dependencies, cmd, event, ft and keys are always merged with the parent spec. Any other property will override the property from the parent spec.

    ā€“ https://github.com/folke/lazy.nvim#%EF%B8%8F-importing-specs-config--opts

    Second, LazyVim will try to generate a default config function when no specs provide the config function for the package.

    config is executed when the plugin loads. The default implementation will automatically run require(MAIN).setup(opts). Lazy uses several heuristics to determine the plugin's MAIN module automatically based on the plugin's name. See also opts. To use the default implementation without opts set config to true.

    ā€“ https://github.com/folke/lazy.nvim#-plugin-spec