feat: Add a new keybinding to search and expand a certain path (#1014)

This commit is contained in:
Andreas Bissinger
2022-02-22 23:36:59 +01:00
committed by GitHub
parent 19346da6e1
commit 48e76bc031
4 changed files with 78 additions and 3 deletions

View File

@@ -222,6 +222,7 @@ require'nvim-tree'.setup {
- Double left click acts like `<CR>` - Double left click acts like `<CR>`
- Double right click acts like `<C-]>` - Double right click acts like `<C-]>`
- `W` will collapse the whole tree - `W` will collapse the whole tree
- `S` will prompt the user to enter a path and then expands the tree to match the path
### Settings ### Settings
@@ -285,7 +286,8 @@ local list = {
{ key = "s", action = "system_open" }, { key = "s", action = "system_open" },
{ key = "q", action = "close" }, { key = "q", action = "close" },
{ key = "g?", action = "toggle_help" }, { key = "g?", action = "toggle_help" },
{ key = "W", action = "collapse_all" } { key = "W", action = "collapse_all" },
{ key = "S", action = "search_node" }
} }
``` ```

View File

@@ -619,6 +619,7 @@ INFORMATIONS *nvim-tree-info*
- Double left click acts like <CR> - Double left click acts like <CR>
- Double right click acts like <C-]> - Double right click acts like <C-]>
- `W` will collapse the whole tree - `W` will collapse the whole tree
- `S` will prompt the user to enter a path and then expands the tree to match the path
Defaults to: Defaults to:
> >
@@ -658,7 +659,8 @@ Defaults to:
{ key = "s", action = "system_open" }, { key = "s", action = "system_open" },
{ key = "q", action = "close" }, { key = "q", action = "close" },
{ key = "g?", action = "toggle_help" }, { key = "g?", action = "toggle_help" },
{ key = 'W', action = "collapse_all" } { key = 'W', action = "collapse_all" },
{ key = "S", action = "search_node" }
} }
< <
The `list` option in `view.mappings.list` is a table of The `list` option in `view.mappings.list` is a table of

View File

@@ -41,7 +41,8 @@ local M = {
{ key = "s", action = "system_open" }, { key = "s", action = "system_open" },
{ key = "q", action = "close"}, { key = "q", action = "close"},
{ key = "g?", action = "toggle_help" }, { key = "g?", action = "toggle_help" },
{ key = 'W', action = "collapse_all" } { key = 'W', action = "collapse_all" },
{ key = "S", action = "search_node" }
}, },
custom_keypress_funcs = {}, custom_keypress_funcs = {},
} }
@@ -69,6 +70,7 @@ local keypress_funcs = {
refresh = require'nvim-tree.actions.reloaders'.reload_explorer, refresh = require'nvim-tree.actions.reloaders'.reload_explorer,
remove = require'nvim-tree.actions.remove-file'.fn, remove = require'nvim-tree.actions.remove-file'.fn,
rename = require'nvim-tree.actions.rename-file'.fn(false), rename = require'nvim-tree.actions.rename-file'.fn(false),
search_node = require'nvim-tree.actions.search-node'.fn,
system_open = require'nvim-tree.actions.system-open'.fn, system_open = require'nvim-tree.actions.system-open'.fn,
toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles, toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles,
toggle_help = require"nvim-tree.actions.toggles".help, toggle_help = require"nvim-tree.actions.toggles".help,

View File

@@ -0,0 +1,69 @@
local utils = require"nvim-tree.utils"
local view = require"nvim-tree.view"
local renderer = require"nvim-tree.renderer"
local M = {}
function M.fn()
if not TreeExplorer then return end
local input_path = vim.fn.input("Search node: ", "", "file")
utils.clear_prompt()
local absolute_input_path = utils.path_join({
TreeExplorer.cwd,
input_path
})
local tree_altered = false
local function search_node(nodes)
-- first search for absolute match
local index_absolute_match = 0
for _, node in ipairs(nodes) do
index_absolute_match = index_absolute_match + 1
if absolute_input_path == node.absolute_path then
return index_absolute_match
end
end
-- if no absolute match in current directory, then search for partial match
local index_partial_match = 0
for _, node in ipairs(nodes) do
index_partial_match = index_partial_match + 1
if node.nodes then
local matches = utils.str_find(absolute_input_path, node.absolute_path)
if matches then
if not node.open then
node.open = true
TreeExplorer:expand(node)
tree_altered = true
end
return index_partial_match + search_node(node.nodes)
end
end
end
return 0
end
local index = search_node(TreeExplorer.nodes)
if tree_altered then
renderer.draw()
end
if index > 0 and view.is_visible() then
if TreeExplorer.cwd ~= '/' and not view.View.hide_root_folder then
index = index + 1
end
view.set_cursor({index, 0})
end
end
return M