Skip to content

Commit dc59fa4

Browse files
committed
feat: add command to prevent nvim-tree window override by another buffer
1 parent 4ee45d9 commit dc59fa4

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next
186186
- Mouse support
187187
- It's fast
188188

189+
## Tips
190+
191+
- You can edit the size of the tree during runtime with `:lua require'nvim-tree.view'.View.width = 50`
192+
189193
## Screenshots
190194

191195
![alt text](.github/screenshot.png?raw=true "kyazdani42 tree")

lua/nvim-tree/lib.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ function M.open_file(mode, filename)
260260
return
261261
end
262262

263-
view.resize()
264-
265263
if vim.g.nvim_tree_quit_on_open == 1 and mode ~= 'preview' then
266264
view.close()
267265
end
268266

267+
view.resize()
268+
269269
renderer.draw(M.Tree, true)
270270
end
271271

lua/nvim-tree/view.lua

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ M.View = {
1111
winnr = nil,
1212
width = 30,
1313
side = 'left',
14-
auto_resize = false,
1514
winopts = {
1615
relativenumber = false,
1716
number = false,
@@ -98,7 +97,6 @@ end
9897

9998
-- set user options and create tree buffer (should never be wiped)
10099
function M.setup()
101-
M.View.auto_resize = vim.g.nvim_tree_auto_resize or M.View.auto_resize
102100
M.View.side = vim.g.nvim_tree_side or M.View.side
103101
M.View.width = vim.g.nvim_tree_width or M.View.width
104102

@@ -123,6 +121,30 @@ function M.setup()
123121
a.nvim_buf_set_keymap(M.View.bufnr, 'n', key, cb, { noremap = true, silent = true })
124122
end
125123
end
124+
125+
vim.cmd "au! BufWinEnter * lua require'nvim-tree.view'._prevent_buffer_override()"
126+
end
127+
128+
local goto_tbl = {
129+
right = 'h',
130+
left = 'l',
131+
top = 'j',
132+
bottom = 'k',
133+
}
134+
135+
function M._prevent_buffer_override()
136+
local curwin = a.nvim_get_current_win()
137+
local curbuf = a.nvim_win_get_buf(curwin)
138+
if curwin ~= M.View.winnr or curbuf == M.View.bufnr then return end
139+
140+
vim.cmd("buffer "..M.View.bufnr)
141+
142+
if #vim.api.nvim_list_wins() < 2 then
143+
vim.cmd("vsplit")
144+
else
145+
vim.cmd("wincmd "..goto_tbl[M.View.side])
146+
end
147+
vim.cmd("buffer "..curbuf)
126148
end
127149

128150
function M.win_open()
@@ -150,7 +172,7 @@ function M.focus(winnr, open_if_closed)
150172
end
151173

152174
function M.resize()
153-
if not M.View.auto_resize then
175+
if not a.nvim_win_is_valid(M.View.winnr) then
154176
return
155177
end
156178

0 commit comments

Comments
 (0)