Skip to content

fix: prevent quit_on_open from opening file in the same window #1637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,16 @@ local function setup_autocommands(opts)
-- reset highlights when colorscheme is changed
create_nvim_tree_autocmd("ColorScheme", { callback = M.reset_highlight })

-- prevent new opened file from opening in the same window as nvim-tree
create_nvim_tree_autocmd("BufWipeout", {
pattern = "NvimTree_*",
callback = function()
if vim.bo.filetype == "NvimTree" then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

view._prevent_buffer_override()
end
end,
})

local has_watchers = opts.filesystem_watchers.enable

if opts.auto_reload_on_write and not has_watchers then
Expand Down Expand Up @@ -369,12 +379,6 @@ local function setup_autocommands(opts)
})
end

if not opts.actions.open_file.quit_on_open then
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view._prevent_buffer_override })
else
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view.abandon_current_window })
end

if opts.hijack_directories.enable then
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
end
Expand Down
11 changes: 10 additions & 1 deletion lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,10 @@ function M._prevent_buffer_override()
-- Otherwise the curwin/curbuf would match the view buffer and the view window.
vim.schedule(function()
local curwin = a.nvim_get_current_win()
local curwinconfig = a.nvim_win_get_config(curwin)
local curbuf = a.nvim_win_get_buf(curwin)
local bufname = a.nvim_buf_get_name(curbuf)

if not bufname:match "NvimTree" then
for i, tabpage in ipairs(M.View.tabpages) do
if tabpage.winnr == view_winnr then
Expand All @@ -420,7 +422,14 @@ function M._prevent_buffer_override()
M.open { focus_tree = false }
require("nvim-tree.renderer").draw()
pcall(a.nvim_win_close, curwin, { force = true })
require("nvim-tree.actions.node.open-file").fn("edit", bufname)

-- to handle opening a file using :e when nvim-tree is on floating mode
-- falling back to the current window instead of creating a new one
if curwinconfig.relative ~= "" then
require("nvim-tree.actions.node.open-file").fn("edit_in_place", bufname)
else
require("nvim-tree.actions.node.open-file").fn("edit", bufname)
end
end)
end

Expand Down