Skip to content

Commit 342fa09

Browse files
fix(files): make default explorer work with 'mini.pick'
Details: - Picking a directory in 'mini.pick' (like in 'visit_paths` 'mini.extra' picker) is intended to execute `:edit <dir-path>`. With 'mini.files' enabled as default explorer, this throws an error when trying to delete a buffer. This happened because of the "smart delete buffer" logic in 'mini.files' default explorer implementation. This commit adjusts the behavior and covers more use cases with tests. Resolve #2156 Co-authored-by: Evgeni Chasnovski <[email protected]>
1 parent c889667 commit 342fa09

File tree

5 files changed

+53
-78
lines changed

5 files changed

+53
-78
lines changed

lua/mini/files.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,11 @@ H.track_dir_edit = function(data)
13821382
if vim.b.minifiles_processed_dir then
13831383
-- Smartly delete directory buffer if already visited
13841384
local alt_buf = vim.fn.bufnr('#')
1385-
if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then vim.api.nvim_win_set_buf(0, alt_buf) end
1385+
-- - Setting alternative buffer is enough for the "directory buffer" to be
1386+
-- wiped out, as it has `bufhidden=wipe`. Forcing delete after showing alt
1387+
-- buffer might result in hard-to-track errors (like when opening directory
1388+
-- in 'mini.pick' when there is an altbuf for the buffer in target window).
1389+
if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then return vim.api.nvim_win_set_buf(0, alt_buf) end
13861390
return vim.api.nvim_buf_delete(data.buf, { force = true })
13871391
end
13881392

tests/screenshots/tests-test_files.lua---Default-explorer---handles-close-without-opening-file

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/screenshots/tests-test_files.lua---Default-explorer---handles-close-without-opening-file-002

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/test_files.lua

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5769,15 +5769,37 @@ T['Default explorer']['works in `:tabfind .`'] = function()
57695769
end
57705770

57715771
T['Default explorer']['handles close without opening file'] = function()
5772+
local validate = function()
5773+
local buf_name = child.api.nvim_buf_get_name(0)
5774+
child.cmd('edit ' .. test_dir_path)
5775+
eq(is_explorer_active(), true)
5776+
close()
5777+
eq(is_explorer_active(), false)
5778+
eq(child.api.nvim_buf_get_name(0), buf_name)
5779+
eq(#child.api.nvim_list_bufs(), 1)
5780+
end
5781+
5782+
-- Should hide "directory buffer" if there is no alternative buffer
5783+
validate()
5784+
5785+
-- Should smartly (preserving layout) delete "directory buffer"
5786+
local win_id_other = child.api.nvim_get_current_win()
57725787
child.cmd('wincmd v')
5773-
child.cmd('edit ' .. test_dir_path)
5774-
child.expect_screenshot()
5788+
local win_id = child.api.nvim_get_current_win()
5789+
local buf_id = child.api.nvim_get_current_buf()
57755790

5776-
-- Should close and smartly (preserving layout) delete "directory buffer"
5777-
close()
5778-
child.expect_screenshot()
5779-
eq(child.api.nvim_buf_get_name(0), '')
5780-
eq(#child.api.nvim_list_bufs(), 1)
5791+
eq(child.fn.win_findbuf(buf_id), { win_id, win_id_other })
5792+
validate()
5793+
eq(child.fn.win_findbuf(buf_id), { win_id, win_id_other })
5794+
end
5795+
5796+
T['Default explorer']['handles forcing other window as current'] = function()
5797+
child.cmd('edit ' .. test_file_path)
5798+
local init_win_id = child.api.nvim_get_current_win()
5799+
5800+
child.cmd('edit ' .. test_dir_path)
5801+
expect.no_error(function() child.api.nvim_set_current_win(init_win_id) end)
5802+
if child.fn.has('nvim-0.10') == 1 then eq(child.api.nvim_get_current_win(), init_win_id) end
57815803
end
57825804

57835805
return T

tests/test_pick.lua

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,21 +2105,36 @@ T['default_choose()']['mimics empty buffer reuse'] = function()
21052105
end
21062106

21072107
T['default_choose()']['works for directory path'] = function()
2108-
local validate = function(item, path)
2108+
local validate = function(item, path, filetype)
21092109
local buf_id_init = child.api.nvim_get_current_buf()
21102110
default_choose(item)
21112111

21122112
local buf_id_cur = child.api.nvim_get_current_buf()
2113-
eq(child.bo.filetype, 'netrw')
2113+
eq(child.bo.filetype, filetype)
21142114
validate_buf_name(buf_id_init, path)
21152115

21162116
-- Cleanup
21172117
child.api.nvim_buf_delete(buf_id_init, { force = true })
21182118
child.api.nvim_buf_delete(buf_id_cur, { force = true })
2119+
2120+
child.lua('if _G.MiniFiles ~= nil then _G.MiniFiles.close() end')
21192121
end
21202122

2121-
validate(test_dir, test_dir)
2122-
validate({ text = test_dir, path = test_dir }, test_dir)
2123+
validate(test_dir, test_dir, 'netrw')
2124+
validate({ text = test_dir, path = test_dir }, test_dir, 'netrw')
2125+
2126+
-- Should work with 'mini.files' as default explorer
2127+
child.lua('require("mini.files").setup()')
2128+
validate(test_dir, test_dir, 'minifiles')
2129+
validate({ text = test_dir, path = test_dir }, test_dir, 'minifiles')
2130+
2131+
-- - Should work when there is an already opened file (matters in which code
2132+
-- path 'mini.files' takes when acting as a default explorer).
2133+
-- Earlier resulted in an outdated 'mini.pick' floating window.
2134+
child.cmd('edit ' .. real_file('a.lua'))
2135+
start_with_items({ test_dir })
2136+
type_keys('<CR>')
2137+
eq(#child.api.nvim_list_wins(), 2)
21232138
end
21242139

21252140
T['default_choose()']['works for buffer'] = function()

0 commit comments

Comments
 (0)