mini.files: "Reveal" current file in tree? #395
-
I know I can do But I'd like to reveal the current file in the whole tree from Neovim's current working directory, like this: Any way to do that? I tried to do it in my config but couldn't find an API for listing all the entries in the window, only getting one at a specified line (though I suppose I could iterate through each line). |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
Yes, there is no "get all entries" because every line describes single entry and users can iterate through lines. |
Beta Was this translation helpful? Give feedback.
-
Here's how I did it, using return {
'echasnovski/mini.files',
keys = {
{
'<F3>',
function()
local files = require('mini.files')
if vim.bo.filetype == 'minifiles' then
files.close()
else
local current_file = vim.api.nvim_buf_get_name(0)
local is_file = not vim.bo.buftype or vim.bo.buftype == ''
files.open(vim.loop.cwd(), true)
-- reveal current buffer in file tree
if is_file then
vim.schedule(function()
local line = 1
local entry = files.get_fs_entry(0, line)
while entry do
if not entry then
return
end
-- if path matches exactly, we found the file, just set cursor to the right line
if current_file == entry.path then
vim.api.nvim_win_set_cursor(0, { line, 1 })
return
-- if buffer file name has the entry path as a prefix, open the directory
elseif
current_file:find(
-- add trailing slash on directory path to avoid matching substrings
-- like `directory` matching `directory_other`;
-- we make it so `directory/` doesn't match `directory_other/`
string.format('%s/', entry.path),
1,
true
) == 1
then
vim.api.nvim_win_set_cursor(0, { line, 1 })
require('mini.files').go_in()
line = 1
else
line = line + 1
end
entry = files.get_fs_entry(0, line)
end
end)
end
end
end,
desc = 'Open mini.files',
},
},
opts = {
-- plugin options here
}
} |
Beta Was this translation helpful? Give feedback.
-
With introduction of
@mrjones2014 and @otavioschwanck, thanks for your interest in this feature! |
Beta Was this translation helpful? Give feedback.
-
Thank you! Look the updated version (much more efficient and 0 errors): local M = {}
M.path_tail = (function()
local os_sep = "/"
return function(path)
for i = #path, 1, -1 do
if path:sub(i, i) == os_sep then
return path:sub(i + 1, -1)
end
end
return path
end
end)()
function M.open_current()
local files = require('mini.files')
if vim.bo.filetype == 'minifiles' then
files.close()
else
local is_file = not vim.bo.buftype or vim.bo.buftype == ''
local current_file = vim.fn.expand("%:p")
if is_file then
vim.schedule(function()
files.open(vim.fn.expand("%:p:h"))
files.reset()
files.reveal_cwd()
local line_num = 1
local entry = files.get_fs_entry(0, line_num)
while entry do
if M.path_tail(current_file) == entry.name then
vim.api.nvim_win_set_cursor(0, { line_num, 1 })
return
end
line_num = line_num + 1
entry = files.get_fs_entry(0, line_num)
end
end)
end
end
end
return M |
Beta Was this translation helpful? Give feedback.
-
Nothing to look, just an updated version for who that wants to also select the current file. XD |
Beta Was this translation helpful? Give feedback.
With introduction of
mappings.reveal_cwd
andreveal_cwd()
this is now possible in at least two ways:MiniFiles.open()
possibly followed by manual@
(or the key supplied inmappings.reveal_cwd
).MiniFiles.open()
followed byMiniFiles.reveal_cwd()
.@mrjones2014 and @otavioschwanck, thanks for your interest in this feature!