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
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 11 additions & 3 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ end

-- This is only to avoid the BufEnter for nvim-tree to trigger
-- which would cause find-file to run on an invalid file.
local function set_current_win_no_autocmd(winid)
local function set_current_win_no_autocmd(winid, autocmd)
local eventignore = vim.opt.eventignore:get()
vim.opt.eventignore:append "BufEnter"
vim.opt.eventignore:append(autocmd)
api.nvim_set_current_win(winid)
vim.opt.eventignore = eventignore
end
Expand Down Expand Up @@ -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

-- ignore "WinLeave" autocmd on preview
-- because the registered "WinLeave"
-- will kill the floating window immediately
set_current_win_no_autocmd(target_winid, { "WinLeave", "BufEnter" })
else
set_current_win_no_autocmd(target_winid, { "BufEnter" })
end

pcall(vim.cmd, cmd)
lib.set_target_win()
end
Expand Down
11 changes: 9 additions & 2 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ end

function M.focus(winnr, open_if_closed)
local wnr = winnr or M.get_winnr()

if a.nvim_win_get_tabpage(wnr or 0) ~= a.nvim_win_get_tabpage(0) then
if not a.nvim_win_is_valid(wnr) then
M.open()
wnr = M.get_winnr()
elseif a.nvim_win_get_tabpage(wnr or 0) ~= a.nvim_win_get_tabpage(0) then
M.close()
M.open()
wnr = M.get_winnr()
Expand All @@ -345,6 +347,11 @@ function M.focus(winnr, open_if_closed)
end

a.nvim_set_current_win(wnr)

-- Redraw the tree if the window has been changed
if wnr == M.get_bufnr then
require("nvim-tree.renderer").draw()
end
end

--- Restores the state of a NvimTree window if it was initialized before.
Expand Down