mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-07 01:09:45 +02:00
Toggle only added/deleted lines in patch building view
This improves the experience when selecting a hunk generously with the mouse, by dragging over it including some context lines above and below. Previously we would consider the "moving end" of the selection range for whether things need to be added or removed, but this doesn't make sense if it's a context line. Now we consider the first actual change line that is included in the range.
This commit is contained in:
@ -126,7 +126,6 @@ func (self *PatchBuildingController) toggleSelection() error {
|
||||
self.context().GetMutex().Lock()
|
||||
defer self.context().GetMutex().Unlock()
|
||||
|
||||
toggleFunc := self.c.Git().Patch.PatchBuilder.AddFileLineRange
|
||||
filename := self.c.Contexts().CommitFiles.GetSelectedPath()
|
||||
if filename == "" {
|
||||
return nil
|
||||
@ -134,19 +133,26 @@ func (self *PatchBuildingController) toggleSelection() error {
|
||||
|
||||
state := self.context().GetState()
|
||||
|
||||
// Get added/deleted lines in the selected patch range
|
||||
lineIndicesToToggle := state.ChangeLinesInSelectedPatchRange()
|
||||
if len(lineIndicesToToggle) == 0 {
|
||||
// Only context lines or header lines selected, so nothing to do
|
||||
return nil
|
||||
}
|
||||
|
||||
includedLineIndices, err := self.c.Git().Patch.PatchBuilder.GetFileIncLineIndices(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currentLineIsStaged := lo.Contains(includedLineIndices, state.GetSelectedPatchLineIdx())
|
||||
if currentLineIsStaged {
|
||||
|
||||
toggleFunc := self.c.Git().Patch.PatchBuilder.AddFileLineRange
|
||||
firstSelectedChangeLineIsStaged := lo.Contains(includedLineIndices, lineIndicesToToggle[0])
|
||||
if firstSelectedChangeLineIsStaged {
|
||||
toggleFunc = self.c.Git().Patch.PatchBuilder.RemoveFileLineRange
|
||||
}
|
||||
|
||||
// add range of lines to those set for the file
|
||||
firstLineIdx, lastLineIdx := state.SelectedPatchRange()
|
||||
|
||||
if err := toggleFunc(filename, firstLineIdx, lastLineIdx); err != nil {
|
||||
if err := toggleFunc(filename, lineIndicesToToggle); err != nil {
|
||||
// might actually want to return an error here
|
||||
self.c.Log.Error(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user