jesseduffield.lazygit/pkg/gui/controllers/sub_commits_controller.go
Stefan Haller fdb16a1c7e Preserve the focused main view's selection across commit rewrites
Dropping a hunk with `d` re-establishes the focused main view's selection
on the next surviving change, which feels great. But other operations that
rewrite the commit under the focused main view — moving a custom patch out
into the index, undoing right after a discard or a patch move — don't run
through the focused-main-view action handlers, so nothing was preserving
the selection. The stale gocui selection was left painted over the new
content, often as a large, now-meaningless range.

Rather than teach every such command to capture and restore the selection
(move-patch, undo, redo, and any future one), the focused main view now
preserves it itself, by its change-line ordinal, as the diff re-renders —
the command-agnostic counterpart of revealSelectionAfterPrimaryAction. The
diff side panels call it from their render-to-main before triggering the
render, so the restore rides the re-render.

It stands down unless the focused main view is current and shows a
selection, no precise restore is already pending (escape / post-stage
reveal / context-size place the selection more precisely), and the diff
command is actually changing. That last gate matters: a plain background
refresh re-renders the same commit's diff unchanged, and there the
selection — range and all — must be left alone; only a command change
(e.g. a rebase rewriting the commit's hash) means the content moved.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-08 12:59:00 +02:00

84 lines
2.5 KiB
Go

package controllers
import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SubCommitsController struct {
baseController
*ListControllerTrait[*models.Commit]
c *ControllerCommon
}
var _ types.IController = &SubCommitsController{}
func NewSubCommitsController(
c *ControllerCommon,
) *SubCommitsController {
return &SubCommitsController{
baseController: baseController{},
ListControllerTrait: NewListControllerTrait(
c,
c.Contexts().SubCommits,
c.Contexts().SubCommits.GetSelected,
c.Contexts().SubCommits.GetSelectedItems,
),
c: c,
}
}
func (self *SubCommitsController) Context() types.Context {
return self.context()
}
func (self *SubCommitsController) context() *context.SubCommitsContext {
return self.c.Contexts().SubCommits
}
func (self *SubCommitsController) GetOnRenderToMain() func() {
return func() {
self.c.Helpers().Diff.WithDiffModeCheck(func() {
commit := self.context().GetSelected()
var task types.UpdateTask
if commit == nil {
task = types.NewRenderStringTask("No commits")
} else {
refRange := self.context().GetSelectedRefRangeForDiffFiles()
renderRaw := self.c.Helpers().Staging.DiffMainViewShouldRenderRaw()
task = self.c.Helpers().Diff.GetUpdateTaskForRenderingCommitsDiff(commit, refRange, renderRaw)
}
// Keep the inclusion gutter in step with the content as this diff
// (re-)renders; a no-op unless the main view is focused and a patch is being
// built from this panel. See LocalCommitsController.GetOnRenderToMain.
self.c.Helpers().Staging.RefreshInclusionGutter()
// Preserve the focused-main-view selection across a commit rewrite. See
// LocalCommitsController.GetOnRenderToMain.
preserveFocusedMainViewSelectionAcrossContentChange(self.c, task)
self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{
Title: "Commit",
SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: task,
},
Secondary: secondaryPatchPanelUpdateOpts(self.c),
})
})
}
}
func (self *SubCommitsController) GetOnFocus() func(types.OnFocusOpts) {
return func(types.OnFocusOpts) {
context := self.context()
if context.GetSelectedLineIdx() > COMMIT_THRESHOLD && context.GetLimitCommits() {
context.SetLimitCommits(false)
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUB_COMMITS}})
}
}
}