Builtin previewer: improve mechanism for backing up/restoring winopts

See :help local-options for more details, but essentially, we have to be
very careful with how we set window-local options on popup windows,
because buffers might take some window-local options from the last
window they were displayed in with them to the new window in certain
circumstances.

This commit changes the backup/restore for the window options so that
the possible window-local options that might get clobbered are saved
from the original active window and reapplied to the preview window just
before it's closed. This ensures any buffers loaded from the picker will
have the same window options applied that were in the active window when
fzf was opened.
main
John Drouhard 3 years ago
parent d8b5981e92
commit d34444e860

@ -72,11 +72,7 @@ function Previewer:new(o, opts, fzf_win)
end
function Previewer:close()
-- restore winopts backup for those that weren't restored
-- (usually the last previewed loaded buffer)
for bufnr, _ in pairs(self.backups) do
self:restore_winopts(bufnr, self.win.preview_winid)
end
self:restore_winopts(self.win.preview_winid)
self:clear_preview_buf()
self.backups = {}
_self = nil
@ -109,31 +105,26 @@ function Previewer:gen_winopts()
cursorlineopt = 'both',
cursorcolumn = false,
signcolumn = 'no',
list = false,
foldenable = false,
foldmethod = 'manual',
}
end
function Previewer:backup_winopts(key, win)
if not key then return end
function Previewer:backup_winopts(win)
if not win or not api.nvim_win_is_valid(win) then return end
self.backups[key] = {}
for opt, _ in pairs(self:gen_winopts()) do
if utils.nvim_has_option(opt) then
self.backups[key][opt] = api.nvim_win_get_option(win, opt)
self.backups[opt] = api.nvim_win_get_option(win, opt)
end
end
end
function Previewer:restore_winopts(key, win)
if not self.backups[key] then return end
function Previewer:restore_winopts(win)
if not win or not api.nvim_win_is_valid(win) then return end
for opt, v in pairs(self.backups[key]) do
if utils.nvim_has_option(opt) then
api.nvim_win_set_option(win, opt, v)
for opt, value in pairs(self.backups) do
vim.api.nvim_win_set_option(win, opt, value)
end
end
self.backups[key] = nil
end
function Previewer:set_winopts(win)
@ -269,10 +260,8 @@ function Previewer:display_last_entry()
end
function Previewer:preview_buf_post(entry)
-- backup window options
local bufnr = self.preview_bufnr
local preview_winid = self.win.preview_winid
self:backup_winopts(bufnr, preview_winid)
-- set preview win options or load the file
-- if not already loaded from buffer
@ -308,11 +297,12 @@ function Previewer:display_entry(entry)
self.last_entry = entry
end
if not self.win or not self.win:validate_preview() then return end
if rawequal(next(self.backups), nil) then
self:backup_winopts(self.win.src_winid)
end
local preview_winid = self.win.preview_winid
local previous_bufnr = api.nvim_win_get_buf(preview_winid)
assert(not self.preview_bufnr or previous_bufnr == self.preview_bufnr)
-- restore settings for the buffer we were previously viewing
self:restore_winopts(previous_bufnr, preview_winid)
-- clear the current preview buffer
local bufnr = self:clear_preview_buf()
-- store the preview buffer

@ -387,6 +387,7 @@ function FzfWin:create()
-- save sending bufnr/winid
self.src_bufnr = vim.api.nvim_get_current_buf()
self.src_winid = vim.api.nvim_get_current_win()
if self.winopts.split then
vim.cmd(self.winopts.split)
self.fzf_bufnr = vim.api.nvim_get_current_buf()

Loading…
Cancel
Save