Skip to content

fix: error when deleting opened file from floating window #2503

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
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
13 changes: 8 additions & 5 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ local M = {
}

local function close_windows(windows)
if view.View.float.enable and #vim.api.nvim_list_wins() == 1 then
-- Prevent from closing when the win count equals 1 or 2,
-- where the win to remove could be the last opened.
-- For details see #2503.
if view.View.float.enable and #vim.api.nvim_list_wins() < 3 then
Copy link
Member

Choose a reason for hiding this comment

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

Please leave a comment explaining the magic number 3

Copy link
Collaborator Author

@geril2207 geril2207 Oct 31, 2023

Choose a reason for hiding this comment

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

Initial problem was in case where list of wins is 2 (float tree, and opened buffer), so when trying to delete opened buffer window, vim throws an error cannot close last window. Is there a case where list can be 1? If it possible we save previous condition and add if not 2(< 3 just looks more compact) which prevents from closing the last window.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Additionally we could check if the window count is 2, and there is no floating tree, in which case we could close the window. But even with quit_on_focus_loss, list_wins length would be 2 including floating tree.

Copy link
Member

Choose a reason for hiding this comment

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

Safety first, thank you.

return
end

Expand All @@ -24,15 +27,15 @@ local function clear_buffer(absolute_path)
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
for _, buf in pairs(bufs) do
if buf.name == absolute_path then
local tree_winnr = vim.api.nvim_get_current_win()
if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then
local winnr = vim.api.nvim_get_current_win()
vim.api.nvim_set_current_win(buf.windows[1])
vim.cmd ":bn"
if not view.View.float.enable then
vim.api.nvim_set_current_win(winnr)
end
end
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
if not view.View.float.quit_on_focus_loss then
Copy link
Collaborator Author

@geril2207 geril2207 Oct 30, 2023

Choose a reason for hiding this comment

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

found that vim.api.nvim_buf_delete does not delete the buffer if focus on float window, so firstly delete buffer then return focus.

vim.api.nvim_set_current_win(tree_winnr)
end
if M.config.actions.remove_file.close_window then
close_windows(buf.windows)
end
Expand Down