Skip to content

Commit b01e7be

Browse files
fix(#1628): quit_on_open e: do not open in the tree's window (#1637)
* fix: prevent quit_on_open from opening in same window * chore: remove condition for quit_on_open * fix: prevent opening file in a new window on floting nvim-tree * stylua Co-authored-by: Alexander Courtis <[email protected]>
1 parent c66cbdf commit b01e7be

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lua/nvim-tree.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ local function setup_autocommands(opts)
335335
-- reset highlights when colorscheme is changed
336336
create_nvim_tree_autocmd("ColorScheme", { callback = M.reset_highlight })
337337

338+
-- prevent new opened file from opening in the same window as nvim-tree
339+
create_nvim_tree_autocmd("BufWipeout", {
340+
pattern = "NvimTree_*",
341+
callback = function()
342+
if vim.bo.filetype == "NvimTree" then
343+
view._prevent_buffer_override()
344+
end
345+
end,
346+
})
347+
338348
local has_watchers = opts.filesystem_watchers.enable
339349

340350
if opts.auto_reload_on_write and not has_watchers then
@@ -369,12 +379,6 @@ local function setup_autocommands(opts)
369379
})
370380
end
371381

372-
if not opts.actions.open_file.quit_on_open then
373-
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view._prevent_buffer_override })
374-
else
375-
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view.abandon_current_window })
376-
end
377-
378382
if opts.hijack_directories.enable then
379383
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
380384
end

lua/nvim-tree/view.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ function M._prevent_buffer_override()
399399
-- Otherwise the curwin/curbuf would match the view buffer and the view window.
400400
vim.schedule(function()
401401
local curwin = a.nvim_get_current_win()
402+
local curwinconfig = a.nvim_win_get_config(curwin)
402403
local curbuf = a.nvim_win_get_buf(curwin)
403404
local bufname = a.nvim_buf_get_name(curbuf)
405+
404406
if not bufname:match "NvimTree" then
405407
for i, tabpage in ipairs(M.View.tabpages) do
406408
if tabpage.winnr == view_winnr then
@@ -420,7 +422,14 @@ function M._prevent_buffer_override()
420422
M.open { focus_tree = false }
421423
require("nvim-tree.renderer").draw()
422424
pcall(a.nvim_win_close, curwin, { force = true })
423-
require("nvim-tree.actions.node.open-file").fn("edit", bufname)
425+
426+
-- to handle opening a file using :e when nvim-tree is on floating mode
427+
-- falling back to the current window instead of creating a new one
428+
if curwinconfig.relative ~= "" then
429+
require("nvim-tree.actions.node.open-file").fn("edit_in_place", bufname)
430+
else
431+
require("nvim-tree.actions.node.open-file").fn("edit", bufname)
432+
end
424433
end)
425434
end
426435

0 commit comments

Comments
 (0)