forked from pwntester/codeql.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.lua
More file actions
38 lines (34 loc) · 975 Bytes
/
Copy pathtree.lua
File metadata and controls
38 lines (34 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
local Tree = {}
Tree.__index = Tree
function Tree:new(id, name, type, children)
local tree = setmetatable({}, Tree)
tree.id = id
tree.name = name
tree.type = type
tree.children = children or {}
return tree
end
-- Calling the tree like a function will create and return children if they are not found.
function Tree:__call(id, name, type)
for _, child in ipairs(self.children) do
if child.id == id then
return child
end
end
local child = Tree:new(id, name, type)
table.insert(self.children, child)
return child
end
function Tree:flatten_directories()
for _, child in ipairs(self.children) do
if child.type == "dir" and #child.children == 1 and child.children[1].type == "dir" then
child.id = child.children[1].id
child.type = child.children[1].type
child.name = child.children[1].name
child.children = child.children[1].children
self:flatten_directories()
end
end
return self
end
return Tree