Repository: jesseduffield/lazygit
File: pkg/gui/controllers/helpers/suggestions_helper.go (Go)

Code before:
		})
	})
}

func (self *SuggestionsHelper) getRemoteBranchNamesWithoutRemotePrefix() []string {
	return slices.FlatMap(self.model.Remotes, func(remote *models.Remote) []string {
		return slices.Map(remote.Branches, func(branch *models.RemoteBranch) string {
			return branch.Name
		})
	})
}

func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion {
	return FuzzySearchFunc(self.getRemoteBranchNames(separator))
}

func (self *SuggestionsHelper) GetRemoteBranchesWithoutRemotePrefixSuggestionsFunc() func(string) []*types.Suggestion {
	return FuzzySearchFunc(self.getRemoteBranchNamesWithoutRemotePrefix())
}

func (self *SuggestionsHelper) getTagNames() []string {
	return slices.Map(self.model.Tags, func(tag *models.Tag) string {
		return tag.Name
	})
}

func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Suggestion {
	remoteBranchNames := self.getRemoteBranchNames("/")
	localBranchNames := self.getBranchNames()
	tagNames := self.getTagNames()
	additionalRefNames := []string{"HEAD", "FETCH_HEAD", "MERGE_HEAD", "ORIG_HEAD"}

	refNames := append(append(append(remoteBranchNames, localBranchNames...), tagNames...), additionalRefNames...)

	return FuzzySearchFunc(refNames)
}

func FuzzySearchFunc(options []string) func(string) []*types.Suggestion {
	return func(input string) []*types.Suggestion {
		var matches []string
		if input == "" {
			matches = options
		} else {
			matches = utils.FuzzySearch(input, options)
		}

		return matchesToSuggestions(matches)
	}
}


Review comment (suggestion):
The implementation is very similar to `getRemoteBranchNames()`, but I haven't been able to come up with a nice way of reducing the code duplication. Hints welcome :)

Code after:

		return matchesToSuggestions(matchingNames)
	}
}

func (self *SuggestionsHelper) getRemoteBranchNames(separator string) []string {
	return lo.FlatMap(self.c.Model().Remotes, func(remote *models.Remote, _ int) []string {
		return lo.Map(remote.Branches, func(branch *models.RemoteBranch, _ int) string {
			return fmt.Sprintf("%s%s%s", remote.Name, separator, branch.Name)
		})
	})
}

func (self *SuggestionsHelper) getRemoteBranchNamesForRemote(remoteName string) []string {
	remote, ok := lo.Find(self.c.Model().Remotes, func(remote *models.Remote) bool {
		return remote.Name == remoteName
	})
	if ok {
		return lo.Map(remote.Branches, func(branch *models.RemoteBranch, _ int) string {
			return branch.Name
		})
	}
	return nil
}

func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion {
	return FilterFunc(self.getRemoteBranchNames(separator), self.c.UserConfig().Gui.UseFuzzySearch())
}

func (self *SuggestionsHelper) GetRemoteBranchesForRemoteSuggestionsFunc(remoteName string) func(string) []*types.Suggestion {
	return FilterFunc(self.getRemoteBranchNamesForRemote(remoteName), self.c.UserConfig().Gui.UseFuzzySearch())
}

func (self *SuggestionsHelper) getTagNames() []string {
	return lo.Map(self.c.Model().Tags, func(tag *models.Tag, _ int) string {
		return tag.Name
	})
}

func (self *SuggestionsHelper) GetTagsSuggestionsFunc() func(string) []*types.Suggestion {
	tagNames := self.getTagNames()

	return FilterFunc(tagNames, self.c.UserConfig().Gui.UseFuzzySearch())
}

func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Suggestion {
	remoteBranchNames := self.getRemoteBranchNames("/")
	localBranchNames := self.getBranchNames()
	tagNames := self.getTagNames()
	additionalRefNames := []string{"HEAD", "FETCH_HEAD", "MERGE_HEAD", "ORIG_HEAD"}