1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-06 03:53:59 +02:00
lazygit/pkg/gui/controllers/helpers/suggestions_helper.go

200 lines
6.4 KiB
Go
Raw Normal View History

2022-02-06 06:54:26 +02:00
package helpers
import (
2021-10-23 02:25:37 +02:00
"fmt"
"os"
2022-03-19 10:12:58 +02:00
"github.com/jesseduffield/generics/slices"
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 03:32:27 +02:00
"github.com/jesseduffield/gocui"
2022-03-19 10:12:58 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/jesseduffield/minimal/gitignore"
"github.com/samber/lo"
"gopkg.in/ozeidan/fuzzy-patricia.v3/patricia"
)
// Thinking out loud: I'm typically a staunch advocate of organising code by feature rather than type,
// because colocating code that relates to the same feature means far less effort
// to get all the context you need to work on any particular feature. But the one
// major benefit of grouping by type is that it makes it makes it less likely that
// somebody will re-implement the same logic twice, because they can quickly see
// if a certain method has been used for some use case, given that as a starting point
// they know about the type. In that vein, I'm including all our functions for
// finding suggestions in this file, so that it's easy to see if a function already
// exists for fetching a particular model.
2022-01-31 13:11:34 +02:00
type ISuggestionsHelper interface {
GetRemoteSuggestionsFunc() func(string) []*types.Suggestion
GetBranchNameSuggestionsFunc() func(string) []*types.Suggestion
GetFilePathSuggestionsFunc() func(string) []*types.Suggestion
GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion
GetRefsSuggestionsFunc() func(string) []*types.Suggestion
}
type SuggestionsHelper struct {
c *HelperCommon
}
2022-01-31 13:11:34 +02:00
var _ ISuggestionsHelper = &SuggestionsHelper{}
func NewSuggestionsHelper(
c *HelperCommon,
) *SuggestionsHelper {
return &SuggestionsHelper{
2023-03-23 03:53:18 +02:00
c: c,
}
}
func (self *SuggestionsHelper) getRemoteNames() []string {
2023-03-23 03:53:18 +02:00
return slices.Map(self.c.Model().Remotes, func(remote *models.Remote) string {
2022-03-19 10:12:58 +02:00
return remote.Name
})
}
func matchesToSuggestions(matches []string) []*types.Suggestion {
2022-03-19 10:12:58 +02:00
return slices.Map(matches, func(match string) *types.Suggestion {
return &types.Suggestion{
Value: match,
Label: match,
}
2022-03-19 10:12:58 +02:00
})
}
func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.Suggestion {
remoteNames := self.getRemoteNames()
2022-02-05 02:00:57 +02:00
return FuzzySearchFunc(remoteNames)
}
func (self *SuggestionsHelper) getBranchNames() []string {
2023-03-23 03:53:18 +02:00
return slices.Map(self.c.Model().Branches, func(branch *models.Branch) string {
2022-03-19 10:12:58 +02:00
return branch.Name
})
}
func (self *SuggestionsHelper) GetBranchNameSuggestionsFunc() func(string) []*types.Suggestion {
branchNames := self.getBranchNames()
return func(input string) []*types.Suggestion {
2021-10-23 02:25:37 +02:00
var matchingBranchNames []string
if input == "" {
matchingBranchNames = branchNames
} else {
matchingBranchNames = utils.FuzzySearch(input, branchNames)
}
2022-03-19 10:12:58 +02:00
return slices.Map(matchingBranchNames, func(branchName string) *types.Suggestion {
return &types.Suggestion{
Value: branchName,
Label: presentation.GetBranchTextStyle(branchName).Sprint(branchName),
}
2022-03-19 10:12:58 +02:00
})
}
}
// here we asynchronously fetch the latest set of paths in the repo and store in
2023-03-23 03:53:18 +02:00
// self.c.Model().FilesTrie. On the main thread we'll be doing a fuzzy search via
// self.c.Model().FilesTrie. So if we've looked for a file previously, we'll start with
// the old trie and eventually it'll be swapped out for the new one.
2021-10-23 02:25:37 +02:00
// Notably, unlike other suggestion functions we're not showing all the options
// if nothing has been typed because there'll be too much to display efficiently
func (self *SuggestionsHelper) GetFilePathSuggestionsFunc() func(string) []*types.Suggestion {
_ = self.c.WithWaitingStatus(self.c.Tr.LoadingFileSuggestions, func(gocui.Task) error {
trie := patricia.NewTrie()
// load every non-gitignored file in the repo
ignore, err := gitignore.FromGit()
if err != nil {
return err
}
err = ignore.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
trie.Insert(patricia.Prefix(path), path)
return nil
})
2022-01-31 13:11:34 +02:00
// cache the trie for future use
2023-03-23 03:53:18 +02:00
self.c.Model().FilesTrie = trie
2023-03-23 03:53:18 +02:00
self.c.Contexts().Suggestions.RefreshSuggestions()
return err
})
return func(input string) []*types.Suggestion {
matchingNames := []string{}
2023-03-23 03:53:18 +02:00
_ = self.c.Model().FilesTrie.VisitFuzzy(patricia.Prefix(input), true, func(prefix patricia.Prefix, item patricia.Item, skipped int) error {
matchingNames = append(matchingNames, item.(string))
return nil
})
// doing another fuzzy search for good measure
matchingNames = utils.FuzzySearch(input, matchingNames)
2022-03-19 10:12:58 +02:00
return matchesToSuggestions(matchingNames)
}
}
2021-10-23 02:25:37 +02:00
func (self *SuggestionsHelper) getRemoteBranchNames(separator string) []string {
2023-03-23 03:53:18 +02:00
return slices.FlatMap(self.c.Model().Remotes, func(remote *models.Remote) []string {
2022-03-19 10:12:58 +02:00
return slices.Map(remote.Branches, func(branch *models.RemoteBranch) string {
return fmt.Sprintf("%s%s%s", remote.Name, separator, branch.Name)
})
})
2021-10-23 02:25:37 +02:00
}
func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion {
2022-02-05 02:00:57 +02:00
return FuzzySearchFunc(self.getRemoteBranchNames(separator))
2021-10-23 02:25:37 +02:00
}
func (self *SuggestionsHelper) getTagNames() []string {
2023-03-23 03:53:18 +02:00
return slices.Map(self.c.Model().Tags, func(tag *models.Tag) string {
2022-03-19 10:12:58 +02:00
return tag.Name
})
2021-10-23 02:25:37 +02:00
}
func (self *SuggestionsHelper) GetTagsSuggestionsFunc() func(string) []*types.Suggestion {
tagNames := self.getTagNames()
return FuzzySearchFunc(tagNames)
}
func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Suggestion {
remoteBranchNames := self.getRemoteBranchNames("/")
localBranchNames := self.getBranchNames()
tagNames := self.getTagNames()
2021-10-23 02:25:37 +02:00
additionalRefNames := []string{"HEAD", "FETCH_HEAD", "MERGE_HEAD", "ORIG_HEAD"}
refNames := append(append(append(remoteBranchNames, localBranchNames...), tagNames...), additionalRefNames...)
2022-02-05 02:00:57 +02:00
return FuzzySearchFunc(refNames)
2021-10-23 02:25:37 +02:00
}
func (self *SuggestionsHelper) GetAuthorsSuggestionsFunc() func(string) []*types.Suggestion {
authors := lo.Map(lo.Values(self.c.Model().Authors), func(author *models.Author, _ int) string {
return author.Combined()
})
slices.Sort(authors)
return FuzzySearchFunc(authors)
}
2022-02-05 02:00:57 +02:00
func FuzzySearchFunc(options []string) func(string) []*types.Suggestion {
2021-10-23 02:25:37 +02:00
return func(input string) []*types.Suggestion {
var matches []string
if input == "" {
matches = options
} else {
matches = utils.FuzzySearch(input, options)
}
return matchesToSuggestions(matches)
}
}