History

Ensure syntax highlighting is the same inside diffs (#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.

mrsdizzie committed 6 years ago

Commit 8d081951 parent 6a628842 files · +12 −8Browse files

Showing the changes to services/gitdiff/gitdiff.go (1 of 2 files). Show all

Unified · Split

services/gitdiff/gitdiff.go+4−4
@@ -188,14 +188,14 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT · 187 unmodified lines
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 · 85 unmodified lines
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 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t