on main help sign in
fix(diffview): fix rendering issue caused by line breaks added by chroma
commite7f29adce969e6056b5a20d0777dd24f1bc0854b authorAndrey Nering <andreynering@users.noreply.github.com> dateWed 23 Jul 2025, 14:12 (1 year ago) parent78789b8
6 files changed+66−2
internal/tui/exp/diffview/chroma.go+5−2 internal/tui/exp/diffview/diffview_test.go+26 internal/tui/exp/diffview/testdata/TestDiffViewLineBreakIssue/Split.golden+9 internal/tui/exp/diffview/testdata/TestDiffViewLineBreakIssue/Unified.golden+12 internal/tui/exp/diffview/testdata/TestLineBreakIssue.after+8 internal/tui/exp/diffview/testdata/TestLineBreakIssue.before+6
internal/tui/exp/diffview/chroma.go+5−2
@@ -4,6 +4,7 @@ import (
44 "fmt"
55 "image/color"
66 "io"
7+ "strings"
78
89 "github.com/alecthomas/chroma/v2"
910 "github.com/charmbracelet/lipgloss/v2"
@@ -20,9 +21,11 @@ type chromaFormatter struct {
2021// Format implements the chroma.Formatter interface.
2122func (c chromaFormatter) Format(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
2223 for token := it(); token != chroma.EOF; token = it() {
24+ value := strings.TrimSuffix(token.Value, "\n")
25+
2326 entry := style.Get(token.Type)
2427 if entry.IsZero() {
25− if _, err := fmt.Fprint(w, token.Value); err != nil {
28+ if _, err := fmt.Fprint(w, value); err != nil {
2629 return err
2730 }
2831 continue
@@ -44,7 +47,7 @@ func (c chromaFormatter) Format(w io.Writer, style *chroma.Style, it chroma.Iter
4447 s = s.Foreground(lipgloss.Color(entry.Colour.String()))
4548 }
4649
47− if _, err := fmt.Fprint(w, s.Render(token.Value)); err != nil {
50+ if _, err := fmt.Fprint(w, s.Render(value)); err != nil {
4851 return err
4952 }
5053 }
internal/tui/exp/diffview/diffview_test.go+26
@@ -36,6 +36,12 @@ var TestTabsBefore string
3636//go:embed testdata/TestTabs.after
3737var TestTabsAfter string
3838
39+//go:embed testdata/TestLineBreakIssue.before
40+var TestLineBreakIssueBefore string
41+
42+//go:embed testdata/TestLineBreakIssue.after
43+var TestLineBreakIssueAfter string
44+
3945type (
4046 TestFunc func(dv *diffview.DiffView) *diffview.DiffView
4147 TestFuncs map[string]TestFunc
@@ -177,6 +183,26 @@ func TestDiffViewTabs(t *testing.T) {
177183 }
178184}
179185
186+func TestDiffViewLineBreakIssue(t *testing.T) {
187+ t.Parallel()
188+
189+ for layoutName, layoutFunc := range LayoutFuncs {
190+ t.Run(layoutName, func(t *testing.T) {
191+ t.Parallel()
192+
193+ dv := diffview.New().
194+ Before("index.js", TestLineBreakIssueBefore).
195+ After("index.js", TestLineBreakIssueAfter).
196+ Style(diffview.DefaultLightStyle()).
197+ ChromaStyle(styles.Get("catppuccin-latte"))
198+ dv = layoutFunc(dv)
199+
200+ output := dv.String()
201+ golden.RequireEqual(t, []byte(output))
202+ })
203+ }
204+}
205+
180206func TestDiffViewWidth(t *testing.T) {
181207 for layoutName, layoutFunc := range LayoutFuncs {
182208 t.Run(layoutName, func(t *testing.T) {
internal/tui/exp/diffview/testdata/TestDiffViewLineBreakIssue/Split.goldennew file+9
@@ -0,0 +1,9 @@
1+  …  @@ -1,6 +1,8 @@    …   
2+  1 - // this is   1 + /** 
3+       2 +  * this is 
4+  2 - // a regular   3 +  * a block 
5+  3 - // comment   4 +  * comment 
6+       5 +  */ 
7+  4  $(function() {   6  $(function() { 
8+  5   console.log("Hello, world!");   7   console.log("Hello, world!"); 
9+  6  });   8  }); 
internal/tui/exp/diffview/testdata/TestDiffViewLineBreakIssue/Unified.goldennew file+12
@@ -0,0 +1,12 @@
1+  …   …  @@ -1,6 +1,8 @@  
2+  1    - // this is 
3+     1 + /** 
4+     2 +  * this is 
5+  2    - // a regular 
6+     3 +  * a block 
7+  3    - // comment 
8+     4 +  * comment 
9+     5 +  */ 
10+  4   6  $(function() { 
11+  5   7   console.log("Hello, world!"); 
12+  6   8  }); 
internal/tui/exp/diffview/testdata/TestLineBreakIssue.afternew file+8
@@ -0,0 +1,8 @@
1+/**
2+ * this is
3+ * a block
4+ * comment
5+ */
6+$(function() {
7+ console.log("Hello, world!");
8+});
internal/tui/exp/diffview/testdata/TestLineBreakIssue.beforenew file+6
@@ -0,0 +1,6 @@
1+// this is
2+// a regular
3+// comment
4+$(function() {
5+ console.log("Hello, world!");
6+});