lazy.nvim icon indicating copy to clipboard operation
lazy.nvim copied to clipboard

feature: load plugin when root pattern detected

Open hoofcushion opened this issue 8 months ago • 3 comments

Did you check the docs?

  • [x] I have read all the lazy.nvim docs

Is your feature request related to a problem? Please describe.

Currently, lazy.nvim uses events, filetypes, commands, and keymaps to load plugins. However, project-specific plugins don't fit well with these existing handlers. For example:

  • Plugins like lewis6991/gitsigns.nvim should load when a .git directory is present
  • zk-org/zk-nvim should load when a .zk directory exists

Describe the solution you'd like

A new handler to detect root pattern when DirChanged or BufRead, and use vim.fs.root(0,patterns). pesudo code:

lazy.setup({
 {"lewis6991/gitsigns.nvim",                    rootpattern={".git"}},
})

Describe alternatives you've considered

pass

Additional context

No response

hoofcushion avatar Apr 18 '25 09:04 hoofcushion

You can do that with cond property already. Something like

cond = function()
      return vim.uv.fs_stat(vim.loop.cwd() .. "/.git") or vim.fn.finddir(".git", ";") ~= ""
    end,

Just specify the cond in each plugin spec that you want.

dpetka2001 avatar Apr 18 '25 09:04 dpetka2001

root pattern has to be check on each dir change or buf read, not startup stage, and a false cond is gonna disable the plugin, was not what it meant

hoofcushion avatar Apr 18 '25 09:04 hoofcushion

This can be a workaround:

vim.api.nvim_create_autocmd("BufRead",{
 callback = function()
  if vim.fs.root(0,".git")~=nil then
   vim.api.nvim_exec_autocmds("User",{data="RootPattern: .git"})
  end
 end
})

lazy.setup({
 {"lewis6991/gitsigns.nvim",                    event="User RootPattern: .git"},
})

hoofcushion avatar Apr 18 '25 09:04 hoofcushion

not interested in adding this

folke avatar May 12 '25 13:05 folke