From 94576dd452e22906cebf834a311aa49c7c3c3e15 Mon Sep 17 00:00:00 2001 From: abeldekat <58370433+abeldekat@users.noreply.github.com> Date: Thu, 28 May 2026 19:31:23 +0200 Subject: [PATCH] fix(files): remove invalid window from `explorer.windows` Details: - When MiniFilesWindowUpdate uses `get_explorer_state()` with enabled preview, an error occurs after the user presses `o`, ``, `u`. --- lua/mini/files.lua | 24 ++++++++++++++++-------- tests/test_files.lua | 13 +++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lua/mini/files.lua b/lua/mini/files.lua index b3e21d6b6..89665a1dc 100644 --- a/lua/mini/files.lua +++ b/lua/mini/files.lua @@ -1544,14 +1544,8 @@ H.explorer_refresh = function(explorer, opts) explorer = H.explorer_sync_cursor_and_branch(explorer, depth) end - -- Unregister windows from showed buffers, as they might get outdated - for _, win_id in ipairs(explorer.windows) do - -- NOTE: window can be invalid if it was showing buffer that was deleted - if H.is_valid_win(win_id) then - local buf_id = vim.api.nvim_win_get_buf(win_id) - H.opened_buffers[buf_id].win_id = nil - end - end + -- Unregister windows from shown buffers, as they might get outdated + explorer = H.explorer_unregister_windows(explorer) -- Compute depth range which is possible to show in current window local depth_range = H.compute_visible_depth_range(explorer, explorer.opts) @@ -1684,6 +1678,20 @@ H.explorer_sync_cursor_and_branch = function(explorer, depth) return explorer end +H.explorer_unregister_windows = function(explorer) + local valid_windows = {} + for i, win_id in ipairs(explorer.windows) do + -- NOTE: window can be invalid if it was showing buffer that was deleted + if H.is_valid_win(win_id) then + local buf_id = vim.api.nvim_win_get_buf(win_id) + H.opened_buffers[buf_id].win_id = nil + table.insert(valid_windows, win_id) + end + end + explorer.windows = valid_windows + return explorer +end + H.explorer_go_in_range = function(explorer, buf_id, from_line, to_line) -- Compute which entries to go in: all files and only last directory local files, path, line = {}, nil, nil diff --git a/tests/test_files.lua b/tests/test_files.lua index 9b0ba0fe6..4d2a66e86 100644 --- a/tests/test_files.lua +++ b/tests/test_files.lua @@ -5733,6 +5733,19 @@ T['Events']['`MiniFilesWindowUpdate` can customize internally set window config expect_screenshot() end +T['Events']['MiniFilesWindowUpdate with preview works with `get_explorer_state()` after `undo`'] = function() + child.lua('MiniFiles.config.windows.preview = true') + child.lua([[ + vim.api.nvim_create_autocmd('User', { + pattern = 'MiniFilesWindowUpdate', + callback = function() MiniFiles.get_explorer_state() end + }) + ]]) + open() + type_keys('o', '', 'u') + eq(#get_explorer_state().windows, 2) +end + T['Events']['`MiniFilesActionCreate` triggers'] = function() track_event('MiniFilesActionCreate')