diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 7d08f63eb7..09a826500b 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -183,19 +183,56 @@ var ( func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { buf := bytes.NewBuffer(nil) - + var addSpan bool for i := range diffs { switch { + case diffs[i].Type == diffmatchpatch.DiffEqual: + // Looking for the case where our 3rd party diff library previously detected a string difference + // in the middle of a span class because we highlight them first. This happens when added/deleted code + // also changes the chroma class name. If found, just move the openining span code forward into the next section + if addSpan { + diffs[i].Text = "") { + buf.WriteString("") + diffs[i].Text = strings.TrimPrefix(diffs[i].Text, "") + } + if strings.HasSuffix(diffs[i].Text, "") { + buf.WriteString("") + diffs[i].Text = strings.TrimPrefix(diffs[i].Text, "") + } + if strings.HasSuffix(diffs[i].Text, "if !nohl && (lexer != nil || r.GuessLanguage) {", diffToHTML("", []dmp.Diff{ + {Type: dmp.DiffEqual, Text: "if !nohl && (lexer != nil"}, + {Type: dmp.DiffInsert, Text: " || r.GuessLanguage)"}, + {Type: dmp.DiffEqual, Text: " {"}, + }, DiffLineAdd)) } func TestParsePatch(t *testing.T) {