Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/mini-extra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,9 @@ Pick from |v:oldfiles| entries representing readable files.

Parameters ~
{local_opts} `(table|nil)` Options defining behavior of this particular picker.
Not used at the moment.
Possible fields:
- <current_dir> `(boolean|nil)` - whether to return files only from
current working directory and its subdirectories. Default: `false`.
{opts} `(table|nil)` Options forwarded to |MiniPick.start()|.

Return ~
Expand Down
12 changes: 10 additions & 2 deletions lua/mini/extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1151,20 +1151,28 @@ end
--- Pick from |v:oldfiles| entries representing readable files.
---
---@param local_opts __extra_pickers_local_opts
--- Not used at the moment.
--- Possible fields:
--- - <current_dir> `(boolean|nil)` - whether to return files only from
--- current working directory and its subdirectories. Default: `false`.
---@param opts __extra_pickers_opts
---
---@return __extra_pickers_return
MiniExtra.pickers.oldfiles = function(local_opts, opts)
local pick = H.validate_pick('oldfiles')
local_opts = vim.tbl_deep_extend('force', { current_dir = false }, local_opts or {})
local oldfiles = vim.v.oldfiles
if not H.islist(oldfiles) then H.error('`pickers.oldfiles` picker needs valid `v:oldfiles`.') end

local show_all = not local_opts.current_dir
local sep = vim.loop.os_uname().sysname == 'Windows_NT' and [[%\]] or '%/'
local items = vim.schedule_wrap(function()
local cwd = pick.get_picker_opts().source.cwd
local cwd_pattern = '^' .. vim.pesc(cwd) .. sep
local res = {}
for _, path in ipairs(oldfiles) do
if vim.fn.filereadable(path) == 1 then table.insert(res, H.short_path(path, cwd)) end
if vim.fn.filereadable(path) == 1 and (show_all or path:find(cwd_pattern) ~= nil) then
table.insert(res, H.short_path(path, cwd))
end
end
pick.set_picker_items(res)
end)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,18 @@ T['pickers']['oldfiles()']['respects `opts.source.cwd`'] = function()
expect.match(items[2], vim.pesc(child.fn.fnamemodify(ref_oldfiles[2], ':~')))
end

T['pickers']['oldfiles()']['respects `local_opts.current_dir'] = function()
child.set_size(10, 70)
local ref_oldfiles = { full_path(real_file('LICENSE')), full_path(make_testpath('mocks', 'diagnostic.lua')) }
child.v.oldfiles = ref_oldfiles

pick_oldfiles({ current_dir = true }, { source = { cwd = real_files_dir } })
local items = get_picker_items()
-- - Only paths inside `cwd` are shown
eq(#items, 1)
eq(items[1], 'LICENSE')
end

T['pickers']['options()'] = new_set()

local pick_options = forward_lua_notify('MiniExtra.pickers.options')
Expand Down