Skip to content

Commit

Permalink
make more use of generics
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Mar 24, 2022
1 parent dde30fa commit c7a629c
Show file tree
Hide file tree
Showing 52 changed files with 3,014 additions and 275 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/git_commands/rebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/loaders/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/loaders/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/patch/hunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
)

type PatchHunk struct {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/patch/patch_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sort"
"strings"

"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/patch/patch_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions pkg/gui/controllers/global_controller.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
)

Expand All @@ -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)
}
Expand Down
36 changes: 18 additions & 18 deletions pkg/gui/controllers/helpers/cherry_pick_helper.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down
32 changes: 25 additions & 7 deletions pkg/gui/filetree/collapsed_paths.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 4 additions & 4 deletions pkg/gui/filetree/commit_file_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/gui/filetree/commit_file_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type CommitFileTree struct {
tree *CommitFileNode
showTree bool
log *logrus.Entry
collapsedPaths CollapsedPaths
collapsedPaths *CollapsedPaths
}

var _ ICommitFileTree = &CommitFileTree{}
Expand All @@ -29,7 +29,7 @@ func NewCommitFileTree(getFiles func() []*models.CommitFile, log *logrus.Entry,
getFiles: getFiles,
log: log,
showTree: showTree,
collapsedPaths: CollapsedPaths{},
collapsedPaths: NewCollapsedPaths(),
}
}

Expand Down Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit c7a629c

Please sign in to comment.