Skip to content

Commit 1653c4c

Browse files
committed
fix(preview): copy_exmtarks in decro callback
fix: bufwinid -> win_findbuf to find window in other tabpage
1 parent cfc0857 commit 1653c4c

3 files changed

Lines changed: 41 additions & 28 deletions

File tree

lua/fzf-lua/previewer/builtin.lua

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -618,36 +618,46 @@ function Previewer.base:scroll(direction)
618618
self.win:update_preview_scrollbar()
619619
self:update_render_markdown()
620620
self:update_ts_context()
621-
self:copy_extmarks()
622621
end
623622

624623
-- https://github.com/kevinhwang91/nvim-bqf/blob/b51a37fcd808edafd52511458467c8c9a701ea8d/lua/bqf/preview/extmark.lua#L20
624+
-- copy extmarks from src_buf to buf
625+
---@param src_buf integer
626+
---@param buf integer
627+
---@param win integer
628+
---@param topline integer 0-based
629+
---@param botline integer 0-based
630+
---@param ns integer
631+
local copy_extmarks = function(src_buf, buf, win, topline, botline, ns)
632+
local src_win = fn.win_findbuf(src_buf)[1]
633+
local src_leftcol = src_win and api.nvim_win_call(src_win, fn.winsaveview).leftcol or nil
634+
-- virt_text is related to screen https://github.com/neovim/neovim/issues/14050
635+
local no_virt_text = api.nvim_win_call(win, fn.winsaveview).leftcol ~= src_leftcol
636+
api.nvim_buf_clear_namespace(buf, ns, topline, botline)
637+
local extmarks = api.nvim_buf_get_extmarks(src_buf, -1, { topline, 0 }, { botline, -1 },
638+
{ details = true })
639+
for _, m in ipairs(extmarks) do
640+
local _, row, col, details = unpack(m) ---@cast details -?
641+
details.ns_id = nil
642+
if no_virt_text and details.virt_text then details.virt_text = nil end
643+
pcall(api.nvim_buf_set_extmark, buf, ns, row, col, details)
644+
end
645+
end
646+
647+
---@return fun()
625648
function Previewer.base:copy_extmarks()
626-
-- copy extmarks from source window to preview window
627-
if not self.win or not self.win:validate_preview() or not self.loaded_entry or not self.loaded_entry.bufnr then return end
628649
self.ns_extmarks = self.ns_extmarks or api.nvim_create_namespace("fzf-lua.preview.extmarks")
629-
local src_bufnr = self.loaded_entry.bufnr
630-
local dst_bufnr = self.preview_bufnr
631-
local ns = self.ns_extmarks
632-
local win = self.win.preview_winid
633-
local winview = api.nvim_win_call(win, fn.winsaveview)
634-
local topline = winview.topline
635-
local botline = fn.line("w$", win)
636-
local src_winid = fn.bufwinid(src_bufnr)
637-
local src_leftcol = src_winid ~= -1 and api.nvim_win_call(src_winid, fn.winsaveview).leftcol or nil
638-
-- virt_text is related to screen https://github.com/neovim/neovim/issues/14050
639-
local no_virt_text = winview.leftcol ~= src_leftcol
640-
api.nvim_buf_clear_namespace(dst_bufnr, ns, topline, botline)
641-
for _, n in pairs(api.nvim_get_namespaces()) do
642-
local extmarks = api.nvim_buf_get_extmarks(src_bufnr, n, { topline - 1, 0 },
643-
{ botline - 1, -1 },
644-
{ details = true })
645-
for _, m in ipairs(extmarks) do
646-
local _, row, col, details = unpack(m) ---@cast details -?
647-
details.ns_id = nil
648-
if no_virt_text and details.virt_text then details.virt_text = nil end
649-
pcall(api.nvim_buf_set_extmark, dst_bufnr, ns, row, col, details)
650-
end
650+
api.nvim_set_decoration_provider(self.ns_extmarks, {
651+
on_win = function(_, win, buf, topline, botline)
652+
if win ~= self.win.preview_winid then return end
653+
local src_buf = self.loaded_entry and self.loaded_entry.bufnr
654+
if not src_buf then return end
655+
copy_extmarks(src_buf, buf, win, topline, botline, self.ns_extmarks)
656+
return false
657+
end,
658+
})
659+
return function()
660+
api.nvim_set_decoration_provider(self.ns_extmarks, {})
651661
end
652662
end
653663

@@ -1535,7 +1545,6 @@ function Previewer.buffer_or_file:preview_buf_post(entry, min_winopts)
15351545
self:update_render_markdown()
15361546
self:update_ts_context()
15371547
self:attach_snacks_image_inline()
1538-
vim.schedule(function() self:copy_extmarks() end)
15391548
end
15401549
-- for attach_snacks_image_{inline,buf}
15411550
-- https://github.com/folke/snacks.nvim/pull/1615

lua/fzf-lua/rpc.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ local rpc_nvim_exec_lua = function(opts)
7171
function_id = opts.fnc_id,
7272
pipe_path = server_socket_path,
7373
selection = opts.selection,
74-
env = vim.uv.os_environ(),
74+
env = uv.os_environ(),
7575
}
7676
local success, errmsg = pcall(function()
7777
local chan_id = vim.fn.sockconnect("pipe", opts.fzf_lua_server, { rpc = true })

lua/fzf-lua/win.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,11 @@ function FzfWin:redraw_preview()
669669
previewer:reset_winhl(self.preview_winid)
670670
previewer:display_last_entry()
671671
previewer:update_ts_context()
672-
self.on_closes.preview = function(hide) self:close_preview(hide) end
672+
local release = previewer:copy_extmarks()
673+
self.on_closes.preview = function(hide)
674+
if release then release() end
675+
self:close_preview(hide)
676+
end
673677
end
674678

675679
function FzfWin:validate()

0 commit comments

Comments
 (0)