nvim-tree / nvim-tree.lua

A file explorer tree for neovim written in lua
Other
7.22k stars 610 forks source link

Create lua callback when tree closes and opens #575

Closed ahmedkhalf closed 2 years ago

ahmedkhalf commented 3 years ago

Currently, I have this:

  vim.cmd "au FileType NvimTree lua require('core.nvimtree').on_type()"
end
--
M.on_type = function()
  local buf = vim.fn.expand "<abuf>"
  vim.cmd("au BufWinEnter <buffer=" .. buf .. "> lua require('core.nvimtree').on_open()")
  vim.cmd("au WinClosed   <buffer=" .. buf .. "> lua require('core.nvimtree').on_close()")
end
--
M.on_open = function()
  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()
  if package.loaded["bufferline.state"] then
    require("bufferline.state").set_offset(0)
  end
end

It works, but the other lunarvim devs are not willing to accept it because it features 3 autocmds #1351. It would be great if nvim-tree could provide a lua callback whenever the tree opens or closes.

kyazdani42 commented 3 years ago

why not just overriding the open/close functions ? like :NvimTreeOpen | some_other_func

ahmedkhalf commented 3 years ago

This works for opening, but for closing, there are many methods of closing, one of them is by doing :q, so unfortunately for closing, we need a callback.

kyazdani42 commented 3 years ago

not sure i can manage all the different methods for closing, it depends on how users use nvim. You could actually bind keymaps for your use case by hacking around the ex commands.

alex-courtis commented 2 years ago

See also #917

alex-courtis commented 2 years ago

Implemented. See :help nvim_tree_events_kind