diff --git a/lua/neo-tree/sources/buffers/commands.lua b/lua/neo-tree/sources/buffers/commands.lua index 37ac287c..1d67f58b 100644 --- a/lua/neo-tree/sources/buffers/commands.lua +++ b/lua/neo-tree/sources/buffers/commands.lua @@ -80,11 +80,17 @@ M.rename = function(state) end M.set_root = function(state) - local tree = state.tree - local node = tree:get_node() - if node.type == "directory" then - buffers.navigate(state, node.id) + local node = state.tree:get_node() + while node and node.type ~= "directory" do + local parent_id = node:get_parent_id() + node = parent_id and state.tree:get_node(parent_id) or nil + end + + if not node then + return end + + buffers.navigate(state, node:get_id()) end cc._add_common_commands(M) diff --git a/lua/neo-tree/sources/filesystem/commands.lua b/lua/neo-tree/sources/filesystem/commands.lua index 09723304..3198d9f1 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -222,14 +222,21 @@ M.rename = function(state) end M.set_root = function(state) - local tree = state.tree - local node = tree:get_node() - if node.type == "directory" then - if state.search_pattern then - fs.reset_search(state, false) - end - fs._navigate_internal(state, node.id, nil, nil, false) + if state.search_pattern then + fs.reset_search(state, false) + end + + local node = state.tree:get_node() + while node and node.type ~= "directory" do + local parent_id = node:get_parent_id() + node = parent_id and state.tree:get_node(parent_id) or nil end + + if not node then + return + end + + fs._navigate_internal(state, node:get_id(), nil, nil, false) end ---Toggles whether hidden files are shown or not.