Skip to content

fix: preview on floating window #1648

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

Almo7aya
Copy link
Contributor

@Almo7aya Almo7aya commented Oct 14, 2022

fixes #1643

My changes prevent nvim-tree from using already closed window, usually when using floating window mode.

More details in the issue #1643

@Almo7aya Almo7aya changed the title fix: fix: preview on floating window Oct 14, 2022
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.

Testing with nvt-min.lua from the issue resulted in an empty nvim-tree window. Upon R the contents were correctly repopulated.

asciicast

@Almo7aya
Copy link
Contributor Author

Ummmm, after some digging I found out that I have

update_focused_file = {
	enable = true,
},

in my config file, when removing it the issue you faced occurs, I'm checking...

You can try this config with my branch and update_focused_file = true

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvt-min/site]])
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
	require("packer").startup({
		{
			"wbthomason/packer.nvim",
			{
				"almo7aya/nvim-tree.lua",
				branch = "fix/preview_action_on_floating_windows",
			},
			"kyazdani42/nvim-web-devicons",
			-- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
		},
		config = {
			package_root = package_root,
			compile_path = install_path .. "/plugin/packer_compiled.lua",
			display = { non_interactive = true },
		},
	})
end
if vim.fn.isdirectory(install_path) == 0 then
	print("Installing nvim-tree and dependencies.")
	vim.fn.system({ "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path })
end
load_plugins()
require("packer").sync()
vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]])
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
	require("nvim-tree").setup({
		update_focused_file = {
			enable = true,
		},
		view = {
			float = {
				enable = true,
			},
		},
	})
end

vim.api.nvim_set_keymap("n", "<Space>e", "<cmd>:NvimTreeToggle<CR>", {})

@alex-courtis
Copy link
Member

You can try this config with my branch and update_focused_file = true

That works as expected.

@Almo7aya
Copy link
Contributor Author

Almo7aya commented Oct 15, 2022

seems like

  if opts.update_focused_file.enable then
    create_nvim_tree_autocmd("BufEnter", {
      callback = function()
        M.find_file(false)
      end,
    })
  end

has something todo with this, removing M.find_file(false) results in the same issue you mentioned

Edit:

adding

require("nvim-tree.renderer").draw()

fixes the issue for update_focused_file = false
I will update the PR

@alex-courtis
Copy link
Member

Cursory investigation:

It seems that the tree window is being closed during the preview action: https://github.com/Almo7aya/nvim-tree.lua/blob/79f631bc1d52b387f4ae59fad1291d894afa97f5/lua/nvim-tree/actions/node/open-file.lua#L134

Rather than being reactive to the closed window in focus, we could keep the floating tree window open.

@Almo7aya
Copy link
Contributor Author

Almo7aya commented Oct 15, 2022

After a deep debugging session, I discovered that we have an option called view.float.quit_on_focus_loss which is enabled by default to kill the window when losing focus

  if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
    create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = view.close })
  end

so ignoring this autocmd on preview fixes the issue

use this config to test all the cases

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvt-min/site]])
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
	require("packer").startup({
		{
			"wbthomason/packer.nvim",
			{
				"almo7aya/nvim-tree.lua",
				branch = "fix/preview_action_on_floating_windows",
			},
			"kyazdani42/nvim-web-devicons",
			-- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
		},
		config = {
			package_root = package_root,
			compile_path = install_path .. "/plugin/packer_compiled.lua",
			display = { non_interactive = true },
		},
	})
end
if vim.fn.isdirectory(install_path) == 0 then
	print("Installing nvim-tree and dependencies.")
	vim.fn.system({ "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path })
end
load_plugins()
require("packer").sync()
vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]])
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
	require("nvim-tree").setup({
		actions = {
			open_file = {
				quit_on_open = true,
			},
		},
		update_focused_file = {
			enable = true,
		},
		view = {
			float = {
				enable = true,
				quit_on_focus_loss = true,
			},
		},
	})
end

vim.api.nvim_set_keymap("n", "<Space>e", "<cmd>:NvimTreeToggle<CR>", {})

@Almo7aya Almo7aya requested a review from alex-courtis October 15, 2022 03:13
@alex-courtis
Copy link
Member

Still blank after preview. The floating window is definitely still being closed. This can be validated via :echo win_getid() before and after the preview.

The next call to set_current_win_no_autocmd is the one actually causing the WinLeave event to be fired. This could be moved into an else, something like

  if mode == "preview" then
    -- ignore "WinLeave" autocmd on preview
    -- because the registered "WinLeave"
    -- will kill the floating window immediately
    set_current_win_no_autocmd(api.nvim_get_current_win(), "WinLeave")
  else
    set_current_win_no_autocmd(target_winid)
  end

Unfortunately there is another WinLeave event fired after open_in_new_window.

@Almo7aya
Copy link
Contributor Author

I was calling set_current_win_no_autocmd with the wrong window id, I adjust it now, I can't replicate the blank window after preview

@alex-courtis
Copy link
Member

I was calling set_current_win_no_autocmd with the wrong window id, I adjust it now, I can't replicate the blank window after preview

Nice work. I'm out of time today, I'll take it for a drive tomorrow.

@@ -209,7 +209,15 @@ local function open_in_new_window(filename, mode, win_ids)
cmd = string.format("edit %s", fname)
end

set_current_win_no_autocmd(target_winid)
if mode == "preview" then
Copy link
Member

Choose a reason for hiding this comment

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

This is correct, however is only desirable for floating windows. There may be (future) WinLeave events in the non-floating case that we still wish to receive.

  • Please change to if mode == "preview" and view float is set then

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.

Tested OK with open-file changes only:

  • float enabled
  • float disabled
  • filter visible
  • quit_on_focus_lost disabled

@alex-courtis alex-courtis merged commit c995ce0 into nvim-tree:master Oct 16, 2022
@alex-courtis
Copy link
Member

Thank you so much for this fix!

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.

Error when calling preview when on floating mode
2 participants