mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-10-05 21:46:49 -04:00
The main view shows a diff renderer's picture of a diff, and that is not what you want on your clipboard: a renderer may drop the +/- column, move the line numbers into a gutter, group a hunk's deletions before its additions, or leave lines out altogether. So copying takes the lines from the diff itself, locating them by the identity of the selected rows. Only the panel that produced the diff can produce it again, though: the working tree's staged or unstaged side, a commit's, a stash entry's. The new seam is there for that. It asks per file, so that copying three lines of a commit's diff doesn't fetch the whole of it, and it will grow the actions on a selection as staging and patch building arrive. The clipboard gets the run of diff lines from the first selected line of a file to the last, so that lines the renderer hid come along and the result still reads as a diff. Headers count as selected lines too. A hunk header names the first line of its hunk, so it is looked for the way a line of the file is. A file header names no line at all, and a rendering may draw it over any number of rows, so a selection touching one of them takes the whole header. As in the patch explorers, a selection that is all additions or all deletions loses its +/- column, ready to be pasted into code. One that reaches into a header keeps the columns, and what comes out is a patch fragment rather than lines of code. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
141 lines
4.0 KiB
Go
141 lines
4.0 KiB
Go
package controllers
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
|
"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) 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) GetFocusedMainViewDiffSource() types.FocusedMainViewDiffSource {
|
|
return self
|
|
}
|
|
|
|
// PlainDiff hands out the diff of the panel's selected commit (or range of them) for
|
|
// the given files — the same diff its main view shows, only without the commit's
|
|
// message and stat above it.
|
|
func (self *SwitchToDiffFilesController) PlainDiff(_ types.DiffPaneContext, paths []string) string {
|
|
ref := self.context.GetSelectedRef()
|
|
if ref == nil {
|
|
return ""
|
|
}
|
|
from, to := context.FromAndToForDiff(ref, self.context.GetSelectedRefRangeForDiffFiles())
|
|
return self.c.Helpers().Diff.PlainDiffBetweenRefs(from, to, paths)
|
|
}
|
|
|
|
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
|
|
}
|