on main help sign in
fix(diffview): fix rendering issue caused by line breaks added by chroma
e7f29adAndrey Nering
internal/tui/exp/diffview/chroma.go+5−2
@@ -4,6 +4,7 @@ import (
4 "fmt"
4 "fmt"
5 "image/color"
5 "image/color"
6 "io"
6 "io"
7+ "strings"
7
8
8 "github.com/alecthomas/chroma/v2"
9 "github.com/alecthomas/chroma/v2"
9 "github.com/charmbracelet/lipgloss/v2"
10 "github.com/charmbracelet/lipgloss/v2"
@@ -20,9 +21,11 @@ type chromaFormatter struct {
20// Format implements the chroma.Formatter interface.
21// Format implements the chroma.Formatter interface.
21func (c chromaFormatter) Format(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
22func (c chromaFormatter) Format(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
22 for token := it(); token != chroma.EOF; token = it() {
23 for token := it(); token != chroma.EOF; token = it() {
24+ value := strings.TrimSuffix(token.Value, "\n")
25+
23 entry := style.Get(token.Type)
26 entry := style.Get(token.Type)
24 if entry.IsZero() {
27 if entry.IsZero() {
25− if _, err := fmt.Fprint(w, token.Value); err != nil {
28+ if _, err := fmt.Fprint(w, value); err != nil {
26 return err
29 return err
27 }
30 }
28 continue
31 continue
@@ -44,7 +47,7 @@ func (c chromaFormatter) Format(w io.Writer, style *chroma.Style, it chroma.Iter
44 s = s.Foreground(lipgloss.Color(entry.Colour.String()))
47 s = s.Foreground(lipgloss.Color(entry.Colour.String()))
45 }
48 }
46
49
47− if _, err := fmt.Fprint(w, s.Render(token.Value)); err != nil {
50+ if _, err := fmt.Fprint(w, s.Render(value)); err != nil {
48 return err
51 return err
49 }
52 }
50 }
53 }
internal/tui/exp/diffview/diffview_test.go+26
@@ -36,6 +36,12 @@ var TestTabsBefore string
36//go:embed testdata/TestTabs.after
36//go:embed testdata/TestTabs.after
37var TestTabsAfter string
37var TestTabsAfter string
38
38
39+//go:embed testdata/TestLineBreakIssue.before
40+var TestLineBreakIssueBefore string
41+
42+//go:embed testdata/TestLineBreakIssue.after
43+var TestLineBreakIssueAfter string
44+
39type (
45type (
40 TestFunc func(dv *diffview.DiffView) *diffview.DiffView
46 TestFunc func(dv *diffview.DiffView) *diffview.DiffView
41 TestFuncs map[string]TestFunc
47 TestFuncs map[string]TestFunc
@@ -177,6 +183,26 @@ func TestDiffViewTabs(t *testing.T) {
177 }
183 }
178}
184}
179
185
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+
180func TestDiffViewWidth(t *testing.T) {
206func TestDiffViewWidth(t *testing.T) {
181 for layoutName, layoutFunc := range LayoutFuncs {
207 for layoutName, layoutFunc := range LayoutFuncs {
182 t.Run(layoutName, func(t *testing.T) {
208 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+});