Skip to content

Commit

Permalink
WIP: collapse/uncollapse all files in tree
Browse files Browse the repository at this point in the history
Seems like this feature is highly requested #4095 #3554

Did a quick wip pr. I got the core functionality working with files and
had some questions on what can be improved in order to polish it more.
I.e. What keymaps if any to add to these, should the functionality be to
toggle vs collapse and uncollapse separately, and if this should be
added anywhere else (right now it's just for the file tree)

Once I figure this out I can add the tests, documentations and anything
else that's missing. I'm also pretty new to the language so if there's a
way I can structure this better please let me know
  • Loading branch information
mtrajano committed Dec 27, 2024
1 parent ec410b2 commit 9ee58f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,34 @@ func (self *FilesController) enter() error {
return self.EnterFile(types.OnFocusOpts{ClickedWindowName: "", ClickedViewLineIdx: -1})
}

func (self *FilesController) collapseAll() error {
nodes := self.context().GetAllItems()

dirPaths := lo.FilterMap(nodes, func(file *filetree.FileNode, index int) (string, bool) {
return file.Path, !file.IsFile()
})

self.context().FileTreeViewModel.CollapseAll(dirPaths)

self.c.PostRefreshUpdate(self.c.Contexts().Files)

return nil
}

func (self *FilesController) uncollapseAll() error {
files := self.context().GetAllFiles()

filePaths := lo.Map(files, func(file *models.File, index int) string {
return file.GetPath()
})

self.context().FileTreeViewModel.UncollapseAll(filePaths)

self.c.PostRefreshUpdate(self.c.Contexts().Files)

return nil
}

func (self *FilesController) EnterFile(opts types.OnFocusOpts) error {
node := self.context().GetSelected()
if node == nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/gui/filetree/file_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type IFileTree interface {
GetAllFiles() []*models.File
GetFilter() FileTreeDisplayFilter
GetRoot() *FileNode
CollapseAll(paths []string)
UncollapseAll(paths []string)
}

type FileTree struct {
Expand Down Expand Up @@ -171,6 +173,18 @@ func (self *FileTree) ToggleCollapsed(path string) {
self.collapsedPaths.ToggleCollapsed(path)
}

func (self *FileTree) CollapseAll(paths []string) {
for _, path := range paths {
self.collapsedPaths.Collapse(path)
}
}

func (self *FileTree) UncollapseAll(paths []string) {
for _, path := range paths {
self.collapsedPaths.ExpandToPath(path)
}
}

func (self *FileTree) Tree() *FileNode {
return NewFileNode(self.tree)
}
Expand Down

0 comments on commit 9ee58f0

Please sign in to comment.