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

Conversation

geril2207
Copy link
Collaborator

@geril2207 geril2207 force-pushed the fix/floating-remove-opened-file branch from a322a1d to 4e3f878 Compare October 30, 2023 07:17
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.

@geril2207 geril2207 force-pushed the fix/floating-remove-opened-file branch from 4e3f878 to 0c54fa2 Compare October 30, 2023 09:59
Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

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

Fantastic work, tested OK:

not float.enable

  • remove only tree open
  • remove open
  • remove not open
  • open in multiple windows

not float.enable and not float.quit_on_focus_loss

  • remove only tree open
  • remove open
  • remove not open
  • open in multiple windows

float.enable and float.quit_on_focus_loss

  • remove only tree open
  • remove open
  • remove not open
  • open in multiple windows - the window changes to #; that's fine

float.enable and not float.quit_on_focus_loss

  • remove only tree open
  • remove open
  • remove not open
  • open in multiple windows

Please:

  • add a comment RE magic number

@@ -9,7 +9,7 @@ local M = {
}

local function close_windows(windows)
if view.View.float.enable and #vim.api.nvim_list_wins() == 1 then
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.

@geril2207 geril2207 force-pushed the fix/floating-remove-opened-file branch from 0c54fa2 to 194cc29 Compare October 31, 2023 01:20
@geril2207 geril2207 force-pushed the fix/floating-remove-opened-file branch from 194cc29 to 1910367 Compare October 31, 2023 01:28
Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

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

Many thanks for your contribution!

@@ -9,7 +9,7 @@ local M = {
}

local function close_windows(windows)
if view.View.float.enable and #vim.api.nvim_list_wins() == 1 then
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.

Safety first, thank you.

@alex-courtis alex-courtis merged commit 24bb0ed into nvim-tree:master Nov 5, 2023
@geril2207 geril2207 deleted the fix/floating-remove-opened-file branch November 6, 2023 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants