feat: add command to prevent nvim-tree window override by another buffer

This commit is contained in:
kiyan 2021-04-16 23:07:10 +02:00
parent 4ee45d9261
commit dc59fa484d
3 changed files with 31 additions and 5 deletions

View File

@ -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")

View File

@ -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

View File

@ -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