diff --git a/pkg/gui/filetree/build_tree.go b/pkg/gui/filetree/build_tree.go index c7c465e28e8..18112ccb190 100644 --- a/pkg/gui/filetree/build_tree.go +++ b/pkg/gui/filetree/build_tree.go @@ -10,6 +10,8 @@ import ( func BuildTreeFromFiles(files []*models.File) *Node[models.File] { root := &Node[models.File]{} + childrenMapsByNode := make(map[*Node[models.File]]map[string]*Node[models.File]) + var curr *Node[models.File] for _, file := range files { splitPath := split(file.Name) @@ -23,19 +25,30 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] { } path := join(splitPath[:i+1]) - for _, existingChild := range curr.Children { - if existingChild.Path == path { - curr = existingChild - continue outer - } + + var currNodeChildrenMap map[string]*Node[models.File] + var isCurrNodeMapped bool + + if currNodeChildrenMap, isCurrNodeMapped = childrenMapsByNode[curr]; !isCurrNodeMapped { + currNodeChildrenMap = make(map[string]*Node[models.File]) + childrenMapsByNode[curr] = currNodeChildrenMap + } + + child, doesCurrNodeHaveChildAlready := currNodeChildrenMap[path] + if doesCurrNodeHaveChildAlready { + curr = child + continue outer } newChild := &Node[models.File]{ Path: path, File: setFile, } + curr.Children = append(curr.Children, newChild) + currNodeChildrenMap[path] = newChild + curr = newChild } }