Skip to content

Commit 8d08195

Browse files
authored
Ensure syntax highlighting is the same inside diffs (go-gitea#12205)
Make sure to end up with the same syntax highlighting inside various sections of diffs by processing the code first before detecting specific changes between the lines. Also try and make sure that when highlighting individual lines in a diff that it is tokenized the same as it would be when part of an entire file with more context. Fixes: go-gitea#12190
1 parent 6a62884 commit 8d08195

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

modules/highlight/highlight.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/setting"
17-
"github.com/alecthomas/chroma"
1817
"github.com/alecthomas/chroma/formatters/html"
1918
"github.com/alecthomas/chroma/lexers"
2019
"github.com/alecthomas/chroma/styles"
@@ -44,9 +43,12 @@ func NewContext() {
4443
func Code(fileName, code string) string {
4544
NewContext()
4645

47-
if code == "" {
46+
// diff view newline will be passed as empty, change to literal \n so it can be copied
47+
// preserve literal newline in blame view
48+
if code == "" || code == "\n" {
4849
return "\n"
4950
}
51+
5052
if len(code) > sizeLimit {
5153
return code
5254
}
@@ -72,7 +74,7 @@ func Code(fileName, code string) string {
7274
lexer = lexers.Fallback
7375
}
7476

75-
iterator, err := lexer.Tokenise(&chroma.TokeniseOptions{State: "root", Nested: true}, string(code))
77+
iterator, err := lexer.Tokenise(nil, string(code))
7678
if err != nil {
7779
log.Error("Can't tokenize code: %v", err)
7880
return code
@@ -85,7 +87,9 @@ func Code(fileName, code string) string {
8587
}
8688

8789
htmlw.Flush()
88-
return htmlbuf.String()
90+
// Chroma will add newlines for certain lexers in order to highlight them properly
91+
// Once highlighted, strip them here so they don't cause copy/paste trouble in HTML output
92+
return strings.TrimSuffix(htmlbuf.String(), "\n")
8993
}
9094

9195
// File returns map with line lumbers and HTML version of code with chroma syntax highlighting classes

services/gitdiff/gitdiff.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT
188188
switch {
189189
case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd:
190190
buf.Write(addedCodePrefix)
191-
buf.WriteString(highlight.Code(fileName, diffs[i].Text))
191+
buf.WriteString(getLineContent(diffs[i].Text))
192192
buf.Write(codeTagSuffix)
193193
case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
194194
buf.Write(removedCodePrefix)
195-
buf.WriteString(highlight.Code(fileName, diffs[i].Text))
195+
buf.WriteString(getLineContent(diffs[i].Text))
196196
buf.Write(codeTagSuffix)
197197
case diffs[i].Type == diffmatchpatch.DiffEqual:
198-
buf.WriteString(highlight.Code(fileName, getLineContent(diffs[i].Text)))
198+
buf.WriteString(getLineContent(diffs[i].Text))
199199
}
200200
}
201201
return template.HTML(buf.Bytes())
@@ -287,7 +287,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem
287287
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content))
288288
}
289289

290-
diffRecord := diffMatchPatch.DiffMain(diff1[1:], diff2[1:], true)
290+
diffRecord := diffMatchPatch.DiffMain(highlight.Code(diffSection.FileName, diff1[1:]), highlight.Code(diffSection.FileName, diff2[1:]), true)
291291
diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
292292
return diffToHTML(diffSection.FileName, diffRecord, diffLine.Type)
293293
}

0 commit comments

Comments
 (0)