Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-apply filter when model changes #3058

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/gui/context/filtered_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (self *FilteredList[T]) ClearFilter() {
self.SetFilter("")
}

func (self *FilteredList[T]) ReApplyFilter() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we expose the already existing applyFilter instead? As an alternative, what about naming it InvalidateModel? I'm trying to reduce the amount of redundant code or justifying it for extensibility. Does that make sense to you?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing applyFilter publicly would be fine with me (I'd rename it to ReApplyFilter then), but on the other hand the one-line forwarder doesn't bother me much. So no strong opinion here.

I wouldn't rename it to InvalidateModel though, this would sound misleading to me. Maybe OnModelInvalidated, but I'm not sure why that's better.

The other question is more interesting: should we move some of the logic specific to contexts (e.g. selecting lines etc) from SearchHelper to FilteredListViewModel. The same could be asked for the existing code in OnPromptContentChanged. I don't really have much of an opinion here, happy to follow whatever Jesse tells me to do. Personally I'd leave everything the way it is, as that's the least amount of work. 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to leave things where they are for now, but happy to accept a separate PR that does the refactor if anybody's up to it

self.applyFilter()
}

func (self *FilteredList[T]) IsFiltering() bool {
return self.filter != ""
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/gui/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (gui *Gui) resetHelpersAndControllers() {
patchBuildingHelper := helpers.NewPatchBuildingHelper(helperCommon)
stagingHelper := helpers.NewStagingHelper(helperCommon)
mergeConflictsHelper := helpers.NewMergeConflictsHelper(helperCommon)
searchHelper := helpers.NewSearchHelper(helperCommon)

refreshHelper := helpers.NewRefreshHelper(
helperCommon,
Expand All @@ -56,6 +57,7 @@ func (gui *Gui) resetHelpersAndControllers() {
stagingHelper,
mergeConflictsHelper,
worktreeHelper,
searchHelper,
)
diffHelper := helpers.NewDiffHelper(helperCommon)
cherryPickHelper := helpers.NewCherryPickHelper(
Expand Down Expand Up @@ -119,7 +121,7 @@ func (gui *Gui) resetHelpersAndControllers() {
modeHelper,
appStatusHelper,
),
Search: helpers.NewSearchHelper(helperCommon),
Search: searchHelper,
Worktree: worktreeHelper,
SubCommits: helpers.NewSubCommitsHelper(helperCommon, refreshHelper, setSubCommits),
}
Expand Down
38 changes: 23 additions & 15 deletions pkg/gui/controllers/helpers/refresh_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type RefreshHelper struct {
stagingHelper *StagingHelper
mergeConflictsHelper *MergeConflictsHelper
worktreeHelper *WorktreeHelper
searchHelper *SearchHelper
}

func NewRefreshHelper(
Expand All @@ -38,6 +39,7 @@ func NewRefreshHelper(
stagingHelper *StagingHelper,
mergeConflictsHelper *MergeConflictsHelper,
worktreeHelper *WorktreeHelper,
searchHelper *SearchHelper,
) *RefreshHelper {
return &RefreshHelper{
c: c,
Expand All @@ -47,6 +49,7 @@ func NewRefreshHelper(
stagingHelper: stagingHelper,
mergeConflictsHelper: mergeConflictsHelper,
worktreeHelper: worktreeHelper,
searchHelper: searchHelper,
}
}

Expand Down Expand Up @@ -328,7 +331,7 @@ func (self *RefreshHelper) refreshCommitsWithLimit() error {
self.c.Model().WorkingTreeStateAtLastCommitRefresh = self.c.Git().Status.WorkingTreeState()
self.c.Model().CheckedOutBranch = checkedOutBranchName

return self.c.PostRefreshUpdate(self.c.Contexts().LocalCommits)
return self.refreshView(self.c.Contexts().LocalCommits)
}

func (self *RefreshHelper) refreshSubCommitsWithLimit() error {
Expand All @@ -351,7 +354,7 @@ func (self *RefreshHelper) refreshSubCommitsWithLimit() error {
self.c.Model().SubCommits = commits
self.RefreshAuthors(commits)

return self.c.PostRefreshUpdate(self.c.Contexts().SubCommits)
return self.refreshView(self.c.Contexts().SubCommits)
}

func (self *RefreshHelper) RefreshAuthors(commits []*models.Commit) {
Expand Down Expand Up @@ -381,7 +384,7 @@ func (self *RefreshHelper) refreshCommitFilesContext() error {
self.c.Model().CommitFiles = files
self.c.Contexts().CommitFiles.CommitFileTreeViewModel.SetTree()

return self.c.PostRefreshUpdate(self.c.Contexts().CommitFiles)
return self.refreshView(self.c.Contexts().CommitFiles)
}

func (self *RefreshHelper) refreshRebaseCommits() error {
Expand All @@ -395,7 +398,7 @@ func (self *RefreshHelper) refreshRebaseCommits() error {
self.c.Model().Commits = updatedCommits
self.c.Model().WorkingTreeStateAtLastCommitRefresh = self.c.Git().Status.WorkingTreeState()

return self.c.PostRefreshUpdate(self.c.Contexts().LocalCommits)
return self.refreshView(self.c.Contexts().LocalCommits)
}

func (self *RefreshHelper) refreshTags() error {
Expand All @@ -406,7 +409,7 @@ func (self *RefreshHelper) refreshTags() error {

self.c.Model().Tags = tags

return self.c.PostRefreshUpdate(self.c.Contexts().Tags)
return self.refreshView(self.c.Contexts().Tags)
}

func (self *RefreshHelper) refreshStateSubmoduleConfigs() error {
Expand Down Expand Up @@ -448,12 +451,12 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool) {

if refreshWorktrees {
self.loadWorktrees()
if err := self.c.PostRefreshUpdate(self.c.Contexts().Worktrees); err != nil {
if err := self.refreshView(self.c.Contexts().Worktrees); err != nil {
self.c.Log.Error(err)
}
}

if err := self.c.PostRefreshUpdate(self.c.Contexts().Branches); err != nil {
if err := self.refreshView(self.c.Contexts().Branches); err != nil {
self.c.Log.Error(err)
}

Expand Down Expand Up @@ -485,11 +488,11 @@ func (self *RefreshHelper) refreshFilesAndSubmodules() error {
}

self.c.OnUIThread(func() error {
if err := self.c.PostRefreshUpdate(self.c.Contexts().Submodules); err != nil {
if err := self.refreshView(self.c.Contexts().Submodules); err != nil {
self.c.Log.Error(err)
}

if err := self.c.PostRefreshUpdate(self.c.Contexts().Files); err != nil {
if err := self.refreshView(self.c.Contexts().Files); err != nil {
self.c.Log.Error(err)
}

Expand Down Expand Up @@ -611,7 +614,7 @@ func (self *RefreshHelper) refreshReflogCommits() error {
model.FilteredReflogCommits = model.ReflogCommits
}

return self.c.PostRefreshUpdate(self.c.Contexts().ReflogCommits)
return self.refreshView(self.c.Contexts().ReflogCommits)
}

func (self *RefreshHelper) refreshRemotes() error {
Expand All @@ -635,11 +638,11 @@ func (self *RefreshHelper) refreshRemotes() error {
}
}

if err := self.c.PostRefreshUpdate(self.c.Contexts().Remotes); err != nil {
if err := self.refreshView(self.c.Contexts().Remotes); err != nil {
return err
}

if err := self.c.PostRefreshUpdate(self.c.Contexts().RemoteBranches); err != nil {
if err := self.refreshView(self.c.Contexts().RemoteBranches); err != nil {
return err
}

Expand All @@ -661,18 +664,18 @@ func (self *RefreshHelper) refreshWorktrees() error {

// need to refresh branches because the branches view shows worktrees against
// branches
if err := self.c.PostRefreshUpdate(self.c.Contexts().Branches); err != nil {
if err := self.refreshView(self.c.Contexts().Branches); err != nil {
return err
}

return self.c.PostRefreshUpdate(self.c.Contexts().Worktrees)
return self.refreshView(self.c.Contexts().Worktrees)
}

func (self *RefreshHelper) refreshStashEntries() error {
self.c.Model().StashEntries = self.c.Git().Loaders.StashLoader.
GetStashEntries(self.c.Modes().Filtering.GetPath())

return self.c.PostRefreshUpdate(self.c.Contexts().Stash)
return self.refreshView(self.c.Contexts().Stash)
}

// never call this on its own, it should only be called from within refreshCommits()
Expand Down Expand Up @@ -711,3 +714,8 @@ func (self *RefreshHelper) refForLog() string {

return bisectInfo.GetStartSha()
}

func (self *RefreshHelper) refreshView(context types.Context) error {
self.searchHelper.ReApplyFilter(context)
return self.c.PostRefreshUpdate(context)
}
12 changes: 12 additions & 0 deletions pkg/gui/controllers/helpers/search_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ func (self *SearchHelper) OnPromptContentChanged(searchString string) {
}
}

func (self *SearchHelper) ReApplyFilter(context types.Context) {
state := self.searchState()
if context == state.Context {
filterableContext, ok := context.(types.IFilterableContext)
if ok {
filterableContext.SetSelectedLineIdx(0)
_ = filterableContext.GetView().SetOriginY(0)
filterableContext.ReApplyFilter()
Comment on lines +235 to +237
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we rename the ReApplyFilter method on the IFilterableContext, it would probably make sense to move all this logic inside it. Also, would it make sense, in your opinion, to reset the filter in case of an empty resulting list? I'm not sure, but it's an option.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not reset the filter because there may intermittently be an empty resulting list (I'm thinking of the weirdness that happens when refreshing the files view for example)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The files view is not filterable (yet?), so this wouldn't be an issue right now. But I agree, the behavior could be strange in other cases; say you filter the branches list, type so much that no branch shows up, and then the background fetch comes along and refreshes the branches.

}
}
}

func (self *SearchHelper) RenderSearchStatus(c types.Context) {
if c.GetKey() == context.SEARCH_CONTEXT_KEY {
return
Expand Down
1 change: 1 addition & 0 deletions pkg/gui/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type IFilterableContext interface {
SetFilter(string)
GetFilter() string
ClearFilter()
ReApplyFilter()
IsFiltering() bool
IsFilterableContext()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package filter_and_search

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var FilterUpdatesWhenModelChanges = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Verify that after deleting a branch the filter is reapplied to show only the remaining branches",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.EmptyCommit("first commit")
shell.NewBranch("branch-to-delete")
shell.NewBranch("other")
shell.NewBranch("checked-out-branch")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Focus().
Lines(
Contains("checked-out-branch").IsSelected(),
Contains("other"),
Contains("branch-to-delete"),
Contains("master"),
).
FilterOrSearch("branch").
Lines(
Contains("branch-to-delete").IsSelected(),
Contains("checked-out-branch"),
).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().
Menu().
Title(Equals("Delete branch 'branch-to-delete'?")).
Select(Contains("Delete local branch")).
Confirm()
}).
Lines(
Contains("checked-out-branch").IsSelected(),
).
// cancel the filter
PressEscape().
Lines(
Contains("checked-out-branch").IsSelected(),
Contains("other"),
Contains("master"),
)
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var tests = []*components.IntegrationTest{
filter_and_search.FilterRemoteBranches,
filter_and_search.FilterRemotes,
filter_and_search.FilterSearchHistory,
filter_and_search.FilterUpdatesWhenModelChanges,
filter_and_search.NestedFilter,
filter_and_search.NestedFilterTransient,
filter_and_search.NewSearch,
Expand Down