Skip to content

Commit 13a3540

Browse files
committed
Introduce options struct for RenderPatchForFile
We're going to add another argument in the next commit, and that's getting a bit much, especially when most of the arguments are bool and you only see true and false at the call sites without knowing what they mean.
1 parent 1a76a7d commit 13a3540

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

pkg/commands/patch/patch_builder.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ func (p *PatchBuilder) PatchToApply(reverse bool) string {
7373
continue
7474
}
7575

76-
patch += p.RenderPatchForFile(filename, true, reverse)
76+
patch += p.RenderPatchForFile(RenderPatchForFileOpts{
77+
Filename: filename,
78+
Plain: true,
79+
Reverse: reverse,
80+
})
7781
}
7882

7983
return patch
@@ -172,8 +176,14 @@ func (p *PatchBuilder) RemoveFileLineRange(filename string, firstLineIdx, lastLi
172176
return nil
173177
}
174178

175-
func (p *PatchBuilder) RenderPatchForFile(filename string, plain bool, reverse bool) string {
176-
info, err := p.getFileInfo(filename)
179+
type RenderPatchForFileOpts struct {
180+
Filename string
181+
Plain bool
182+
Reverse bool
183+
}
184+
185+
func (p *PatchBuilder) RenderPatchForFile(opts RenderPatchForFileOpts) string {
186+
info, err := p.getFileInfo(opts.Filename)
177187
if err != nil {
178188
p.Log.Error(err)
179189
return ""
@@ -183,7 +193,7 @@ func (p *PatchBuilder) RenderPatchForFile(filename string, plain bool, reverse b
183193
return ""
184194
}
185195

186-
if info.mode == WHOLE && plain {
196+
if info.mode == WHOLE && opts.Plain {
187197
// Use the whole diff (spares us parsing it and then formatting it).
188198
// TODO: see if this is actually noticeably faster.
189199
// The reverse flag is only for part patches so we're ignoring it here.
@@ -192,11 +202,11 @@ func (p *PatchBuilder) RenderPatchForFile(filename string, plain bool, reverse b
192202

193203
patch := Parse(info.diff).
194204
Transform(TransformOpts{
195-
Reverse: reverse,
205+
Reverse: opts.Reverse,
196206
IncludedLineIndices: info.includedLineIndices,
197207
})
198208

199-
if plain {
209+
if opts.Plain {
200210
return patch.FormatPlain()
201211
} else {
202212
return patch.FormatView(FormatViewOpts{})
@@ -209,7 +219,11 @@ func (p *PatchBuilder) renderEachFilePatch(plain bool) []string {
209219

210220
sort.Strings(filenames)
211221
patches := lo.Map(filenames, func(filename string, _ int) string {
212-
return p.RenderPatchForFile(filename, plain, false)
222+
return p.RenderPatchForFile(RenderPatchForFileOpts{
223+
Filename: filename,
224+
Plain: plain,
225+
Reverse: false,
226+
})
213227
})
214228
output := lo.Filter(patches, func(patch string, _ int) bool {
215229
return patch != ""

pkg/gui/controllers/helpers/patch_building_helper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package helpers
33
import (
44
"errors"
55

6+
"github.com/jesseduffield/lazygit/pkg/commands/patch"
67
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
78
"github.com/jesseduffield/lazygit/pkg/gui/patch_exploring"
89
"github.com/jesseduffield/lazygit/pkg/gui/types"
@@ -80,7 +81,11 @@ func (self *PatchBuildingHelper) RefreshPatchBuildingPanel(opts types.OnFocusOpt
8081
return err
8182
}
8283

83-
secondaryDiff := self.c.Git().Patch.PatchBuilder.RenderPatchForFile(path, false, false)
84+
secondaryDiff := self.c.Git().Patch.PatchBuilder.RenderPatchForFile(patch.RenderPatchForFileOpts{
85+
Filename: path,
86+
Plain: false,
87+
Reverse: false,
88+
})
8489

8590
context := self.c.Contexts().CustomPatchBuilder
8691

0 commit comments

Comments
 (0)