diff --git a/README.md b/README.md index 4c60b51d..6a15eb65 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ require'nvim-tree'.setup { - Double left click acts like `` - Double right click acts like `` - `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 @@ -285,7 +286,8 @@ local list = { { key = "s", action = "system_open" }, { key = "q", action = "close" }, { key = "g?", action = "toggle_help" }, - { key = "W", action = "collapse_all" } + { key = "W", action = "collapse_all" }, + { key = "S", action = "search_node" } } ``` diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 1ab222fd..ae2aeaa6 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -619,6 +619,7 @@ INFORMATIONS *nvim-tree-info* - Double left click acts like - Double right click acts like - `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: > @@ -658,7 +659,8 @@ Defaults to: { key = "s", action = "system_open" }, { key = "q", action = "close" }, { 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 diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index 0f25077b..d58f42f5 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -41,7 +41,8 @@ local M = { { key = "s", action = "system_open" }, { key = "q", action = "close"}, { key = "g?", action = "toggle_help" }, - { key = 'W', action = "collapse_all" } + { key = 'W', action = "collapse_all" }, + { key = "S", action = "search_node" } }, custom_keypress_funcs = {}, } @@ -69,6 +70,7 @@ local keypress_funcs = { refresh = require'nvim-tree.actions.reloaders'.reload_explorer, remove = require'nvim-tree.actions.remove-file'.fn, 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, toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles, toggle_help = require"nvim-tree.actions.toggles".help, diff --git a/lua/nvim-tree/actions/search-node.lua b/lua/nvim-tree/actions/search-node.lua new file mode 100644 index 00000000..3ad6a698 --- /dev/null +++ b/lua/nvim-tree/actions/search-node.lua @@ -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