feat: add vinegar abilities to the tree

fixes #860
This commit is contained in:
kiyan 2022-02-21 21:42:43 +01:00
parent f977e5c05a
commit f8f297acbf
6 changed files with 84 additions and 11 deletions

View File

@ -253,6 +253,7 @@ These are the default bindings:
-- default mappings
local list = {
{ key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
{ key = "<C-e>", action = "edit_in_place" },
{ key = {"O"}, action = "edit_no_picker" },
{ key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
{ key = "<C-v>", action = "vsplit" },
@ -293,9 +294,10 @@ You can toggle the help UI by pressing `g?`.
## Tips & reminders
1. you can add a directory by adding a `/` at the end of the paths, entering multiple directories `BASE/foo/bar/baz` will add directory foo, then bar and add a file baz to it.
2. you can update window options for the tree by setting `require"nvim-tree.view".View.winopts.MY_OPTION = MY_OPTION_VALUE`
1. You can add a directory by adding a `/` at the end of the paths, entering multiple directories `BASE/foo/bar/baz` will add directory foo, then bar and add a file baz to it.
2. You can update window options for the tree by setting `require"nvim-tree.view".View.winopts.MY_OPTION = MY_OPTION_VALUE`
3. `toggle` has a second parameter which allows to toggle without focusing the explorer (`require"nvim-tree.toggle(false, false)`).
4. You can allow nvim-tree to behave like vinegar (see `:help nvim-tree-vinegar`).
## Screenshots

View File

@ -585,7 +585,7 @@ INFORMATIONS *nvim-tree-info*
- type `>` to navigate to the next sibling of current file/directory
- type `J` to navigate to the first sibling of current file/directory
- type `K` to navigate to the last sibling of current file/directory
- type `<C-e>` to edit the file in place, effectively replacing the tree explorer.
- if the file is a directory, <CR> will open the directory
- otherwise it will open the file in the buffer near the tree
- if the file is a symlink, <CR> will follow the symlink
@ -605,6 +605,7 @@ Defaults to:
lua <<EOF
local list = {
{ key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
{ key = "<C-e>", action = "edit_in_place" },
{ key = {"O"}, action = "edit_no_picker" },
{ key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
{ key = "<C-v>", action = "vsplit" },
@ -745,7 +746,42 @@ NvimTreeFileNew
NvimTreeFileDeleted
==============================================================================
EVENTS *nvim-tree-events*
vinegar style *nvim-tree-vinegar*
|nvim_tree_vinegar| *nvim_tree_vinegar*
nvim-tree can behave like vinegar. To allow this, you will need to configure
it in a specific way:
- Use `require"nvim-tree".open_replacing_current_buffer()` instead of the
default open command.
You can easily implement a toggle using this too:
>
local function toggle_replace()
local view = require"nvim-tree.view"
if view.is_visible() then
require.close()
else
require"nvim-tree".open_replacing_current_buffer()
end
end
<
- Use the `edit_in_place` action to edit files. It's bound to `<C-e>` by
default, vinegar uses `-`. You can override this with:
>
require"nvim-tree".setup {
view = {
mappings = {
{ key = "-", action = "edit_in_place" }
}
}
}
<
You'll also need to set |nvim-tree.hijack_netrw| to `true` during setup.
A good functionnality to enable is |nvim-tree.hijack_directories|.
==============================================================================
events *nvim-tree-events*
|nvim_tree_events| *nvim_tree_events*

View File

@ -44,6 +44,25 @@ function M.open(cwd)
end
end
function M.open_replacing_current_buffer()
if view.is_visible() then
return
end
local buf = api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(buf)
if bufname == "" or vim.loop.fs_stat(bufname) == nil then
return
end
local cwd = vim.fn.fnamemodify(bufname, ':p:h')
if not TreeExplorer or cwd ~= TreeExplorer.cwd then
lib.init(cwd)
end
view.open_in_current_win({ hijack_current_buf = false, resize = false })
require"nvim-tree.renderer".draw()
end
function M.tab_change()
if view.is_visible({ any_tabpage = true }) then
local bufname = vim.api.nvim_buf_get_name(0)

View File

@ -8,7 +8,8 @@ local nvim_tree_callback = require'nvim-tree.config'.nvim_tree_callback
local M = {
mappings = {
{ key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
{ key = {"O"}, action = "edit_no_picker" },
{ key = "<C-e>", action = "edit_in_place" },
{ key = "O", action = "edit_no_picker" },
{ key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
{ key = "<C-v>", action = "vsplit" },
{ key = "<C-x>", action = "split"},
@ -96,9 +97,10 @@ function M.on_keypress(action)
end
elseif node.name == ".." then
return require'nvim-tree.actions.change-dir'.fn("..")
elseif action == "cd" and node.nodes ~= nil then
return require'nvim-tree.actions.change-dir'.fn(lib.get_last_group_node(node).absolute_path)
elseif action == "cd" then
if node.nodes ~= nil then
require'nvim-tree.actions.change-dir'.fn(lib.get_last_group_node(node).absolute_path)
end
return
end

View File

@ -154,6 +154,12 @@ function M.fn(mode, filename)
return
end
if mode == "edit_in_place" then
vim.cmd("edit " .. vim.fn.fnameescape(filename))
require"nvim-tree.view".abandon_current_window()
return
end
local tabpage = api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage)

View File

@ -194,12 +194,20 @@ local function set_current_win()
M.View.tabpages[current_tab] = { winnr = a.nvim_get_current_win() }
end
function M.open_in_current_win()
create_buffer(a.nvim_get_current_buf())
function M.open_in_current_win(opts)
opts = opts or { hijack_current_buf = true, resize = true }
create_buffer(opts.hijack_current_buf and a.nvim_get_current_buf())
set_current_win()
set_window_options_and_buffer()
M.reposition_window()
M.resize()
if opts.resize then
M.reposition_window()
M.resize()
end
end
function M.abandon_current_window()
local tab = a.nvim_get_current_tabpage()
M.View.tabpages[tab] = { winnr = nil }
end
function M.is_visible(opts)