mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-10 15:46:26 -04:00
Escaping a patch explorer (staging / patch building) back to the focused main view it was entered from used to replay a numeric scroll position and selection index captured on the way in. But the reason to escape after staging or dropping a hunk is that the content changed, so a saved index points at the wrong line — and the host auto-advances the explorer's selection to a still-valid line anyway, which is the line the user actually cares about returning to. Restore by *patch identity* instead. On escape, read the (file, type, source line) the explorer currently has selected, then have the main view's re-render land on the row that matches it: scan the incoming content as it loads (the inverse of the diff-line primitive), and once the matching row plus a screenful below it have loaded, swap the off-screen render in and scroll to / select that row in one step. FocusPoint with scrollIntoView centres the row only if it's off-screen, so the common unchanged-content escape — where the row is already where it was — doesn't move at all. If the line is gone (the content really changed), nothing is forced. This generalizes the scroll restore from a fixed origin to a predicate (RenderRestore: FirstPaintReady decides when the saved position is reachable, Apply re-establishes it), folding the separate selection restore into the same first paint — so it no longer rides a post-load callback that could fire early. The restore also now survives task replacement, which the numeric version did not: a periodic refresh can stop the escape's re-render before it first-paints. The pending restore is held on the buffer manager and is *not* cleared when a task starts, so the replacement task picks it up. It is not gated on the command key — staging the last unstaged hunk re-renders `git diff` as `git diff --cached`, a different command, yet the line to land on is still in the new content — but validates itself: the scan finds the target line only when the content still contains it, so applying it to a different item is a harmless no-op. A task clears it once it has applied it (found or not), so it lives for exactly one re-render. Because the restore is anchored on content identity and is idempotent, "survive replacement" and "restore by identity" are one mechanism, not two. With the identity in hand the snapshot no longer needs the captured scroll/index; they're derived from the explorer's live selection.
167 lines
4.8 KiB
Go
167 lines
4.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
// This controller is for all contexts that contain commit files.
|
|
|
|
var _ types.IController = &SwitchToDiffFilesController{}
|
|
|
|
type CanSwitchToDiffFiles interface {
|
|
types.IListContext
|
|
CanRebase() bool
|
|
GetSelectedRef() models.Ref
|
|
GetSelectedRefRangeForDiffFiles() *types.RefRange
|
|
}
|
|
|
|
// Not using our ListControllerTrait because we have our own way of working with
|
|
// range selections that's different from ListControllerTrait's
|
|
type SwitchToDiffFilesController struct {
|
|
baseController
|
|
c *ControllerCommon
|
|
context CanSwitchToDiffFiles
|
|
}
|
|
|
|
func NewSwitchToDiffFilesController(
|
|
c *ControllerCommon,
|
|
context CanSwitchToDiffFiles,
|
|
) *SwitchToDiffFilesController {
|
|
return &SwitchToDiffFilesController{
|
|
baseController: baseController{},
|
|
c: c,
|
|
context: context,
|
|
}
|
|
}
|
|
|
|
func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
bindings := []*types.Binding{
|
|
{
|
|
Keys: opts.GetKeys(opts.Config.Universal.GoInto),
|
|
Handler: self.enter,
|
|
GetDisabledReason: self.canEnter,
|
|
Description: self.c.Tr.ViewItemFiles,
|
|
},
|
|
}
|
|
|
|
return bindings
|
|
}
|
|
|
|
func (self *SwitchToDiffFilesController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error {
|
|
return func(mainViewName string, clickedLineIdx int) error {
|
|
info, ok := self.c.Helpers().Staging.GetDiffLineInfo(mainViewName, clickedLineIdx)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
line, isDeletion := info.PatchSelectLine()
|
|
|
|
// Capture before self.enter() pushes the commit files panel, which
|
|
// re-renders the main view. We escape "all the way out" to this side
|
|
// panel (skipping the commit files panel), then focus the main view.
|
|
snapshot := focusedMainViewSnapshot(self.c, mainViewName, self.context)
|
|
|
|
if err := self.enter(); err != nil {
|
|
return err
|
|
}
|
|
|
|
context := self.c.Contexts().CommitFiles
|
|
var node *filetree.CommitFileNode
|
|
|
|
relativePath, err := filepath.Rel(self.c.Git().RepoPaths.WorktreePath(), info.Path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
relativePath = "./" + relativePath
|
|
context.CommitFileTreeViewModel.ExpandToPath(relativePath)
|
|
self.c.PostRefreshUpdate(context)
|
|
|
|
idx, ok := context.CommitFileTreeViewModel.GetIndexForPath(relativePath)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
context.SetSelectedLineIdx(idx)
|
|
context.GetViewTrait().FocusPoint(
|
|
context.ModelIndexToViewIndex(idx), false)
|
|
node = context.GetSelected()
|
|
return self.c.Helpers().CommitFiles.EnterCommitFile(node, snapshot, types.OnFocusOpts{ClickedWindowName: "main", ClickedViewLineIdx: line, ClickedViewRealLineIdx: line, ClickedViewRealLineIsDeletion: isDeletion, SelectLineInDefaultMode: true})
|
|
}
|
|
}
|
|
|
|
func (self *SwitchToDiffFilesController) Context() types.Context {
|
|
return self.context
|
|
}
|
|
|
|
func (self *SwitchToDiffFilesController) GetOnDoubleClick() func() error {
|
|
return func() error {
|
|
if self.canEnter() == nil {
|
|
return self.enter()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (self *SwitchToDiffFilesController) enter() error {
|
|
ref := self.context.GetSelectedRef()
|
|
refsRange := self.context.GetSelectedRefRangeForDiffFiles()
|
|
commitFilesContext := self.c.Contexts().CommitFiles
|
|
|
|
canRebase := self.context.CanRebase()
|
|
if canRebase {
|
|
if self.c.Modes().Diffing.Active() {
|
|
if self.c.Modes().Diffing.Ref != ref.RefName() {
|
|
canRebase = false
|
|
}
|
|
} else if refsRange != nil {
|
|
canRebase = false
|
|
}
|
|
}
|
|
|
|
commitFilesContext.ClearFilter()
|
|
commitFilesContext.ReInit(ref, refsRange)
|
|
commitFilesContext.SetSelection(0)
|
|
commitFilesContext.SetCanRebase(canRebase)
|
|
commitFilesContext.SetParentContext(self.context)
|
|
commitFilesContext.SetWindowName(self.context.GetWindowName())
|
|
commitFilesContext.GetView().TitlePrefix = self.context.GetView().TitlePrefix
|
|
|
|
self.c.Refresh(types.RefreshOptions{
|
|
Scope: []types.RefreshableView{types.COMMIT_FILES},
|
|
Then: func() error {
|
|
if filterPath := self.c.Modes().Filtering.GetPath(); filterPath != "" {
|
|
path, err := filepath.Rel(self.c.Git().RepoPaths.RepoPath(), filterPath)
|
|
if err != nil {
|
|
path = filterPath
|
|
}
|
|
commitFilesContext.CommitFileTreeViewModel.SelectPath(
|
|
filepath.ToSlash(path), self.c.UserConfig().Gui.ShowRootItemInFileTree)
|
|
}
|
|
self.c.Context().Push(commitFilesContext, types.OnFocusOpts{})
|
|
return nil
|
|
},
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (self *SwitchToDiffFilesController) canEnter() *types.DisabledReason {
|
|
refRange := self.context.GetSelectedRefRangeForDiffFiles()
|
|
if refRange != nil {
|
|
return nil
|
|
}
|
|
ref := self.context.GetSelectedRef()
|
|
if ref == nil {
|
|
return &types.DisabledReason{Text: self.c.Tr.NoItemSelected}
|
|
}
|
|
if ref.RefName() == "" {
|
|
return &types.DisabledReason{Text: self.c.Tr.SelectedItemDoesNotHaveFiles}
|
|
}
|
|
|
|
return nil
|
|
}
|