mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-10-06 14:06:32 -04:00
The focused diff addresses patch lines by identity, so changing how much context is rendered cannot invalidate an active patch. Remove the explorer era refusal and prove that another line can still be toggled correctly after the rerender. Co-Authored-By: GitHub Copilot <copilot@github.com>
77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
// This controller lets you change the context size for diffs. The 'context' in 'context size' refers to the conventional meaning of the word 'context' in a diff, as opposed to lazygit's own idea of a 'context'.
|
|
|
|
type ContextLinesController struct {
|
|
baseController
|
|
c *ControllerCommon
|
|
}
|
|
|
|
var _ types.IController = &ContextLinesController{}
|
|
|
|
func NewContextLinesController(
|
|
c *ControllerCommon,
|
|
) *ContextLinesController {
|
|
return &ContextLinesController{
|
|
baseController: baseController{},
|
|
c: c,
|
|
}
|
|
}
|
|
|
|
func (self *ContextLinesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
bindings := []*types.Binding{
|
|
{
|
|
Keys: opts.GetKeys(opts.Config.Universal.IncreaseContextInDiffView),
|
|
Handler: self.Increase,
|
|
Description: self.c.Tr.IncreaseContextInDiffView,
|
|
Tooltip: self.c.Tr.IncreaseContextInDiffViewTooltip,
|
|
},
|
|
{
|
|
Keys: opts.GetKeys(opts.Config.Universal.DecreaseContextInDiffView),
|
|
Handler: self.Decrease,
|
|
Description: self.c.Tr.DecreaseContextInDiffView,
|
|
Tooltip: self.c.Tr.DecreaseContextInDiffViewTooltip,
|
|
},
|
|
}
|
|
|
|
return bindings
|
|
}
|
|
|
|
func (self *ContextLinesController) Context() types.Context {
|
|
return nil
|
|
}
|
|
|
|
func (self *ContextLinesController) Increase() error {
|
|
if self.c.UserConfig().Git.DiffContextSize < math.MaxUint64 {
|
|
self.c.UserConfig().Git.DiffContextSize++
|
|
}
|
|
return self.applyChange()
|
|
}
|
|
|
|
func (self *ContextLinesController) Decrease() error {
|
|
if self.c.UserConfig().Git.DiffContextSize > 0 {
|
|
self.c.UserConfig().Git.DiffContextSize--
|
|
}
|
|
return self.applyChange()
|
|
}
|
|
|
|
func (self *ContextLinesController) applyChange() error {
|
|
self.c.Toast(fmt.Sprintf(self.c.Tr.DiffContextSizeChanged, self.c.UserConfig().Git.DiffContextSize))
|
|
|
|
currentContext := self.c.Context().CurrentSide()
|
|
// The diff is about to be rendered again with more or less context around
|
|
// each change, which reads as the lines you were looking at moving up or down
|
|
// the view; keep them where they are instead.
|
|
self.c.Helpers().DiffLine.PreserveDiffPositionOnRerender(self.c.Contexts().Normal.GetView())
|
|
self.c.Helpers().DiffLine.PreserveDiffPositionOnRerender(self.c.Contexts().NormalSecondary.GetView())
|
|
currentContext.HandleRenderToMain()
|
|
return nil
|
|
}
|