From d0ece7f70d0bfd3300bc14582969194905789037 Mon Sep 17 00:00:00 2001 From: mrsdizzie Date: Mon, 13 Jul 2020 10:50:02 -0400 Subject: [PATCH 1/5] 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: ``` oldtext var newtext ``` Will then split on ``` nx"oldtext Date: Mon, 13 Jul 2020 13:16:55 -0400 Subject: [PATCH 2/5] fix lint --- services/gitdiff/gitdiff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index c143e4840c16d..a0e4f13b3d9a7 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -192,7 +192,7 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT // also changes the chroma class name. If found, just move the openining span code forward into the added/deleted code section if strings.HasSuffix(diffs[i].Text, " Date: Mon, 13 Jul 2020 18:46:43 -0400 Subject: [PATCH 3/5] apply fix to all diff sections. Also handle case where insert/remove starts with a closing span --- services/gitdiff/gitdiff.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index a0e4f13b3d9a7..09a826500b461 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -189,7 +189,10 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT 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 added/deleted code section + // 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, " Date: Tue, 14 Jul 2020 00:14:50 -0400 Subject: [PATCH 4/5] Add a test for this new code --- services/gitdiff/gitdiff_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 0dc3f061554fa..9ef104494ed4c 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -42,8 +42,17 @@ func TestDiffToHTML(t *testing.T) { {Type: dmp.DiffInsert, Text: " baz"}, {Type: dmp.DiffEqual, Text: " biz"}, }, DiffLineDel)) + + assertEqual(t, "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)) } +//{Type:Insert Text: || r.GuessLanguage)} {Type:Equal Text: {}] func TestParsePatch(t *testing.T) { var diff = `diff --git "a/README.md" "b/README.md" --- a/README.md From 5d2f39bd93a3429032fe21da4e14aca00ddbb963 Mon Sep 17 00:00:00 2001 From: mrsdizzie Date: Tue, 14 Jul 2020 00:20:32 -0400 Subject: [PATCH 5/5] remove comment --- services/gitdiff/gitdiff_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 9ef104494ed4c..dabb49654e421 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -52,7 +52,6 @@ func TestDiffToHTML(t *testing.T) { }, DiffLineAdd)) } -//{Type:Insert Text: || r.GuessLanguage)} {Type:Equal Text: {}] func TestParsePatch(t *testing.T) { var diff = `diff --git "a/README.md" "b/README.md" --- a/README.md