Skip to content

Commit

Permalink
Move file-tree builder API's to dedicated ns
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Aug 1, 2024
1 parent c3edadb commit 99886cf
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 80 deletions.
94 changes: 94 additions & 0 deletions lua/hunk/api/file_tree.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
local M = {}

local function split_path(path)
local parts = {}
for part in string.gmatch(path, "([^/]+)") do
table.insert(parts, part)
end
return parts
end

local function insert_path(tree, change)
local parts = split_path(change.filepath)
local node = tree
for i, part in ipairs(parts) do
local is_last = i == #parts
local found = false

for _, child in ipairs(node.children) do
if child.name == part and child.type == "dir" then
node = child
found = true
break
end
end

if not found then
local new_node = {
name = part,
type = is_last and "file" or "dir",
change = change,
children = {},
}
table.insert(node.children, new_node)
node = new_node
end
end
end

local function sort_tree(tree)
table.sort(tree, function(a, b)
if a.type == b.type then
return a.name < b.name
else
return a.type == "dir" and b.type ~= "dir"
end
end)

for _, child in ipairs(tree) do
if child.children then
sort_tree(child.children)
end
end
end

function M.build_file_tree(changeset)
local tree = { children = {} }
for _, change in pairs(changeset) do
insert_path(tree, change)
end

sort_tree(tree.children)

return tree.children
end

function M.build_flat_file_tree(changeset)
local nodes = {}
for _, change in pairs(changeset) do
table.insert(nodes, {
name = change.filepath,
type = "file",
change = change,
children = {},
})
end

sort_tree(nodes)
return nodes
end

function M.find_first_file_in_tree(tree)
local child = tree[1]
if not child then
return
end

if child.children and #child.children > 0 then
return M.find_first_file_in_tree(child.children)
end

return child
end

return M
83 changes: 3 additions & 80 deletions lua/hunk/ui/tree.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local file_tree_api = require("hunk.api.file_tree")
local signs = require("hunk.api.signs")
local config = require("hunk.config")
local utils = require("hunk.utils")
Expand All @@ -14,84 +15,6 @@ local function get_file_extension(path)
return string.sub(extension, 2) or ""
end

local function split_path(path)
local parts = {}
for part in string.gmatch(path, "([^/]+)") do
table.insert(parts, part)
end
return parts
end

local function insert_path(tree, change)
local parts = split_path(change.filepath)
local node = tree
for i, part in ipairs(parts) do
local is_last = i == #parts
local found = false

for _, child in ipairs(node.children) do
if child.name == part and child.type == "dir" then
node = child
found = true
break
end
end

if not found then
local new_node = {
name = part,
type = is_last and "file" or "dir",
change = change,
children = {},
}
table.insert(node.children, new_node)
node = new_node
end
end
end

local function sort_tree(tree)
table.sort(tree, function(a, b)
if a.type == b.type then
return a.name < b.name
else
return a.type == "dir" and b.type ~= "dir"
end
end)

for _, child in ipairs(tree) do
if child.children then
sort_tree(child.children)
end
end
end

local function build_file_tree(changeset)
local tree = { children = {} }
for _, change in pairs(changeset) do
insert_path(tree, change)
end

sort_tree(tree.children)

return tree.children
end

local function build_flat_file_tree(changeset)
local nodes = {}
for _, change in pairs(changeset) do
table.insert(nodes, {
name = change.filepath,
type = "file",
change = change,
children = {},
})
end

sort_tree(nodes)
return nodes
end

local function get_icon(path)
local has_mini_icons, mini_icons = pcall(require, "mini.icons")

Expand Down Expand Up @@ -267,9 +190,9 @@ function M.create(opts)

local file_tree
if config.ui.tree.mode == "nested" then
file_tree = build_file_tree(opts.changeset)
file_tree = file_tree_api.build_file_tree(opts.changeset)
elseif config.ui.tree.mode == "flat" then
file_tree = build_flat_file_tree(opts.changeset)
file_tree = file_tree_api.build_flat_file_tree(opts.changeset)
else
error("Unknown value '" .. config.ui.tree("' for config entry `ui.tree.mode`"))
end
Expand Down

0 comments on commit 99886cf

Please sign in to comment.