Skip to content

Commit

Permalink
extract methods from lib
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Sep 29, 2024
1 parent 29eccca commit e527e65
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function M.place_cursor_on_node()
if not node or node.name == ".." then
return
end
node = utils.get_parent_of_group(node)
node = node:get_parent_of_group()

local line = vim.api.nvim_get_current_line()
local cursor = vim.api.nvim_win_get_cursor(0)
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/moves/parent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function M.fn(should_close)
return
end

local parent = utils.get_parent_of_group(node).parent
local parent = node:get_parent_of_group().parent

if not parent or not parent.parent then
return view.set_cursor({ 1, 0 })
Expand Down
17 changes: 2 additions & 15 deletions lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,6 @@ function M.ungroup_empty_folders(node)
end
end

---TODO move to node
---@param node Node
---@return Node[]
function M.get_all_nodes_in_group(node)
local next_node = utils.get_parent_of_group(node)
local nodes = {}
while next_node do
table.insert(nodes, next_node)
next_node = next_node.group_next
end
return nodes
end

---TODO move to node
-- Toggle group empty folders
---@param head_node Node
Expand Down Expand Up @@ -119,7 +106,7 @@ function M.expand_or_collapse(node, toggle_group)
explorer:expand(node)
end

local head_node = utils.get_parent_of_group(node)
local head_node = node:get_parent_of_group()
if toggle_group then
toggle_group_folders(head_node)
end
Expand All @@ -131,7 +118,7 @@ function M.expand_or_collapse(node, toggle_group)
else
next_open = not open
end
for _, n in ipairs(M.get_all_nodes_in_group(head_node)) do
for _, n in ipairs(head_node:get_all_nodes_in_group()) do
n.open = next_open
end

Expand Down
25 changes: 22 additions & 3 deletions lua/nvim-tree/node/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local git = require("nvim-tree.git")
local utils = require("nvim-tree.utils")

---Abstract Node class.
---Uses the abstract factory pattern to instantiate child instances.
Expand Down Expand Up @@ -217,7 +216,7 @@ end

---Refresh contents and git status for a single node
function BaseNode:refresh()
local parent_node = utils.get_parent_of_group(self)
local parent_node = self:get_parent_of_group()
local toplevel = git.get_toplevel(self.absolute_path)

git.reload_project(toplevel, self.absolute_path, function()
Expand All @@ -231,10 +230,30 @@ function BaseNode:refresh()
end)
end

---Get the highest parent of grouped nodes
---@return Node node or parent
function BaseNode:get_parent_of_group()
local node = self
while node and node.parent and node.parent.group_next do
node = node.parent or node
end
return node
end

---@return Node[]
function BaseNode:get_all_nodes_in_group()
local next_node = self:get_parent_of_group()
local nodes = {}
while next_node do
table.insert(nodes, next_node)
next_node = next_node.group_next
end
return nodes
end

---Create a sanitized partial copy of a node, populating children recursively.
---@return BaseNode cloned
function BaseNode:clone()

---@type Explorer
local placeholder

Expand Down
10 changes: 0 additions & 10 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,6 @@ function M.get_node_from_path(path)
:iterate()
end

---Get the highest parent of grouped nodes
---@param node Node
---@return Node node or parent
function M.get_parent_of_group(node)
while node and node.parent and node.parent.group_next do
node = node.parent or node
end
return node
end

M.default_format_hidden_count = function(hidden_count, simple)
local parts = {}
local total_count = 0
Expand Down

0 comments on commit e527e65

Please sign in to comment.