Skip to content

Commit 9539bf2

Browse files
committed
fix
1 parent 12a737e commit 9539bf2

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

services/gitdiff/gitdiff.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,10 +1336,34 @@ func GetDiffForRender(ctx context.Context, repoLink string, gitRepo *git.Reposit
13361336
return diff, nil
13371337
}
13381338

1339+
func splitHighlightLines(buf []byte) (ret [][]byte) {
1340+
lineCount := bytes.Count(buf, []byte("\n")) + 1
1341+
ret = make([][]byte, 0, lineCount)
1342+
nlTagClose := []byte("\n</")
1343+
for {
1344+
pos := bytes.IndexByte(buf, '\n')
1345+
if pos == -1 {
1346+
ret = append(ret, buf)
1347+
return ret
1348+
}
1349+
// Chroma highlighting output sometimes have "</span>" right after \n, sometimes before.
1350+
// * "<span>text\n</span>"
1351+
// * "<span>text</span>\n"
1352+
if bytes.HasPrefix(buf[pos:], nlTagClose) {
1353+
pos1 := bytes.IndexByte(buf[pos:], '>')
1354+
if pos1 != -1 {
1355+
pos += pos1
1356+
}
1357+
}
1358+
ret = append(ret, buf[:pos+1])
1359+
buf = buf[pos+1:]
1360+
}
1361+
}
1362+
13391363
func highlightCodeLines(diffFile *DiffFile, isLeft bool, rawContent []byte) map[int]template.HTML {
13401364
content := util.UnsafeBytesToString(charset.ToUTF8(rawContent, charset.ConvertOpts{}))
13411365
highlightedNewContent, _ := highlight.Code(diffFile.Name, diffFile.Language, content)
1342-
splitLines := strings.Split(string(highlightedNewContent), "\n")
1366+
splitLines := splitHighlightLines([]byte(highlightedNewContent))
13431367
lines := make(map[int]template.HTML, len(splitLines))
13441368
// only save the highlighted lines we need, but not the whole file, to save memory
13451369
for _, sec := range diffFile.Sections {

services/gitdiff/gitdiff_test.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package gitdiff
66

77
import (
8+
"html/template"
89
"strconv"
910
"strings"
1011
"testing"
@@ -1108,16 +1109,39 @@ func TestDiffLine_GetExpandDirection(t *testing.T) {
11081109
}
11091110

11101111
func TestHighlightCodeLines(t *testing.T) {
1111-
diffFile := &DiffFile{
1112-
Name: "a.c",
1113-
Language: "C",
1114-
Sections: []*DiffSection{
1115-
{
1116-
Lines: []*DiffLine{{LeftIdx: 1}},
1112+
t.Run("CharsetDetecting", func(t *testing.T) {
1113+
diffFile := &DiffFile{
1114+
Name: "a.c",
1115+
Language: "c",
1116+
Sections: []*DiffSection{
1117+
{
1118+
Lines: []*DiffLine{{LeftIdx: 1}},
1119+
},
11171120
},
1118-
},
1119-
}
1120-
ret := highlightCodeLines(diffFile, true, []byte("// abc\xcc def\xcd")) // ISO-8859-1 bytes
1121-
// FIXME: tag is not correctly closed
1122-
assert.Equal(t, `<span class="c1">// abcÌ defÍ`, string(ret[0]))
1121+
}
1122+
ret := highlightCodeLines(diffFile, true, []byte("// abc\xcc def\xcd")) // ISO-8859-1 bytes
1123+
assert.Equal(t, "<span class=\"c1\">// abcÌ defÍ\n</span>", string(ret[0]))
1124+
})
1125+
1126+
t.Run("LeftLines", func(t *testing.T) {
1127+
diffFile := &DiffFile{
1128+
Name: "a.c",
1129+
Language: "c",
1130+
Sections: []*DiffSection{
1131+
{
1132+
Lines: []*DiffLine{
1133+
{LeftIdx: 1},
1134+
{LeftIdx: 2},
1135+
{LeftIdx: 3},
1136+
},
1137+
},
1138+
},
1139+
}
1140+
const nl = "\n"
1141+
ret := highlightCodeLines(diffFile, true, []byte("a\nb\n"))
1142+
assert.Equal(t, map[int]template.HTML{
1143+
0: `<span class="n">a</span>` + nl,
1144+
1: `<span class="n">b</span>`,
1145+
}, ret)
1146+
})
11231147
}

services/gitdiff/highlightdiff_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ func TestDiffWithHighlight(t *testing.T) {
2525

2626
t.Run("CleanUp", func(t *testing.T) {
2727
hcd := newHighlightCodeDiff()
28-
codeA := template.HTML(`<span class="cm>this is a comment</span>`)
29-
codeB := template.HTML(`<span class="cm>this is updated comment</span>`)
28+
codeA := template.HTML(`<span class="cm">this is a comment</span>`)
29+
codeB := template.HTML(`<span class="cm">this is updated comment</span>`)
3030
outDel := hcd.diffLineWithHighlight(DiffLineDel, codeA, codeB)
31-
assert.Equal(t, `<span class="cm>this is <span class="removed-code">a</span> comment</span>`, string(outDel))
31+
assert.Equal(t, `<span class="cm">this is <span class="removed-code">a</span> comment</span>`, string(outDel))
3232
outAdd := hcd.diffLineWithHighlight(DiffLineAdd, codeA, codeB)
33-
assert.Equal(t, `<span class="cm>this is <span class="added-code">updated</span> comment</span>`, string(outAdd))
33+
assert.Equal(t, `<span class="cm">this is <span class="added-code">updated</span> comment</span>`, string(outAdd))
3434
})
3535

3636
t.Run("OpenCloseTags", func(t *testing.T) {

0 commit comments

Comments
 (0)