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.
Showing the changes to services/gitdiff/gitdiff.go (1 of 2 files). Show all
Unified · Split
| @@ -188,14 +188,14 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT · 187 unmodified lines | |||
| 188 | switch { | 188 | switch { |
| 189 | case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd: | 189 | case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd: |
| 190 | buf.Write(addedCodePrefix) | 190 | buf.Write(addedCodePrefix) |
| 191 | buf.WriteString(highlight.Code(fileName, diffs[i].Text)) | 191 | buf.WriteString(getLineContent(diffs[i].Text)) |
| 192 | buf.Write(codeTagSuffix) | 192 | buf.Write(codeTagSuffix) |
| 193 | case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel: | 193 | case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel: |
| 194 | buf.Write(removedCodePrefix) | 194 | buf.Write(removedCodePrefix) |
| 195 | buf.WriteString(highlight.Code(fileName, diffs[i].Text)) | 195 | buf.WriteString(getLineContent(diffs[i].Text)) |
| 196 | buf.Write(codeTagSuffix) | 196 | buf.Write(codeTagSuffix) |
| 197 | case diffs[i].Type == diffmatchpatch.DiffEqual: | 197 | case diffs[i].Type == diffmatchpatch.DiffEqual: |
| 198 | buf.WriteString(highlight.Code(fileName, getLineContent(diffs[i].Text))) | 198 | buf.WriteString(getLineContent(diffs[i].Text)) |
| 199 | } | 199 | } |
| 200 | } | 200 | } |
| 201 | return template.HTML(buf.Bytes()) | 201 | return template.HTML(buf.Bytes()) |
| @@ -287,7 +287,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem · 85 unmodified lines | |||
| 287 | return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content)) | 287 | return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content)) |
| 288 | } | 288 | } |
| 289 | 289 | ||
| 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) |
| 291 | diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord) | 291 | diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord) |
| 292 | return diffToHTML(diffSection.FileName, diffRecord, diffLine.Type) | 292 | return diffToHTML(diffSection.FileName, diffRecord, diffLine.Type) |
| 293 | } | 293 | } |