Skip to content

Commit

Permalink
(mini.files) FEATURE: Update go_in() to have close_on_file option.
Browse files Browse the repository at this point in the history
  • Loading branch information
echasnovski committed Dec 29, 2023
1 parent ce2632c commit 045d5af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ vim.keymap.set('i', '<C-z>', '<C-g>u<Esc>[s1z=`]a<C-g>u', { desc = 'Correct late
- BREAKING: Update default `write_post` hook to not display current time in success message.
- Update to include space before `~` in generated section headings.

## mini.files

- FEATURE: Update `go_in()` to have `close_on_file` option.

## mini.misc

- Update `bench_time()` to use `vim.loop.hrtime()` (as better designed for benchmarking) instead of `vim.loop.gettimeofday()`.
Expand Down
8 changes: 7 additions & 1 deletion doc/mini-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -691,14 +691,20 @@ Return ~

------------------------------------------------------------------------------
*MiniFiles.go_in()*
`MiniFiles.go_in`()
`MiniFiles.go_in`({opts})
Go in entry under cursor

Depends on entry under cursor:
- If directory, focus on it in the window to the right.
- If file, open it in the window which was current during |MiniFiles.open()|.
Explorer is not closed after that.

Parameters ~
{opts} Options. Possible fields:
- <close_on_file> `(boolean)` - whether to close explorer after going
inside a file. Powers the `go_in_plus` mapping.
Default: `false`.

------------------------------------------------------------------------------
*MiniFiles.go_out()*
`MiniFiles.go_out`()
Expand Down
25 changes: 18 additions & 7 deletions lua/mini/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -822,14 +822,29 @@ end
--- - If directory, focus on it in the window to the right.
--- - If file, open it in the window which was current during |MiniFiles.open()|.
--- Explorer is not closed after that.
MiniFiles.go_in = function()
---
---@param opts Options. Possible fields:
--- - <close_on_file> `(boolean)` - whether to close explorer after going
--- inside a file. Powers the `go_in_plus` mapping.
--- Default: `false`.
MiniFiles.go_in = function(opts)
local explorer = H.explorer_get()
if explorer == nil then return end

opts = vim.tbl_deep_extend('force', { close_on_file = false }, opts or {})

local should_close = opts.close_on_file
if should_close then
local fs_entry = MiniFiles.get_fs_entry()
should_close = fs_entry ~= nil and fs_entry.fs_type == 'file'
end

local cur_line = vim.fn.line('.')
explorer = H.explorer_go_in_range(explorer, vim.api.nvim_get_current_buf(), cur_line, cur_line)

H.explorer_refresh(explorer)

if should_close then MiniFiles.close() end
end

--- Go out to parent directory
Expand Down Expand Up @@ -1852,13 +1867,9 @@ H.buffer_make_mappings = function(buf_id, mappings)
end

local go_in_plus = function()
for _ = 1, vim.v.count1 - 1 do
MiniFiles.go_in()
for _ = 1, vim.v.count1 do
MiniFiles.go_in({ close_on_file = true })
end
local fs_entry = MiniFiles.get_fs_entry()
local is_at_file = fs_entry ~= nil and fs_entry.fs_type == 'file'
MiniFiles.go_in()
if is_at_file then MiniFiles.close() end
end

local go_out_with_count = function()
Expand Down
10 changes: 10 additions & 0 deletions tests/test_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,16 @@ T['go_in()']['works on file'] = function()
expect.match(child.cmd_capture('buffers'), '"' .. vim.pesc(test_dir_path))
end

T['go_in()']['respects `opts.close_on_file`'] = function()
open(test_dir_path)
type_keys('/', [[\.a-file]], '<CR>')
go_in({ close_on_file = true })
expect.match(child.api.nvim_buf_get_name(0), '%.a%-file$')
eq(get_lines(), { '.a-file' })

validate_n_wins(1)
end

T['go_in()']['works on files with problematic names'] = function()
local bad_name = '%a bad-file-name'
local temp_dir = make_temp_dir('temp', { bad_name })
Expand Down

0 comments on commit 045d5af

Please sign in to comment.