Skip to content

Commit

Permalink
feat(extra): update pickers.oldfiles() to have current_dir option
Browse files Browse the repository at this point in the history
  • Loading branch information
abeldekat authored and echasnovski committed Jun 30, 2024
1 parent cd0ae73 commit 7cd21b7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

- BREAKING FEATURE: update `afterlines_to_code()` to result into Lua code block in help file by using `>lua` at the start instead of `>`. NOTE: users need enabled `help` tree-sitter parser (which is default on Neovim>=0.9) for code blocks to have proper highlighting.

## mini.extra

- FEATURE: update `pickers.oldfiles()` to have `current_dir` option which if `true` shows files only from picker's working directory. By @abeldekat, PR #997.


# Version 0.13.0

## mini.comment
Expand Down
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)` - 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
13 changes: 10 additions & 3 deletions lua/mini/extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1151,20 +1151,26 @@ 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)` - 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 items = vim.schedule_wrap(function()
local cwd = pick.get_picker_opts().source.cwd
local cwd = pick.get_picker_opts().source.cwd .. '/'
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 vim.startswith(path, cwd)) then
table.insert(res, H.short_path(path, cwd))
end
end
pick.set_picker_items(res)
end)
Expand Down Expand Up @@ -2010,6 +2016,7 @@ H.full_path = function(path) return (vim.fn.fnamemodify(path, ':p'):gsub('(.)/$'

H.short_path = function(path, cwd)
cwd = cwd or vim.fn.getcwd()
cwd = cwd:sub(-1) == '/' and cwd or (cwd .. '/')
if not vim.startswith(path, cwd) then return vim.fn.fnamemodify(path, ':~') end
local res = path:sub(cwd:len() + 1):gsub('^/+', ''):gsub('/+$', '')
return res
Expand Down
13 changes: 13 additions & 0 deletions tests/test_extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2667,6 +2667,19 @@ T['pickers']['oldfiles()']['can not show icons'] = function()
child.expect_screenshot()
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

child.fn.chdir(real_files_dir)
pick_oldfiles({ current_dir = true })
local items = get_picker_items()
-- - Only paths inside current directory are shown
eq(#items, 1)
eq(items[1], 'LICENSE')
end

T['pickers']['oldfiles()']['respects `opts`'] = function()
pick_oldfiles({}, { source = { name = 'My name' } })
validate_picker_name('My name')
Expand Down

0 comments on commit 7cd21b7

Please sign in to comment.