From dc59fa484df3c39b06aea2788741f139b6932d92 Mon Sep 17 00:00:00 2001 From: kiyan Date: Fri, 16 Apr 2021 23:07:10 +0200 Subject: [PATCH] feat: add command to prevent nvim-tree window override by another buffer --- README.md | 4 ++++ lua/nvim-tree/lib.lua | 4 ++-- lua/nvim-tree/view.lua | 28 +++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 888a3dc4..4b1865cc 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,10 @@ This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next - Mouse support - It's fast +## Tips + +- You can edit the size of the tree during runtime with `:lua require'nvim-tree.view'.View.width = 50` + ## Screenshots ![alt text](.github/screenshot.png?raw=true "kyazdani42 tree") diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 03b31a00..e9a5d9df 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -260,12 +260,12 @@ function M.open_file(mode, filename) return end - view.resize() - if vim.g.nvim_tree_quit_on_open == 1 and mode ~= 'preview' then view.close() end + view.resize() + renderer.draw(M.Tree, true) end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 84462db4..857376cc 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -11,7 +11,6 @@ M.View = { winnr = nil, width = 30, side = 'left', - auto_resize = false, winopts = { relativenumber = false, number = false, @@ -98,7 +97,6 @@ end -- set user options and create tree buffer (should never be wiped) function M.setup() - M.View.auto_resize = vim.g.nvim_tree_auto_resize or M.View.auto_resize M.View.side = vim.g.nvim_tree_side or M.View.side M.View.width = vim.g.nvim_tree_width or M.View.width @@ -123,6 +121,30 @@ function M.setup() a.nvim_buf_set_keymap(M.View.bufnr, 'n', key, cb, { noremap = true, silent = true }) end end + + vim.cmd "au! BufWinEnter * lua require'nvim-tree.view'._prevent_buffer_override()" +end + +local goto_tbl = { + right = 'h', + left = 'l', + top = 'j', + bottom = 'k', +} + +function M._prevent_buffer_override() + local curwin = a.nvim_get_current_win() + local curbuf = a.nvim_win_get_buf(curwin) + if curwin ~= M.View.winnr or curbuf == M.View.bufnr then return end + + vim.cmd("buffer "..M.View.bufnr) + + if #vim.api.nvim_list_wins() < 2 then + vim.cmd("vsplit") + else + vim.cmd("wincmd "..goto_tbl[M.View.side]) + end + vim.cmd("buffer "..curbuf) end function M.win_open() @@ -150,7 +172,7 @@ function M.focus(winnr, open_if_closed) end function M.resize() - if not M.View.auto_resize then + if not a.nvim_win_is_valid(M.View.winnr) then return end