Skip to content

Commit 9542b73

Browse files
mrsdizzielafriks
andauthored
Fix Syntax highlight for token change in added/deleted code (#12238)
* Fix Syntax highlight for token change in added/deleted code For diffs we first syntax highlight the before and after line then use a 3rd party diff library to find the difference in them and create a substring based on that, which we then highlight a 2nd time to show the specific difference within a line that has changed. In a specific case if the diffrence also changes the chroma class it will split in the middle of the attr and cause broken HTML: ``` <span class="nx">oldtext<span> <span class="k">var newtext<span> ``` Will then split on ``` <span class=" ``` Where the difference starts, and produce something broken like: ``` <span class="<span class="removed-code">nx"oldtext</span></span ``` Fix that by detecting when it happens and putting the HTML back together properly before highlighting the added/deleted code. Fixes #12235 * fix lint * apply fix to all diff sections. Also handle case where insert/remove starts with a closing span * Add a test for this new code * remove comment Co-authored-by: Lauris BH <lauris@nix.lv>
1 parent 6ea2701 commit 9542b73

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

services/gitdiff/gitdiff.go

+40-3
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,56 @@ var (
183183

184184
func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
185185
buf := bytes.NewBuffer(nil)
186-
186+
var addSpan bool
187187
for i := range diffs {
188188
switch {
189+
case diffs[i].Type == diffmatchpatch.DiffEqual:
190+
// Looking for the case where our 3rd party diff library previously detected a string difference
191+
// in the middle of a span class because we highlight them first. This happens when added/deleted code
192+
// also changes the chroma class name. If found, just move the openining span code forward into the next section
193+
if addSpan {
194+
diffs[i].Text = "<span class=\"" + diffs[i].Text
195+
}
196+
if strings.HasSuffix(diffs[i].Text, "<span class=\"") {
197+
addSpan = true
198+
buf.WriteString(strings.TrimSuffix(diffs[i].Text, "<span class=\""))
199+
} else {
200+
addSpan = false
201+
buf.WriteString(getLineContent(diffs[i].Text))
202+
}
189203
case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd:
204+
if addSpan {
205+
addSpan = false
206+
diffs[i].Text = "<span class=\"" + diffs[i].Text
207+
}
208+
// Print existing closing span first before opening added-code span so it doesn't unintentionally close it
209+
if strings.HasPrefix(diffs[i].Text, "</span>") {
210+
buf.WriteString("</span>")
211+
diffs[i].Text = strings.TrimPrefix(diffs[i].Text, "</span>")
212+
}
213+
if strings.HasSuffix(diffs[i].Text, "<span class=\"") {
214+
addSpan = true
215+
diffs[i].Text = strings.TrimSuffix(diffs[i].Text, "<span class=\"")
216+
}
190217
buf.Write(addedCodePrefix)
191218
buf.WriteString(getLineContent(diffs[i].Text))
192219
buf.Write(codeTagSuffix)
193220
case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
221+
if addSpan {
222+
addSpan = false
223+
diffs[i].Text = "<span class=\"" + diffs[i].Text
224+
}
225+
if strings.HasPrefix(diffs[i].Text, "</span>") {
226+
buf.WriteString("</span>")
227+
diffs[i].Text = strings.TrimPrefix(diffs[i].Text, "</span>")
228+
}
229+
if strings.HasSuffix(diffs[i].Text, "<span class=\"") {
230+
addSpan = true
231+
diffs[i].Text = strings.TrimSuffix(diffs[i].Text, "<span class=\"")
232+
}
194233
buf.Write(removedCodePrefix)
195234
buf.WriteString(getLineContent(diffs[i].Text))
196235
buf.Write(codeTagSuffix)
197-
case diffs[i].Type == diffmatchpatch.DiffEqual:
198-
buf.WriteString(getLineContent(diffs[i].Text))
199236
}
200237
}
201238
return template.HTML(buf.Bytes())

services/gitdiff/gitdiff_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func TestDiffToHTML(t *testing.T) {
4242
{Type: dmp.DiffInsert, Text: " baz"},
4343
{Type: dmp.DiffEqual, Text: " biz"},
4444
}, DiffLineDel))
45+
46+
assertEqual(t, "<span class=\"k\">if</span> <span class=\"p\">!</span><span class=\"nx\">nohl</span> <span class=\"o\">&amp;&amp;</span> <span class=\"added-code\"><span class=\"p\">(</span></span><span class=\"nx\">lexer</span> <span class=\"o\">!=</span> <span class=\"kc\">nil</span><span class=\"added-code\"> <span class=\"o\">||</span> <span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nx\">GuessLanguage</span><span class=\"p\">)</span></span> <span class=\"p\">{</span>", diffToHTML("", []dmp.Diff{
47+
{Type: dmp.DiffEqual, Text: "<span class=\"k\">if</span> <span class=\"p\">!</span><span class=\"nx\">nohl</span> <span class=\"o\">&amp;&amp;</span> <span class=\""},
48+
{Type: dmp.DiffInsert, Text: "p\">(</span><span class=\""},
49+
{Type: dmp.DiffEqual, Text: "nx\">lexer</span> <span class=\"o\">!=</span> <span class=\"kc\">nil"},
50+
{Type: dmp.DiffInsert, Text: "</span> <span class=\"o\">||</span> <span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nx\">GuessLanguage</span><span class=\"p\">)"},
51+
{Type: dmp.DiffEqual, Text: "</span> <span class=\"p\">{</span>"},
52+
}, DiffLineAdd))
4553
}
4654

4755
func TestParsePatch(t *testing.T) {

0 commit comments

Comments
 (0)