summaryrefslogtreecommitdiffstats
path: root/pkg/gui
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2024-03-10 10:58:19 +0100
committerStefan Haller <stefan@haller-berlin.de>2024-03-17 11:55:09 +0100
commita8797c72617b3ace7ce7c1c0eab469497e40b774 (patch)
tree6370fb8baef8b90da53b5d3b366d83d9b4ee2c37 /pkg/gui
parenta82e26d11e4c937c8ca2fdef92bb75ae912e7b44 (diff)
Default to substring filtering, add option to go back to fuzzy filtering
By default we now search for substrings; you can search for multiple substrings by separating them with spaces. Add a config option gui.filterMode that can be set to 'fuzzy' to switch back to the previous behavior.
Diffstat (limited to 'pkg/gui')
-rw-r--r--pkg/gui/context/filtered_list.go16
-rw-r--r--pkg/gui/controllers/custom_command_action.go2
-rw-r--r--pkg/gui/controllers/helpers/search_helper.go4
-rw-r--r--pkg/gui/controllers/helpers/suggestions_helper.go42
-rw-r--r--pkg/gui/types/context.go4
5 files changed, 41 insertions, 27 deletions
diff --git a/pkg/gui/context/filtered_list.go b/pkg/gui/context/filtered_list.go
index 13b9c166a..8ac912cd0 100644
--- a/pkg/gui/context/filtered_list.go
+++ b/pkg/gui/context/filtered_list.go
@@ -39,18 +39,18 @@ func (self *FilteredList[T]) GetFilter() string {
return self.filter
}
-func (self *FilteredList[T]) SetFilter(filter string) {
+func (self *FilteredList[T]) SetFilter(filter string, useFuzzySearch bool) {
self.filter = filter
- self.applyFilter()
+ self.applyFilter(useFuzzySearch)
}
func (self *FilteredList[T]) ClearFilter() {
- self.SetFilter("")
+ self.SetFilter("", false)
}
-func (self *FilteredList[T]) ReApplyFilter() {
- self.applyFilter()
+func (self *FilteredList[T]) ReApplyFilter(useFuzzySearch bool) {
+ self.applyFilter(useFuzzySearch)
}
func (self *FilteredList[T]) IsFiltering() bool {
@@ -84,7 +84,7 @@ func (self *fuzzySource[T]) Len() int {
return len(self.list)
}
-func (self *FilteredList[T]) applyFilter() {
+func (self *FilteredList[T]) applyFilter(useFuzzySearch bool) {
self.mutex.Lock()
defer self.mutex.Unlock()
@@ -96,11 +96,11 @@ func (self *FilteredList[T]) applyFilter() {
getFilterFields: self.getFilterFields,
}
- matches := fuzzy.FindFrom(self.filter, source)
+ matches := utils.FindFrom(self.filter, source, useFuzzySearch)
self.filteredIndices = lo.Map(matches, func(match fuzzy.Match, _ int) int {
return match.Index
})
- if self.shouldRetainSortOrder != nil && self.shouldRetainSortOrder() {
+ if useFuzzySearch && self.shouldRetainSortOrder != nil && self.shouldRetainSortOrder() {
slices.Sort(self.filteredIndices)
}
}
diff --git a/pkg/gui/controllers/custom_command_action.go b/pkg/gui/controllers/custom_command_action.go
index bc595934d..16d811aff 100644
--- a/pkg/gui/controllers/custom_command_action.go
+++ b/pkg/gui/controllers/custom_command_action.go
@@ -38,7 +38,7 @@ func (self *CustomCommandAction) Call() error {
func (self *CustomCommandAction) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion {
history := self.c.GetAppState().CustomCommandsHistory
- return helpers.FuzzySearchFunc(history)
+ return helpers.FuzzySearchFunc(history, self.c.UserConfig.Gui.UseFuzzySearch())
}
// this mimics the shell functionality `ignorespace`
diff --git a/pkg/gui/controllers/helpers/search_helper.go b/pkg/gui/controllers/helpers/search_helper.go
index 9ceea2f90..c31720949 100644
--- a/pkg/gui/controllers/helpers/search_helper.go
+++ b/pkg/gui/controllers/helpers/search_helper.go
@@ -218,7 +218,7 @@ func (self *SearchHelper) OnPromptContentChanged(searchString string) {
case types.IFilterableContext:
context.SetSelection(0)
_ = context.GetView().SetOriginY(0)
- context.SetFilter(searchString)
+ context.SetFilter(searchString, self.c.UserConfig.Gui.UseFuzzySearch())
_ = self.c.PostRefreshUpdate(context)
case types.ISearchableContext:
// do nothing
@@ -234,7 +234,7 @@ func (self *SearchHelper) ReApplyFilter(context types.Context) {
if ok {
filterableContext.SetSelection(0)
_ = filterableContext.GetView().SetOriginY(0)
- filterableContext.ReApplyFilter()
+ filterableContext.ReApplyFilter(self.c.UserConfig.Gui.UseFuzzySearch())
}
}
}
diff --git a/pkg/gui/controllers/helpers/suggestions_helper.go b/pkg/gui/controllers/helpers/suggestions_helper.go
index 2ae9d2158..91f42df8c 100644
--- a/pkg/gui/controllers/helpers/suggestions_helper.go
+++ b/pkg/gui/controllers/helpers/suggestions_helper.go
@@ -3,6 +3,7 @@ package helpers
import (
"fmt"
"os"
+ "strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
@@ -65,7 +66,7 @@ func matchesToSuggestions(matches []string) []*types.Suggestion {
func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.Suggestion {
remoteNames := self.getRemoteNames()
- return FuzzySearchFunc(remoteNames)
+ return FuzzySearchFunc(remoteNames, self.c.UserConfig.Gui.UseFuzzySearch())
}
func (self *SuggestionsHelper) getBranchNames() []string {
@@ -82,7 +83,7 @@ func (self *SuggestionsHelper) GetBranchNameSuggestionsFunc() func(string) []*ty
if input == "" {
matchingBranchNames = branchNames
} else {
- matchingBranchNames = utils.FuzzySearch(input, branchNames)
+ matchingBranchNames = utils.FuzzySearch(input, branchNames, self.c.UserConfig.Gui.UseFuzzySearch())
}
return lo.Map(matchingBranchNames, func(branchName string, _ int) *types.Suggestion {
@@ -128,13 +129,26 @@ func (self *SuggestionsHelper) GetFilePathSuggestionsFunc() func(string) []*type
return func(input string) []*types.Suggestion {
matchingNames := []string{}
- _ = 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
- })
+ if self.c.UserConfig.Gui.UseFuzzySearch() {
+ _ = 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)
+ // doing another fuzzy search for good measure
+ matchingNames = utils.FuzzySearch(input, matchingNames, true)
+ } else {
+ substrings := strings.Fields(input)
+ _ = self.c.Model().FilesTrie.Visit(func(prefix patricia.Prefix, item patricia.Item) error {
+ for _, sub := range substrings {
+ if !utils.CaseAwareContains(item.(string), sub) {
+ return nil
+ }
+ }
+ matchingNames = append(matchingNames, item.(string))
+ return nil
+ })
+ }
return matchesToSuggestions(matchingNames)
}
@@ -149,7 +163,7 @@ func (self *SuggestionsHelper) getRemoteBranchNames(separator string) []string {
}
func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion {
- return FuzzySearchFunc(self.getRemoteBranchNames(separator))
+ return FuzzySearchFunc(self.getRemoteBranchNames(separator), self.c.UserConfig.Gui.UseFuzzySearch())
}
func (self *SuggestionsHelper) getTagNames() []string {
@@ -161,7 +175,7 @@ func (self *SuggestionsHelper) getTagNames() []string {
func (self *SuggestionsHelper) GetTagsSuggestionsFunc() func(string) []*types.Suggestion {
tagNames := self.getTagNames()
- return FuzzySearchFunc(tagNames)
+ return FuzzySearchFunc(tagNames, self.c.UserConfig.Gui.UseFuzzySearch())
}
func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Suggestion {
@@ -172,7 +186,7 @@ func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Su
refNames := append(append(append(remoteBranchNames, localBranchNames...), tagNames...), additionalRefNames...)
- return FuzzySearchFunc(refNames)
+ return FuzzySearchFunc(refNames, self.c.UserConfig.Gui.UseFuzzySearch())
}
func (self *SuggestionsHelper) GetAuthorsSuggestionsFunc() func(string) []*types.Suggestion {
@@ -182,16 +196,16 @@ func (self *SuggestionsHelper) GetAuthorsSuggestionsFunc() func(string) []*types
slices.Sort(authors)
- return FuzzySearchFunc(authors)
+ return FuzzySearchFunc(authors, self.c.UserConfig.Gui.UseFuzzySearch())
}
-func FuzzySearchFunc(options []string) func(string) []*types.Suggestion {
+func FuzzySearchFunc(options []string, useFuzzySearch bool) func(string) []*types.Suggestion {
return func(input string) []*types.Suggestion {
var matches []string
if input == "" {
matches = options
} else {
- matches = utils.FuzzySearch(input, options)
+ matches = utils.FuzzySearch(input, options, useFuzzySearch)
}
return matchesToSuggestions(matches)
diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go
index 92b07a729..bb57375f9 100644
--- a/pkg/gui/types/context.go
+++ b/pkg/gui/types/context.go
@@ -102,10 +102,10 @@ type IFilterableContext interface {
IListPanelState
ISearchHistoryContext
- SetFilter(string)
+ SetFilter(string, bool)
GetFilter() string
ClearFilter()
- ReApplyFilter()
+ ReApplyFilter(bool)
IsFiltering() bool
IsFilterableContext()
}