From 78789b8cfbaa705d08465261df70fb2a96a87275 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Wed, 23 Jul 2025 13:31:39 -0300 Subject: [PATCH] refactor(diffview): simplify how we handle line endings --- internal/tui/exp/diffview/diffview.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/tui/exp/diffview/diffview.go b/internal/tui/exp/diffview/diffview.go index ddac14296984cb31ce7f0b179950b2832280d3d1..eaea2837fcaa7522294143f0385bcbb0879316bd 100644 --- a/internal/tui/exp/diffview/diffview.go +++ b/internal/tui/exp/diffview/diffview.go @@ -193,6 +193,7 @@ func (dv *DiffView) clearSyntaxCache() { // String returns the string representation of the DiffView. func (dv *DiffView) String() string { + dv.normalizeLineEndings() dv.replaceTabs() if err := dv.computeDiff(); err != nil { return err.Error() @@ -227,6 +228,12 @@ func (dv *DiffView) String() string { } } +// normalizeLineEndings ensures the file contents use Unix-style line endings. +func (dv *DiffView) normalizeLineEndings() { + dv.before.content = strings.ReplaceAll(dv.before.content, "\r\n", "\n") + dv.after.content = strings.ReplaceAll(dv.after.content, "\r\n", "\n") +} + // replaceTabs replaces tabs in the before and after file contents with spaces // according to the specified tab width. func (dv *DiffView) replaceTabs() { @@ -396,8 +403,7 @@ func (dv *DiffView) renderUnified() string { shouldWrite := func() bool { return printedLines >= 0 } getContent := func(in string, ls LineStyle) (content string, leadingEllipsis bool) { - content = strings.ReplaceAll(in, "\r\n", "\n") - content = strings.TrimSuffix(content, "\n") + content = strings.TrimSuffix(in, "\n") content = dv.hightlightCode(content, ls.Code.GetBackground()) content = ansi.GraphemeWidth.Cut(content, dv.xOffset, len(content)) content = ansi.Truncate(content, dv.codeWidth, "…") @@ -520,8 +526,7 @@ func (dv *DiffView) renderSplit() string { shouldWrite := func() bool { return printedLines >= 0 } getContent := func(in string, ls LineStyle) (content string, leadingEllipsis bool) { - content = strings.ReplaceAll(in, "\r\n", "\n") - content = strings.TrimSuffix(content, "\n") + content = strings.TrimSuffix(in, "\n") content = dv.hightlightCode(content, ls.Code.GetBackground()) content = ansi.GraphemeWidth.Cut(content, dv.xOffset, len(content)) content = ansi.Truncate(content, dv.codeWidth, "…")