From 045d5af8faa5b796a2e3c62b3362cd997ff8cb07 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Fri, 29 Dec 2023 19:16:23 +0200 Subject: [PATCH] (mini.files) FEATURE: Update `go_in()` to have `close_on_file` option. --- CHANGELOG.md | 4 ++++ doc/mini-files.txt | 8 +++++++- lua/mini/files.lua | 25 ++++++++++++++++++------- tests/test_files.lua | 10 ++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73e84568a..148f95c24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ vim.keymap.set('i', '', 'u[s1z=`]au', { 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()`. diff --git a/doc/mini-files.txt b/doc/mini-files.txt index e132c218f..c30a40bf6 100644 --- a/doc/mini-files.txt +++ b/doc/mini-files.txt @@ -691,7 +691,7 @@ Return ~ ------------------------------------------------------------------------------ *MiniFiles.go_in()* - `MiniFiles.go_in`() + `MiniFiles.go_in`({opts}) Go in entry under cursor Depends on entry under cursor: @@ -699,6 +699,12 @@ Depends on entry under cursor: - If file, open it in the window which was current during |MiniFiles.open()|. Explorer is not closed after that. +Parameters ~ +{opts} Options. Possible fields: + - `(boolean)` - whether to close explorer after going + inside a file. Powers the `go_in_plus` mapping. + Default: `false`. + ------------------------------------------------------------------------------ *MiniFiles.go_out()* `MiniFiles.go_out`() diff --git a/lua/mini/files.lua b/lua/mini/files.lua index 087cd00d0..5aa41c2b8 100644 --- a/lua/mini/files.lua +++ b/lua/mini/files.lua @@ -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: +--- - `(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 @@ -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() diff --git a/tests/test_files.lua b/tests/test_files.lua index 656f41d12..e3c9adbfc 100644 --- a/tests/test_files.lua +++ b/tests/test_files.lua @@ -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]], '') + 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 })