diff options
author | DerTeta <derteta@gmx.de> | 2021-09-03 22:40:11 +0200 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-12-06 22:37:28 +1100 |
commit | e1cf6912db329b47f60071f58bb970a583cf3003 (patch) | |
tree | fb74d98a4a88712b41c17e220880e5d777817a1c | |
parent | c99d373e133d96927ef14700ba8d53365eeffcda (diff) |
Add the `IncreaseContextInDiffView` function
-rw-r--r-- | pkg/gui/diff_context_size.go | 16 | ||||
-rw-r--r-- | pkg/gui/diff_context_size_test.go | 82 |
2 files changed, 98 insertions, 0 deletions
diff --git a/pkg/gui/diff_context_size.go b/pkg/gui/diff_context_size.go new file mode 100644 index 000000000..3e6e6b4a2 --- /dev/null +++ b/pkg/gui/diff_context_size.go @@ -0,0 +1,16 @@ +package gui + +func isShowingDiff(gui *Gui) bool { + key := gui.currentStaticContext().GetKey() + + return key == FILES_CONTEXT_KEY || key == COMMIT_FILES_CONTEXT_KEY || key == STASH_CONTEXT_KEY || key == BRANCH_COMMITS_CONTEXT_KEY || key == SUB_COMMITS_CONTEXT_KEY || key == MAIN_STAGING_CONTEXT_KEY || key == MAIN_PATCH_BUILDING_CONTEXT_KEY +} + +func (gui *Gui) IncreaseContextInDiffView() error { + if isShowingDiff(gui) { + gui.Config.GetUserConfig().Git.DiffContextSize = gui.Config.GetUserConfig().Git.DiffContextSize + 1 + return gui.postRefreshUpdate(gui.currentStaticContext()) + } + + return nil +} diff --git a/pkg/gui/diff_context_size_test.go b/pkg/gui/diff_context_size_test.go new file mode 100644 index 000000000..61d0bc728 --- /dev/null +++ b/pkg/gui/diff_context_size_test.go @@ -0,0 +1,82 @@ +package gui + +import ( + "testing" + + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands/patch" + "github.com/stretchr/testify/assert" +) + +const diffForTest = `diff --git a/pkg/gui/diff_context_size.go b/pkg/gui/diff_context_size.go +index 0da0a982..742b7dcf 100644 +--- a/pkg/gui/diff_context_size.go ++++ b/pkg/gui/diff_context_size.go +@@ -9,12 +9,12 @@ func getRefreshFunction(gui *Gui) func()error { + } + } else if key == MAIN_STAGING_CONTEXT_KEY { + return func() error { +- selectedLine := gui.Views.Secondary.SelectedLineIdx() ++ selectedLine := gui.State.Panels.LineByLine.GetSelectedLineIdx() + return gui.handleRefreshStagingPanel(false, selectedLine) + } + } else if key == MAIN_PATCH_BUILDING_CONTEXT_KEY { +` + +func setupGuiForTest(gui *Gui) { + gui.g = &gocui.Gui{} + gui.Views.Main, _ = gui.prepareView("main") + gui.Views.Secondary, _ = gui.prepareView("secondary") + gui.GitCommand.PatchManager = &patch.PatchManager{} + gui.refreshLineByLinePanel(diffForTest, "", false, 11) +} + +func TestIncreasesContextInDiffViewByOneInContextWithDiff(t *testing.T) { + contexts := []func(gui *Gui) Context { + func(gui *Gui) Context { return gui.State.Contexts.Files }, + func(gui *Gui) Context { return gui.State.Contexts.BranchCommits }, + func(gui *Gui) Context { return gui.State.Contexts.CommitFiles }, + func(gui *Gui) Context { return gui.State.Contexts.Stash }, + func(gui *Gui) Context { return gui.State.Contexts.Staging }, + func(gui *Gui) Context { return gui.State.Contexts.PatchBuilding }, + func(gui *Gui) Context { return gui.State.Contexts.SubCommits }, + } + + for _, c := range contexts { + gui := NewDummyGui() + context := c(gui) + setupGuiForTest(gui) + gui.Config.GetUserConfig().Git.DiffContextSize = 1 + gui.pushContextDirect(context) + + gui.IncreaseContextInDiffView() + + assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey())) + } +} + +func TestDoesntIncreaseContextInDiffViewInContextWithoutDiff(t *testing.T) { + contexts := []func(gui *Gui) Context { + func(gui *Gui) Context { return gui.State.Contexts.Status }, + func(gui *Gui) Context { return gui.State.Contexts.Submodules }, + func(gui *Gui) Context { return gui.State.Contexts.Remotes }, + func(gui *Gui) Context { return gui.State.Contexts.Normal }, + func(gui *Gui) Context { return gui.State.Contexts.ReflogCommits }, + func(gui *Gui) Context { return gui.State.Contexts.RemoteBranches }, + func(gui *Gui) Context { return gui.State.Contexts.Tags }, + func(gui *Gui) Context { return gui.State.Contexts.Merging }, + func(gui *Gui) Context { return gui.State.Contexts.CommandLog }, + } + + for _, c := range contexts { + gui := NewDummyGui() + context := c(gui) + setupGuiForTest(gui) + gui.Config.GetUserConfig().Git.DiffContextSize = 1 + gui.pushContextDirect(context) + + gui.IncreaseContextInDiffView() + + assert.Equal(t, 1, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey())) + } +} |