diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 12:26:30 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 20:14:41 +1100 |
commit | c7a629c4401ae0d4aad06767c88ce1e9e418dbf3 (patch) | |
tree | 2ff5599041030a423e02214989f07129772337c6 | |
parent | dde30fa104347ab9c01f82b7886864b473e6f51c (diff) |
make more use of generics
52 files changed, 3014 insertions, 275 deletions
@@ -14,6 +14,7 @@ require ( github.com/gookit/color v1.4.2 github.com/imdario/mergo v0.3.11 github.com/integrii/flaggy v1.4.0 + github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4 github.com/jesseduffield/gocui v0.3.1-0.20220227022729-69f0c798eec8 github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e @@ -25,6 +26,7 @@ require ( github.com/mgutz/str v1.2.0 github.com/pmezard/go-difflib v1.0.0 github.com/sahilm/fuzzy v0.1.0 + github.com/samber/lo v1.10.1 github.com/sanity-io/litter v1.5.2 github.com/sirupsen/logrus v1.4.2 github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad @@ -66,6 +66,8 @@ github.com/integrii/flaggy v1.4.0 h1:A1x7SYx4jqu5NSrY14z8Z+0UyX2S5ygfJJrfolWR3zM github.com/integrii/flaggy v1.4.0/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f h1:9USuZttmg5ioHsjFyXboiGSbncpAqcKkq9qb4ga5PD0= +github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f/go.mod h1:+LLj9/WUPAP8LqCchs7P+7X0R98HiFujVFANdNaxhGk= github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4 h1:GOQrmaE8i+KEdB8NzAegKYd4tPn/inM0I1uo0NXFerg= github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o= github.com/jesseduffield/gocui v0.3.1-0.20220227022729-69f0c798eec8 h1:9N08i5kjvOfkzMj6THmIM110wPTQLdVYEOHMHT2DFiI= @@ -129,6 +131,8 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= +github.com/samber/lo v1.10.1 h1:0D3h7i0U3hRAbaCeQ82DLe67n0A7Bbl0/cEoWqFGp+U= +github.com/samber/lo v1.10.1/go.mod h1:2I7tgIv8Q1SG2xEIkRq0F2i2zgxVpnyPOP0d3Gj2r+A= github.com/sanity-io/litter v1.5.2 h1:AnC8s9BMORWH5a4atZ4D6FPVvKGzHcnc5/IVTa87myw= github.com/sanity-io/litter v1.5.2/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= @@ -146,6 +150,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= diff --git a/pkg/commands/git_commands/rebase_test.go b/pkg/commands/git_commands/rebase_test.go index 56df77a86..4e7b5c2c6 100644 --- a/pkg/commands/git_commands/rebase_test.go +++ b/pkg/commands/git_commands/rebase_test.go @@ -8,7 +8,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/stretchr/testify/assert" ) @@ -64,7 +64,7 @@ func TestRebaseSkipEditorCommand(t *testing.T) { "^LAZYGIT_CLIENT_COMMAND=EXIT_IMMEDIATELY$", } { regexStr := regexStr - foundMatch := utils.IncludesStringFunc(envVars, func(envVar string) bool { + foundMatch := lo.ContainsBy(envVars, func(envVar string) bool { return regexp.MustCompile(regexStr).MatchString(envVar) }) if !foundMatch { diff --git a/pkg/commands/loaders/branches.go b/pkg/commands/loaders/branches.go index 1f78908a8..9fa1e80f4 100644 --- a/pkg/commands/loaders/branches.go +++ b/pkg/commands/loaders/branches.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/go-git/v5/config" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/common" @@ -181,15 +182,15 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { // TODO: only look at the new reflog commits, and otherwise store the recencies in // int form against the branch to recalculate the time ago func (self *BranchLoader) obtainReflogBranches(reflogCommits []*models.Commit) []*models.Branch { - foundBranchesMap := map[string]bool{} + foundBranches := set.New[string]() re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) reflogBranches := make([]*models.Branch, 0, len(reflogCommits)) for _, commit := range reflogCommits { if match := re.FindStringSubmatch(commit.Name); len(match) == 3 { recency := utils.UnixToTimeAgo(commit.UnixTimestamp) for _, branchName := range match[1:] { - if !foundBranchesMap[branchName] { - foundBranchesMap[branchName] = true + if !foundBranches.Includes(branchName) { + foundBranches.Add(branchName) reflogBranches = append(reflogBranches, &models.Branch{ Recency: recency, Name: branchName, diff --git a/pkg/commands/loaders/files.go b/pkg/commands/loaders/files.go index f5becdb92..8ab727453 100644 --- a/pkg/commands/loaders/files.go +++ b/pkg/commands/loaders/files.go @@ -7,7 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type FileLoaderConfig interface { @@ -57,10 +57,10 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File change := status.Change stagedChange := change[0:1] unstagedChange := change[1:2] - untracked := utils.IncludesString([]string{"??", "A ", "AM"}, change) - hasNoStagedChanges := utils.IncludesString([]string{" ", "U", "?"}, stagedChange) - hasInlineMergeConflicts := utils.IncludesString([]string{"UU", "AA"}, change) - hasMergeConflicts := hasInlineMergeConflicts || utils.IncludesString([]string{"DD", "AU", "UA", "UD", "DU"}, change) + untracked := lo.Contains([]string{"??", "A ", "AM"}, change) + hasNoStagedChanges := lo.Contains([]string{" ", "U", "?"}, stagedChange) + hasInlineMergeConflicts := lo.Contains([]string{"UU", "AA"}, change) + hasMergeConflicts := hasInlineMergeConflicts || lo.Contains([]string{"DD", "AU", "UA", "UD", "DU"}, change) file := &models.File{ Name: status.Name, diff --git a/pkg/commands/patch/hunk.go b/pkg/commands/patch/hunk.go index bbb2d54ff..98d932126 100644 --- a/pkg/commands/patch/hunk.go +++ b/pkg/commands/patch/hunk.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type PatchHunk struct { @@ -54,7 +55,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool) []string { if line == "" { break } - isLineSelected := utils.IncludesInt(lineIndices, lineIdx) + isLineSelected := lo.Contains(lineIndices, lineIdx) firstChar, content := line[:1], line[1:] transformedFirstChar := transformedFirstChar(firstChar, reverse, isLineSelected) diff --git a/pkg/commands/patch/patch_manager.go b/pkg/commands/patch/patch_manager.go index cbdf7b2d4..1282356f8 100644 --- a/pkg/commands/patch/patch_manager.go +++ b/pkg/commands/patch/patch_manager.go @@ -4,7 +4,7 @@ import ( "sort" "strings" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -140,7 +140,7 @@ func (p *PatchManager) AddFileLineRange(filename string, firstLineIdx, lastLineI return err } info.mode = PART - info.includedLineIndices = utils.UnionInt(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) + info.includedLineIndices = lo.Union(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) return nil } @@ -151,7 +151,7 @@ func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLi return err } info.mode = PART - info.includedLineIndices = utils.DifferenceInt(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) + info.includedLineIndices, _ = lo.Difference(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) if len(info.includedLineIndices) == 0 { p.removeFile(info) } diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go index c2be120c9..3810d8a29 100644 --- a/pkg/commands/patch/patch_parser.go +++ b/pkg/commands/patch/patch_parser.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -186,7 +187,7 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic renderedLines := make([]string, len(p.PatchLines)) for index, patchLine := range p.PatchLines { selected := index >= firstLineIndex && index <= lastLineIndex - included := utils.IncludesInt(incLineIndices, index) + included := lo.Contains(incLineIndices, index) renderedLines[index] = patchLine.render(selected, included) } result := strings.Join(renderedLines, "\n") diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 487c1e10b..c45e98d85 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -1,9 +1,11 @@ package controllers import ( + "github.com/jesseduffield/generics/list" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type GlobalController struct { @@ -36,9 +38,7 @@ func (self *GlobalController) customCommand() error { FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(), HandleConfirm: func(command string) error { self.c.GetAppState().CustomCommandsHistory = utils.Limit( - utils.Uniq( - append(self.c.GetAppState().CustomCommandsHistory, command), - ), + lo.Uniq(append(self.c.GetAppState().CustomCommandsHistory, command)), 1000, ) @@ -57,7 +57,7 @@ func (self *GlobalController) customCommand() error { func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { // reversing so that we display the latest command first - history := utils.Reverse(self.c.GetAppState().CustomCommandsHistory) + history := list.Reverse(self.c.GetAppState().CustomCommandsHistory) return helpers.FuzzySearchFunc(history) } diff --git a/pkg/gui/controllers/helpers/cherry_pick_helper.go b/pkg/gui/controllers/helpers/cherry_pick_helper.go index badbf0dfe..e4a9b3e81 100644 --- a/pkg/gui/controllers/helpers/cherry_pick_helper.go +++ b/pkg/gui/controllers/helpers/cherry_pick_helper.go @@ -1,11 +1,13 @@ package helpers import ( + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) type CherryPickHelper struct { @@ -63,13 +65,13 @@ func (self *CherryPickHelper) CopyRange(selectedIndex int, commitsList []*models return err } - commitShaMap := self.CherryPickedCommitShaMap() + commitSet := self.CherryPickedCommitShaSet() // find the last commit that is copied that's above our position // if there are none, startIndex = 0 startIndex := 0 for index, commit := range commitsList[0:selectedIndex] { - if commitShaMap[commit.Sha] { + if commitSet.Includes(commit.Sha) { startIndex = index } } @@ -105,25 +107,23 @@ func (self *CherryPickHelper) Reset() error { return self.rerender() } -func (self *CherryPickHelper) CherryPickedCommitShaMap() map[string]bool { - commitShaMap := map[string]bool{} - for _, commit := range self.getData().CherryPickedCommits { - commitShaMap[commit.Sha] = true - } - return commitShaMap +func (self *CherryPickHelper) CherryPickedCommitShaSet() *set.Set[string] { + shas := lo.Map(self.getData().CherryPickedCommits, func(commit *models.Commit, _ int) string { + return commit.Sha + }) + return set.NewFromSlice(shas) } func (self *CherryPickHelper) add(selectedCommit *models.Commit, commitsList []*models.Commit) { - commitShaMap := self.CherryPickedCommitShaMap() - commitShaMap[selectedCommit.Sha] = true - - newCommits := []*models.Commit{} - for _, commit := range commitsList { - if commitShaMap[commit.Sha] { - // duplicating just the things we need to put in the rebase TODO list - newCommits = append(newCommits, &models.Commit{Name: commit.Name, Sha: commit.Sha}) - } - } + commitSet := self.CherryPickedCommitShaSet() + commitSet.Add(selectedCommit.Sha) + + commitsInSet := lo.Filter(commitsList, func(commit *models.Commit, _ int) bool { + return commitSet.Includes(commit.Sha) + }) + newCommits := lo.Map(commitsInSet, func(commit *models.Commit, _ int) *models.Commit { + return &models.Commit{Name: commit.Name, Sha: commit.Sha} + }) self.getData().CherryPickedCommits = newCommits } diff --git a/pkg/gui/filetree/collapsed_paths.go b/pkg/gui/filetree/collapsed_paths.go index 02c0b4303..903999b37 100644 --- a/pkg/gui/filetree/collapsed_paths.go +++ b/pkg/gui/filetree/collapsed_paths.go @@ -1,20 +1,38 @@ package filetree -type CollapsedPaths map[string]bool +import "github.com/jesseduffield/generics/set" -func (cp CollapsedPaths) ExpandToPath(path string) { +type CollapsedPaths struct { + collapsedPaths *set.Set[string] +} + +func NewCollapsedPaths() *CollapsedPaths { + return &CollapsedPaths{ + collapsedPaths: set.New[string](), + } +} + +func (self *CollapsedPaths) ExpandToPath(path string) { // need every directory along the way splitPath := split(path) for i := range splitPath { dir := join(splitPath[0 : i+1]) - cp[dir] = false + self.collapsedPaths.Remove(dir) } } -func (cp CollapsedPaths) IsCollapsed(path string) bool { - return cp[path] +func (self *CollapsedPaths) IsCollapsed(path string) bool { + return self.collapsedPaths.Includes(path) +} + +func (self *CollapsedPaths) Collapse(path string) { + self.collapsedPaths.Add(path) } -func (cp CollapsedPaths) ToggleCollapsed(path string) { - cp[path] = !cp[path] +func (self *CollapsedPaths) ToggleCollapsed(path string) { + if self.collapsedPaths.Includes(path) { + self.collapsedPaths.Remove(path) + } else { + self.collapsedPaths.Add(path) + } } diff --git a/pkg/gui/filetree/commit_file_node.go b/pkg/gui/filetree/commit_file_node.go index a8f7d0a95..ac2057da5 100644 --- a/pkg/gui/filetree/commit_file_node.go +++ b/pkg/gui/filetree/commit_file_node.go @@ -100,7 +100,7 @@ func (s *CommitFileNode) EveryFile(test func(file *models.CommitFile) bool) bool }) } -func (n *CommitFileNode) Flatten(collapsedPaths map[string]bool) []*CommitFileNode { +func (n *CommitFileNode) Flatten(collapsedPaths *CollapsedPaths) []*CommitFileNode { results := flatten(n, collapsedPaths) nodes := make([]*CommitFileNode, len(results)) for i, result := range results { @@ -110,7 +110,7 @@ func (n *CommitFileNode) Flatten(collapsedPaths map[string]bool) []*CommitFileNo return nodes } -func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *CommitFileNode { +func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *CommitFileNode { if node == nil { return nil } @@ -124,11 +124,11 @@ func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths map[string] return result.(*CommitFileNode) } -func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { +func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) { return getIndexForPath(node, path, collapsedPaths) } -func (node *CommitFileNode) Size(collapsedPaths map[string]bool) int { +func (node *CommitFileNode) Size(collapsedPaths *CollapsedPaths) int { if node == nil { return 0 } diff --git a/pkg/gui/filetree/commit_file_tree.go b/pkg/gui/filetree/commit_file_tree.go index 055e273f3..e539c9dea 100644 --- a/pkg/gui/filetree/commit_file_tree.go +++ b/pkg/gui/filetree/commit_file_tree.go @@ -19,7 +19,7 @@ type CommitFileTree struct { tree *CommitFileNode showTree bool log *logrus.Entry - collapsedPaths CollapsedPaths + collapsedPaths *CollapsedPaths } var _ ICommitFileTree = &CommitFileTree{} @@ -29,7 +29,7 @@ func NewCommitFileTree(getFiles func() []*models.CommitFile, log *logrus.Entry, getFiles: getFiles, log: log, showTree: showTree, - collapsedPaths: CollapsedPaths{}, + collapsedPaths: NewCollapsedPaths(), } } @@ -88,7 +88,7 @@ func (self *CommitFileTree) Tree() INode { return self.tree } -func (self *CommitFileTree) CollapsedPaths() CollapsedPaths { +func (self *CommitFileTree) CollapsedPaths() *CollapsedPaths { return self.collapsedPaths } diff --git a/pkg/gui/filetree/file_node.go b/pkg/gui/filetree/file_node.go index 841f723fc..e73504321 100644 --- a/pkg/gui/filetree/file_node.go +++ b/pkg/gui/filetree/file_node.go @@ -87,7 +87,7 @@ func (s *FileNode) Any(test func(node *FileNode) bool) bool { }) } -func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode { +func (n *FileNode) Flatten(collapsedPaths *CollapsedPaths) []*FileNode { results := flatten(n, collapsedPaths) nodes := make([]*FileNode, len(results)) for i, result := range results { @@ -97,7 +97,7 @@ func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode { return nodes } -func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileNode { +func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *FileNode { if node == nil { return nil } @@ -111,11 +111,11 @@ func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) return result.(*FileNode) } -func (node *FileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { +func (node *FileNode) GetIndexForPath(path str |