Skip to content

Commit 36afa32

Browse files
committed
chore: move _prevent_buffer_override in entry file
1 parent c226eaf commit 36afa32

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

lua/nvim-tree.lua

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,45 @@ function M.open()
4343
end
4444
end
4545

46+
local move_cmd = {
47+
right = 'h',
48+
left = 'l',
49+
top = 'j',
50+
bottom = 'k',
51+
}
52+
53+
function M._prevent_buffer_override()
54+
vim.schedule(function()
55+
local curwin = api.nvim_get_current_win()
56+
local curbuf = api.nvim_win_get_buf(curwin)
57+
58+
if curwin ~= view.get_winnr() or curbuf == view.View.bufnr then
59+
return
60+
end
61+
62+
if view.is_buf_valid(view.View.bufnr) then
63+
-- pcall necessary to avoid erroring with `mark not set` although no mark are set
64+
-- this avoid other issues
65+
pcall(api.nvim_win_set_buf, view.get_winnr(), view.View.bufnr)
66+
end
67+
68+
local bufname = api.nvim_buf_get_name(curbuf)
69+
local isdir = vim.fn.isdirectory(bufname) == 1
70+
if isdir or not bufname or bufname == "" then
71+
return
72+
end
73+
74+
if #vim.api.nvim_list_wins() < 2 then
75+
local cmd = view.is_vertical() and "vsplit" or "split"
76+
vim.cmd(cmd)
77+
else
78+
vim.cmd("wincmd "..move_cmd[view.View.side])
79+
end
80+
vim.cmd("buffer "..curbuf)
81+
view.resize()
82+
end)
83+
end
84+
4685
function M.tab_change()
4786
vim.schedule(function()
4887
if not view.win_open() and view.win_open({ any_tabpage = true }) then
@@ -303,7 +342,7 @@ local function setup_autocommands(opts)
303342

304343
vim.cmd "au BufUnload NvimTree lua require'nvim-tree.view'.View.tabpages = {}"
305344
if not opts.actions.open_file.quit_on_open then
306-
vim.cmd "au BufWinEnter,BufWinLeave * lua require'nvim-tree.view'._prevent_buffer_override()"
345+
vim.cmd "au BufWinEnter,BufWinLeave * lua require'nvim-tree'._prevent_buffer_override()"
307346
end
308347
vim.cmd "au BufEnter,BufNewFile * lua require'nvim-tree'.open_on_directory()"
309348

lua/nvim-tree/view.lua

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -85,45 +85,6 @@ function M.setup(opts)
8585
M.View.winopts.signcolumn = options.signcolumn
8686
end
8787

88-
local move_cmd = {
89-
right = 'h',
90-
left = 'l',
91-
top = 'j',
92-
bottom = 'k',
93-
}
94-
95-
function M._prevent_buffer_override()
96-
vim.schedule(function()
97-
local curwin = a.nvim_get_current_win()
98-
local curbuf = a.nvim_win_get_buf(curwin)
99-
100-
if curwin ~= M.get_winnr() or curbuf == M.View.bufnr then
101-
return
102-
end
103-
104-
if a.nvim_buf_is_loaded(M.View.bufnr) and a.nvim_buf_is_valid(M.View.bufnr) then
105-
-- pcall necessary to avoid erroring with `mark not set` although no mark are set
106-
-- this avoid other issues
107-
pcall(vim.api.nvim_win_set_buf, M.get_winnr(), M.View.bufnr)
108-
end
109-
110-
local bufname = a.nvim_buf_get_name(curbuf)
111-
local isdir = vim.fn.isdirectory(bufname) == 1
112-
if isdir or not bufname or bufname == "" then
113-
return
114-
end
115-
116-
if #vim.api.nvim_list_wins() < 2 then
117-
local cmd = M.is_vertical() and "vsplit" or "split"
118-
vim.cmd(cmd)
119-
else
120-
vim.cmd("wincmd "..move_cmd[M.View.side])
121-
end
122-
vim.cmd("buffer "..curbuf)
123-
M.resize()
124-
end)
125-
end
126-
12788
function M.win_open(opts)
12889
if opts and opts.any_tabpage then
12990
for _, v in pairs(M.View.tabpages) do
@@ -221,14 +182,14 @@ local function open_window()
221182
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or {help = false}, {winnr = winnr})
222183
end
223184

224-
local function is_buf_valid(bufnr)
185+
function M.is_buf_valid(bufnr)
225186
return bufnr and a.nvim_buf_is_valid(bufnr) and a.nvim_buf_is_loaded(bufnr)
226187
end
227188

228189
function M.open(options)
229190
M.View.last_focused_winnr = a.nvim_get_current_win()
230191
local should_redraw = false
231-
if not is_buf_valid(M.View.bufnr) then
192+
if not M.is_buf_valid(M.View.bufnr) then
232193
should_redraw = true
233194
M.create_buffer()
234195
end

0 commit comments

Comments
 (0)