Skip to content

Commit

Permalink
fix: auto-clear/re-render on tab switch
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Feb 20, 2024
1 parent 4c6cb5a commit 95a3afd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,18 @@ api.setup = function(options)
local group = vim.api.nvim_create_augroup("image.nvim", { clear = true })

-- auto-clear on buffer / window close
vim.api.nvim_create_autocmd({ "BufLeave", "WinClosed" }, {
vim.api.nvim_create_autocmd({ "BufLeave", "WinClosed", "TabEnter" }, {
group = group,
callback = function() -- auto-clear images when windows and buffers change
vim.schedule(function()
local images = api.get_images()

local windows_in_current_tab = vim.api.nvim_tabpage_list_wins(0)
local windows_in_current_tab_map = {}
for _, current_window in ipairs(windows_in_current_tab) do
windows_in_current_tab_map[current_window] = true
end

for _, current_image in ipairs(images) do
if current_image.window then
local window_ok, is_valid_window = pcall(vim.api.nvim_win_is_valid, current_image.window)
Expand All @@ -190,6 +197,12 @@ api.setup = function(options)
return
end

local is_window_in_current_tab = windows_in_current_tab_map[current_image.window]
if not is_window_in_current_tab then
current_image:clear()
return
end

if current_image.buffer then
local buf_ok, is_valid_buffer = pcall(vim.api.nvim_buf_is_valid, current_image.buffer)
if not buf_ok or not is_valid_buffer then
Expand Down
2 changes: 1 addition & 1 deletion lua/image/utils/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ local create_document_integration = function(config)
local group = vim.api.nvim_create_augroup(group_name, { clear = true })

-- watch for window changes
vim.api.nvim_create_autocmd({ "WinNew", "BufWinEnter" }, {
vim.api.nvim_create_autocmd({ "WinNew", "BufWinEnter", "TabEnter" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
Expand Down

0 comments on commit 95a3afd

Please sign in to comment.