From 6302271ad13a62926cea817fe55a251ad623af99 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 30 Mar 2024 16:14:12 -0400 Subject: [PATCH 1/6] feat(set_root): allow filesystem and buffers to run set_root when the cursor is on both files and directories --- lua/neo-tree/sources/buffers/commands.lua | 8 +++----- lua/neo-tree/sources/filesystem/commands.lua | 13 ++++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lua/neo-tree/sources/buffers/commands.lua b/lua/neo-tree/sources/buffers/commands.lua index 37ac287c..60b9052b 100644 --- a/lua/neo-tree/sources/buffers/commands.lua +++ b/lua/neo-tree/sources/buffers/commands.lua @@ -80,11 +80,9 @@ 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) - end + local node = state.tree:get_node() + local id = node.type == "directory" and node.id or node._parent_id + buffers.navigate(state, 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..1cfe9032 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -222,14 +222,13 @@ 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() + local id = node.type == "directory" and node.id or node._parent_id + fs._navigate_internal(state, id, nil, nil, false) end ---Toggles whether hidden files are shown or not. From b12f221b586e1dc3e67acd2b04406ddd6f07e0c6 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 31 Mar 2024 18:31:03 -0400 Subject: [PATCH 2/6] fix(set_root): use get methods instead of node.id and node._parent_id --- lua/neo-tree/sources/buffers/commands.lua | 2 +- lua/neo-tree/sources/filesystem/commands.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/neo-tree/sources/buffers/commands.lua b/lua/neo-tree/sources/buffers/commands.lua index 60b9052b..17ed562f 100644 --- a/lua/neo-tree/sources/buffers/commands.lua +++ b/lua/neo-tree/sources/buffers/commands.lua @@ -81,7 +81,7 @@ end M.set_root = function(state) local node = state.tree:get_node() - local id = node.type == "directory" and node.id or node._parent_id + local id = node.type == "directory" and node:get_id() or node:get_parent_id() buffers.navigate(state, id) end diff --git a/lua/neo-tree/sources/filesystem/commands.lua b/lua/neo-tree/sources/filesystem/commands.lua index 1cfe9032..5f29b39b 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -227,7 +227,7 @@ M.set_root = function(state) end local node = state.tree:get_node() - local id = node.type == "directory" and node.id or node._parent_id + local id = node.type == "directory" and node:get_id() or node:get_parent_id() fs._navigate_internal(state, id, nil, nil, false) end From 737ac4691eee89f7f9fa31bfa6eb5e1be30c60f9 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 31 Mar 2024 19:11:28 -0400 Subject: [PATCH 3/6] fix(set_root): handle nil value --- lua/neo-tree/sources/buffers/commands.lua | 6 ++++++ lua/neo-tree/sources/filesystem/commands.lua | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lua/neo-tree/sources/buffers/commands.lua b/lua/neo-tree/sources/buffers/commands.lua index 17ed562f..6e93f7bb 100644 --- a/lua/neo-tree/sources/buffers/commands.lua +++ b/lua/neo-tree/sources/buffers/commands.lua @@ -82,6 +82,12 @@ end M.set_root = function(state) local node = state.tree:get_node() local id = node.type == "directory" and node:get_id() or node:get_parent_id() + + -- "node:get_parent_id()" and "node:get_id()" may return nil in some cases + if not id then + return + end + buffers.navigate(state, id) end diff --git a/lua/neo-tree/sources/filesystem/commands.lua b/lua/neo-tree/sources/filesystem/commands.lua index 5f29b39b..bfab8683 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -228,6 +228,12 @@ M.set_root = function(state) local node = state.tree:get_node() local id = node.type == "directory" and node:get_id() or node:get_parent_id() + + -- "node:get_parent_id()" and "node:get_id()" may return nil in some cases + if not id then + return + end + fs._navigate_internal(state, id, nil, nil, false) end From 96b6ed2837d1ca8a97f396e1e9974db55d3cb50b Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 31 Mar 2024 19:43:13 -0400 Subject: [PATCH 4/6] fix(set_root): handle nil value for tree:get_node() --- lua/neo-tree/sources/buffers/commands.lua | 5 ++++- lua/neo-tree/sources/filesystem/commands.lua | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/neo-tree/sources/buffers/commands.lua b/lua/neo-tree/sources/buffers/commands.lua index 6e93f7bb..aef46294 100644 --- a/lua/neo-tree/sources/buffers/commands.lua +++ b/lua/neo-tree/sources/buffers/commands.lua @@ -81,9 +81,12 @@ end M.set_root = function(state) local node = state.tree:get_node() - local id = node.type == "directory" and node:get_id() or node:get_parent_id() + if not node then + return + end -- "node:get_parent_id()" and "node:get_id()" may return nil in some cases + local id = node.type == "directory" and node:get_id() or node:get_parent_id() if not id then return end diff --git a/lua/neo-tree/sources/filesystem/commands.lua b/lua/neo-tree/sources/filesystem/commands.lua index bfab8683..afa2e47f 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -227,9 +227,12 @@ M.set_root = function(state) end local node = state.tree:get_node() - local id = node.type == "directory" and node:get_id() or node:get_parent_id() + if not node then + return + end -- "node:get_parent_id()" and "node:get_id()" may return nil in some cases + local id = node.type == "directory" and node:get_id() or node:get_parent_id() if not id then return end From aebf60412aef30c100b4edee0ac421f1d1773ad5 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 31 Mar 2024 19:45:08 -0400 Subject: [PATCH 5/6] fix(set_root): remove unnecessary comment --- lua/neo-tree/sources/buffers/commands.lua | 1 - lua/neo-tree/sources/filesystem/commands.lua | 1 - 2 files changed, 2 deletions(-) diff --git a/lua/neo-tree/sources/buffers/commands.lua b/lua/neo-tree/sources/buffers/commands.lua index aef46294..5af7a165 100644 --- a/lua/neo-tree/sources/buffers/commands.lua +++ b/lua/neo-tree/sources/buffers/commands.lua @@ -85,7 +85,6 @@ M.set_root = function(state) return end - -- "node:get_parent_id()" and "node:get_id()" may return nil in some cases local id = node.type == "directory" and node:get_id() or node:get_parent_id() if not id then return diff --git a/lua/neo-tree/sources/filesystem/commands.lua b/lua/neo-tree/sources/filesystem/commands.lua index afa2e47f..83a6dde4 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -231,7 +231,6 @@ M.set_root = function(state) return end - -- "node:get_parent_id()" and "node:get_id()" may return nil in some cases local id = node.type == "directory" and node:get_id() or node:get_parent_id() if not id then return From 2b754f4a8087d344104395c565c8f41dbbd2ffc5 Mon Sep 17 00:00:00 2001 From: xvzc Date: Wed, 3 Apr 2024 15:22:26 -0400 Subject: [PATCH 6/6] fix(set_root): use while loop to find parent_id --- lua/neo-tree/sources/buffers/commands.lua | 10 +++++----- lua/neo-tree/sources/filesystem/commands.lua | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lua/neo-tree/sources/buffers/commands.lua b/lua/neo-tree/sources/buffers/commands.lua index 5af7a165..1d67f58b 100644 --- a/lua/neo-tree/sources/buffers/commands.lua +++ b/lua/neo-tree/sources/buffers/commands.lua @@ -81,16 +81,16 @@ end M.set_root = function(state) local node = state.tree:get_node() - if not node then - return + 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 - local id = node.type == "directory" and node:get_id() or node:get_parent_id() - if not id then + if not node then return end - buffers.navigate(state, id) + 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 83a6dde4..3198d9f1 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -227,16 +227,16 @@ M.set_root = function(state) end local node = state.tree:get_node() - if not node then - return + 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 - local id = node.type == "directory" and node:get_id() or node:get_parent_id() - if not id then + if not node then return end - fs._navigate_internal(state, id, nil, nil, false) + fs._navigate_internal(state, node:get_id(), nil, nil, false) end ---Toggles whether hidden files are shown or not.