summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-10-18 19:00:03 +1100
committerJesse Duffield <jessedduffield@gmail.com>2021-10-19 09:02:42 +1100
commitca7252ef8ee26affdc2c74f05c9c20196a8d571b (patch)
tree7323ca472558a435f01ae62e123cc2576e1959b3 /pkg
parenta496858c629e3c6241b51cf99cd38d6fa1b787bc (diff)
suggest files when picking a path to filter on
async fetching of suggestions remove limit cache the trie for future use more more
Diffstat (limited to 'pkg')
-rw-r--r--pkg/gui/confirmation_panel.go2
-rw-r--r--pkg/gui/editors.go6
-rw-r--r--pkg/gui/filtering.go66
-rw-r--r--pkg/gui/filtering_menu_panel.go3
-rw-r--r--pkg/gui/gui.go34
-rw-r--r--pkg/i18n/english.go2
-rw-r--r--pkg/tasks/async_handler.go56
-rw-r--r--pkg/tasks/async_handler_test.go44
8 files changed, 196 insertions, 17 deletions
diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go
index d3e1dac18..6664f765d 100644
--- a/pkg/gui/confirmation_panel.go
+++ b/pkg/gui/confirmation_panel.go
@@ -189,7 +189,7 @@ func (gui *Gui) prepareConfirmationPanel(title, prompt string, hasLoader bool, f
if err != nil {
return err
}
- suggestionsView.Wrap = true
+ suggestionsView.Wrap = false
suggestionsView.FgColor = theme.GocuiDefaultTextColor
gui.setSuggestions([]*types.Suggestion{})
suggestionsView.Visible = true
diff --git a/pkg/gui/editors.go b/pkg/gui/editors.go
index b94e6b5bd..430dcfdfe 100644
--- a/pkg/gui/editors.go
+++ b/pkg/gui/editors.go
@@ -77,8 +77,10 @@ func (gui *Gui) defaultEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.M
if gui.findSuggestions != nil {
input := v.TextArea.GetContent()
- suggestions := gui.findSuggestions(input)
- gui.setSuggestions(suggestions)
+ gui.suggestionsAsyncHandler.Do(func() func() {
+ suggestions := gui.findSuggestions(input)
+ return func() { gui.setSuggestions(suggestions) }
+ })
}
return matched
diff --git a/pkg/gui/filtering.go b/pkg/gui/filtering.go
index 1f5c5032a..17fd44e23 100644
--- a/pkg/gui/filtering.go
+++ b/pkg/gui/filtering.go
@@ -1,5 +1,14 @@
package gui
+import (
+ "os"
+
+ "github.com/jesseduffield/lazygit/pkg/gui/types"
+ "github.com/jesseduffield/lazygit/pkg/utils"
+ "github.com/jesseduffield/minimal/gitignore"
+ "gopkg.in/ozeidan/fuzzy-patricia.v3/patricia"
+)
+
func (gui *Gui) validateNotInFilterMode() (bool, error) {
if gui.State.Modes.Filtering.Active() {
err := gui.ask(askOpts{
@@ -40,3 +49,60 @@ func (gui *Gui) setFiltering(path string) error {
gui.State.Contexts.BranchCommits.GetPanelState().SetSelectedLineIdx(0)
}})
}
+
+// here we asynchronously fetch the latest set of paths in the repo and store in
+// gui.State.FilesTrie. On the main thread we'll be doing a fuzzy search via
+// gui.State.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.
+func (gui *Gui) getFindSuggestionsForFilterPath() func(string) []*types.Suggestion {
+ _ = gui.WithWaitingStatus(gui.Tr.LcLoadingFileSuggestions, func() 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
+ })
+ // cache the trie for future use
+ gui.State.FilesTrie = trie
+
+ // refresh the selections view
+ gui.suggestionsAsyncHandler.Do(func() func() {
+ // assuming here that the confirmation view is what we're typing into.
+ // This assumption may prove false over time
+ suggestions := gui.findSuggestions(gui.Views.Confirmation.TextArea.GetContent())
+ return func() { gui.setSuggestions(suggestions) }
+ })
+
+ return err
+ })
+
+ return func(input string) []*types.Suggestion {
+ matchingNames := []string{}
+ _ = gui.State.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)
+
+ suggestions := make([]*types.Suggestion, len(matchingNames))
+ for i, name := range matchingNames {
+ suggestions[i] = &types.Suggestion{
+ Value: name,
+ Label: name,
+ }
+ }
+
+ return suggestions
+ }
+}
diff --git a/pkg/gui/filtering_menu_panel.go b/pkg/gui/filtering_menu_panel.go
index 649172b45..bd17291ae 100644
--- a/pkg/gui/filtering_menu_panel.go
+++ b/pkg/gui/filtering_menu_panel.go
@@ -35,7 +35,8 @@ func (gui *Gui) handleCreateFilteringMenuPanel() error {
displayString: gui.Tr.LcFilterPathOption,
onPress: func() error {
return gui.prompt(promptOpts{
- title: gui.Tr.LcEnterFileName,
+ findSuggestionsFunc: gui.getFindSuggestionsForFilterPath(),
+ title: gui.Tr.LcEnterFileName,
handleConfirm: func(response string) error {
return gui.setFiltering(strings.TrimSpace(response))
},
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go
index 95c165a71..1349920e5 100644
--- a/pkg/gui/gui.go
+++ b/pkg/gui/gui.go
@@ -30,6 +30,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/updates"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
+ "gopkg.in/ozeidan/fuzzy-patricia.v3/patricia"
)
// screen sizing determines how much space your selected window takes up (window
@@ -116,6 +117,8 @@ type Gui struct {
// the extras window contains things like the command log
ShowExtrasWindow bool
+
+ suggestionsAsyncHandler *tasks.AsyncHandler
}
type listPanelState struct {
@@ -336,6 +339,9 @@ type guiState struct {
// flag as to whether or not the diff view should ignore whitespace
IgnoreWhitespaceInDiffView bool
+
+ // for displaying suggestions while typing in a file name
+ FilesTrie *patricia.Trie
}
// reuseState determines if we pull the repo state from our repo state map or
@@ -412,6 +418,7 @@ func (gui *Gui) resetState(filterPath string, reuseState bool) {
// TODO: put contexts in the context manager
ContextManager: NewContextManager(initialContext),
Contexts: contexts,
+ FilesTrie: patricia.NewTrie(),
}
gui.RepoStateMap[Repo(currentDir)] = gui.State
@@ -421,19 +428,20 @@ func (gui *Gui) resetState(filterPath string, reuseState bool) {
// NewGui builds a new gui handler
func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscommands.OSCommand, tr *i18n.TranslationSet, config config.AppConfigurer, updater *updates.Updater, filterPath string, showRecentRepos bool) (*Gui, error) {
gui := &Gui{
- Log: log,
- GitCommand: gitCommand,
- OSCommand: oSCommand,
- Config: config,
- Tr: tr,
- Updater: updater,
- statusManager: &statusManager{},
- viewBufferManagerMap: map[string]*tasks.ViewBufferManager{},
- showRecentRepos: showRecentRepos,
- RepoPathStack: []string{},
- RepoStateMap: map[Repo]*guiState{},
- CmdLog: []string{},
- ShowExtrasWindow: config.GetUserConfig().Gui.ShowCommandLog,
+ Log: log,
+ GitCommand: gitCommand,
+ OSCommand: oSCommand,
+ Config: config,
+ Tr: tr,
+ Updater: updater,
+ statusManager: &statusManager{},
+ viewBufferManagerMap: map[string]*tasks.ViewBufferManager{},
+ showRecentRepos: showRecentRepos,
+ RepoPathStack: []string{},
+ RepoStateMap: map[Repo]*guiState{},
+ CmdLog: []string{},
+ ShowExtrasWindow: config.GetUserConfig().Gui.ShowCommandLog,
+ suggestionsAsyncHandler: tasks.NewAsyncHandler(),
}
gui.resetState(filterPath, false)
diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go
index 1156cfee7..e3712b7b5 100644
--- a/pkg/i18n/english.go
+++ b/pkg/i18n/english.go
@@ -430,6 +430,7 @@ type TranslationSet struct {
CreatingPullRequestAtUrl string
SelectConfigFile string
NoConfigFileFoundErr string
+ LcLoadingFileSuggestions string
Spans Spans
}
@@ -954,6 +955,7 @@ func englishTranslationSet() TranslationSet {
CreatingPullRequestAtUrl: "Creating pull request at URL: %s",
SelectConfigFile: "Select config file",
NoConfigFileFoundErr: "No config file found",
+ LcLoadingFileSuggestions: "loading file suggestions",
Spans: Spans{
// TODO: combine this with the original keybinding descriptions (those are all in lowercase atm)
CheckoutCommit: "Checkout commit",
diff --git a/pkg/tasks/async_handler.go b/pkg/tasks/async_handler.go
new file mode 100644
index 000000000..897efb0e2
--- /dev/null
+++ b/pkg/tasks/async_handler.go
@@ -0,0 +1,56 @@
+package tasks
+
+import (
+ "sync"
+
+ "github.com/jesseduffield/lazygit/pkg/utils"
+)
+
+// the purpose of an AsyncHandler is to ensure that if we have multiple long-running
+// requests, we only handle the result of the latest one. For example, if I am
+// searching for 'abc' and I have to type 'a' then 'b' then 'c' and each keypress
+// dispatches a request to search for things with the string so-far, we'll be searching
+// for 'a', 'ab', and 'abc', and it may be that 'abc' comes back first, then 'ab',
+// then 'a' and we don't want to display the result for 'a' just because it came
+// back last. AsyncHandler keeps track of the order in which things were dispatched
+// so that we can ignore anything that comes back late.
+type AsyncHandler struct {
+ currentId int
+ lastId int
+ mutex sync.Mutex
+ onReject func()
+}
+
+func NewAsyncHandler() *AsyncHandler {
+ return &AsyncHandler{
+ mutex: sync.Mutex{},
+ }
+}
+
+func (self *AsyncHandler) Do(f func() func()) {
+ self.mutex.Lock()
+ self.currentId++
+ id := self.currentId
+ self.mutex.Unlock()
+
+ go utils.Safe(func() {
+ after := f()
+ self.handle(after, id)
+ })
+}
+
+// f here is expected to be a function that doesn't take long to run
+func (self *AsyncHandler) handle(f func(), id int) {
+ self.mutex.Lock()
+ defer self.mutex.Unlock()
+
+ if id < self.lastId {
+ if self.onReject != nil {
+ self.onReject()
+ }
+ return
+ }
+
+ self.lastId = id
+ f()
+}
diff --git a/pkg/tasks/async_handler_test.go b/pkg/tasks/async_handler_test.go
new file mode 100644
index 000000000..b6edbec20
--- /dev/null
+++ b/pkg/tasks/async_handler_test.go
@@ -0,0 +1,44 @@
+package tasks
+
+import (
+ "fmt"
+ "sync"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestAsyncHandler(t *testing.T) {
+ wg := sync.WaitGroup{}
+ wg.Add(2)
+
+ handler := NewAsyncHandler()
+ handler.onReject = func() {
+ wg.Done()
+ }
+
+ result := 0
+
+ wg2 := sync.WaitGroup{}
+ wg2.Add(1)
+
+ handler.Do(func() func() {
+ wg2.Wait()
+ return func() {
+ fmt.Println("setting to 1")
+ result = 1
+ }
+ })
+ handler.Do(func() func() {
+ return func() {
+ fmt.Println("setting to 2")
+ result = 2
+ wg.Done()
+ wg2.Done()
+ }
+ })
+
+ wg.Wait()
+
+ assert.EqualValues(t, 2, result)
+}