Skip to content

Commit

Permalink
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 Jan 6, 2025
1 parent ef718f3 commit ffffe2e
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ keybinding:
openMergeTool: M
openStatusFilter: <c-b>
copyFileInfoToClipboard: "y"
collapseAll: "-"
expandAll: "="
branches:
createPullRequest: o
viewPullRequestOptions: O
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ type KeybindingFilesConfig struct {
OpenMergeTool string `yaml:"openMergeTool"`
OpenStatusFilter string `yaml:"openStatusFilter"`
CopyFileInfoToClipboard string `yaml:"copyFileInfoToClipboard"`
CollapseAll string `yaml:"collapseAll"`
ExpandAll string `yaml:"expandAll"`
}

type KeybindingBranchesConfig struct {
Expand Down Expand Up @@ -895,6 +897,8 @@ func GetDefaultConfig() *UserConfig {
OpenStatusFilter: "<c-b>",
ConfirmDiscard: "x",
CopyFileInfoToClipboard: "y",
CollapseAll: "-",
ExpandAll: "=",
},
Branches: KeybindingBranchesConfig{
CopyPullRequestURL: "<c-y>",
Expand Down
40 changes: 40 additions & 0 deletions pkg/gui/controllers/commits_files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func (self *CommitFilesController) GetKeybindings(opts types.KeybindingsOpts) []
Description: self.c.Tr.ToggleTreeView,
Tooltip: self.c.Tr.ToggleTreeViewTooltip,
},
{
Key: opts.GetKey(opts.Config.Files.CollapseAll),
Handler: self.collapseAll,
Description: self.c.Tr.CollapseAll,
Tooltip: self.c.Tr.CollapseAllTooltip,
},
{
Key: opts.GetKey(opts.Config.Files.ExpandAll),
Handler: self.expandAll,
Description: self.c.Tr.ExpandAll,
Tooltip: self.c.Tr.ExpandAllTooltip,
},
}

return bindings
Expand Down Expand Up @@ -401,6 +413,34 @@ func (self *CommitFilesController) toggleTreeView() error {
return nil
}

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

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

self.context().CommitFileTreeViewModel.CollapseAll(dirPaths)

self.c.PostRefreshUpdate(self.context())

return nil
}

func (self *CommitFilesController) expandAll() error {
files := self.context().GetAllFiles()

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

self.context().CommitFileTreeViewModel.UncollapseAll(filePaths)

self.c.PostRefreshUpdate(self.context())

return nil
}

// NOTE: these functions are identical to those in files_controller.go (except for types) and
// could also be cleaned up with some generics
func normalisedSelectedCommitFileNodes(selectedNodes []*filetree.CommitFileNode) []*filetree.CommitFileNode {
Expand Down
40 changes: 40 additions & 0 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
Description: self.c.Tr.Fetch,
Tooltip: self.c.Tr.FetchTooltip,
},
{
Key: opts.GetKey(opts.Config.Files.CollapseAll),
Handler: self.collapseAll,
Description: self.c.Tr.CollapseAll,
Tooltip: self.c.Tr.CollapseAllTooltip,
},
{
Key: opts.GetKey(opts.Config.Files.ExpandAll),
Handler: self.expandAll,
Description: self.c.Tr.ExpandAll,
Tooltip: self.c.Tr.ExpandAllTooltip,
},
}
}

Expand Down Expand Up @@ -478,6 +490,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.context())

return nil
}

func (self *FilesController) expandAll() 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.context())

return nil
}

func (self *FilesController) EnterFile(opts types.OnFocusOpts) error {
node := self.context().GetSelected()
if node == nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/gui/filetree/commit_file_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ type CommitFileTree struct {
collapsedPaths *CollapsedPaths
}

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

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

var _ ICommitFileTree = &CommitFileTree{}

func NewCommitFileTree(getFiles func() []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileTree {
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 @@ -31,6 +31,8 @@ type ITree[T any] interface {
IsCollapsed(path string) bool
ToggleCollapsed(path string)
CollapsedPaths() *CollapsedPaths
CollapseAll(paths []string)
UncollapseAll(paths []string)
}

type IFileTree interface {
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
8 changes: 8 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ type TranslationSet struct {
NoBranchOnRemote string
Fetch string
FetchTooltip string
CollapseAll string
CollapseAllTooltip string
ExpandAll string
ExpandAllTooltip string
FileEnter string
FileEnterTooltip string
FileStagingRequirements string
Expand Down Expand Up @@ -1258,6 +1262,10 @@ func EnglishTranslationSet() *TranslationSet {
NoBranchOnRemote: `This branch doesn't exist on remote. You need to push it to remote first.`,
Fetch: `Fetch`,
FetchTooltip: "Fetch changes from remote.",
CollapseAll: "Collapse all files",
CollapseAllTooltip: "Collapse all entries in the files tree",
ExpandAll: "Expand all files",
ExpandAllTooltip: "Expand all entries in the file tree",
FileEnter: `Stage lines / Collapse directory`,
FileEnterTooltip: "If the selected item is a file, focus the staging view so you can stage individual hunks/lines. If the selected item is a directory, collapse/expand it.",
FileStagingRequirements: `Can only stage individual lines for tracked files`,
Expand Down
8 changes: 8 additions & 0 deletions schema/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,14 @@
"copyFileInfoToClipboard": {
"type": "string",
"default": "y"
},
"collapseAll": {
"type": "string",
"default": "-"
},
"expandAll": {
"type": "string",
"default": "="
}
},
"additionalProperties": false,
Expand Down

0 comments on commit ffffe2e

Please sign in to comment.