From 11196f279e97524736c41aef3ade170ed3ba5e6b Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 1 Jul 2025 17:06:12 +0200 Subject: [PATCH] Render only + and - lines as included in a patch It is confusing to get header lines, hunk headers, or context lines rendered as being included in a custom patch, when including these makes no difference to the patch. This is only a visual change; internally, we still record these non-patch lines as being included in the patch. It doesn't matter though; you can press space on a header line and nothing happens. It would probably be cleaner to only record + and - lines in the includedLines array, but that would be a bit more work, and doesn't seem worth it. --- pkg/commands/patch/format.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/pkg/commands/patch/format.go b/pkg/commands/patch/format.go index c61c8ef05..ccfc7924d 100644 --- a/pkg/commands/patch/format.go +++ b/pkg/commands/patch/format.go @@ -72,30 +72,22 @@ func (self *patchPresenter) format() string { lineIdx++ } - appendFormattedLine := func(line string, style style.TextStyle) { - formattedLine := self.formatLine( - line, - style, - lineIdx, - ) - - appendLine(formattedLine) - } for _, line := range self.patch.header { - appendFormattedLine(line, theme.DefaultTextColor.SetBold()) + // always passing false for 'included' here because header lines are not part of the patch + appendLine(self.formatLineAux(line, theme.DefaultTextColor.SetBold(), false)) } for _, hunk := range self.patch.hunks { appendLine( - self.formatLine( + self.formatLineAux( hunk.formatHeaderStart(), style.FgCyan, - lineIdx, + false, ) + // we're splitting the line into two parts: the diff header and the context - // We explicitly pass 'included' as false here so that we're only tagging the - // first half of the line as included if the line is indeed included. + // We explicitly pass 'included' as false for both because these are not part + // of the actual patch self.formatLineAux( hunk.headerContext, theme.DefaultTextColor, @@ -104,7 +96,12 @@ func (self *patchPresenter) format() string { ) for _, line := range hunk.bodyLines { - appendFormattedLine(line.Content, self.patchLineStyle(line)) + style := self.patchLineStyle(line) + if line.isChange() { + appendLine(self.formatLine(line.Content, style, lineIdx)) + } else { + appendLine(self.formatLineAux(line.Content, style, false)) + } } }