Skip to content

Commit 4362fa6

Browse files
committed
fix diff compare in one line
Signed-off-by: a1012112796 <[email protected]>
1 parent 424a57e commit 4362fa6

File tree

4 files changed

+42
-37
lines changed

4 files changed

+42
-37
lines changed

routers/web/repo/compare.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
933933
}
934934

935935
var (
936-
scanner *bufio.Scanner
936+
scanner *bufio.Scanner
937937
highlightedContent []string
938938
)
939939

@@ -967,10 +967,10 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
967967
}
968968
lineText := scanner.Text()
969969
diffLine := &gitdiff.DiffLine{
970-
LeftIdx: idxLeft + (line - idxRight) + 1,
971-
RightIdx: line + 1,
972-
Type: gitdiff.DiffLinePlain,
973-
Content: " " + lineText,
970+
LeftIdx: idxLeft + (line - idxRight) + 1,
971+
RightIdx: line + 1,
972+
Type: gitdiff.DiffLinePlain,
973+
Content: " " + lineText,
974974
}
975975

976976
if highlightedContent != nil {

services/gitdiff/gitdiff.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine, loc
294294

295295
var (
296296
compareDiffLine *DiffLine
297-
diff1 string
298-
diff2 string
297+
diff1 *DiffLine
298+
diff2 *DiffLine
299299
)
300300

301301
language := ""
@@ -308,29 +308,29 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine, loc
308308
case DiffLineSection:
309309
return getLineContent(diffLine.Content[1:], locale)
310310
case DiffLineAdd:
311-
if diffLine.HasHighlightContent {
312-
status, content := charset.EscapeControlHTML(diffLine.HighlightContent, locale)
313-
return DiffInline{EscapeStatus: status, Content: template.HTML(content)}
314-
}
315-
316311
compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
317312
if compareDiffLine == nil {
313+
if diffLine.HasHighlightContent {
314+
status, content := charset.EscapeControlHTML(diffLine.HighlightContent, locale)
315+
return DiffInline{EscapeStatus: status, Content: template.HTML(content)}
316+
}
317+
318318
return DiffInlineWithHighlightCode(diffSection.FileName, language, diffLine.Content[1:], locale)
319319
}
320-
diff1 = compareDiffLine.Content
321-
diff2 = diffLine.Content
320+
diff1 = compareDiffLine
321+
diff2 = diffLine
322322
case DiffLineDel:
323-
if diffLine.HasHighlightContent {
324-
status, content := charset.EscapeControlHTML(diffLine.HighlightContent, locale)
325-
return DiffInline{EscapeStatus: status, Content: template.HTML(content)}
326-
}
327-
328323
compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
329324
if compareDiffLine == nil {
325+
if diffLine.HasHighlightContent {
326+
status, content := charset.EscapeControlHTML(diffLine.HighlightContent, locale)
327+
return DiffInline{EscapeStatus: status, Content: template.HTML(content)}
328+
}
329+
330330
return DiffInlineWithHighlightCode(diffSection.FileName, language, diffLine.Content[1:], locale)
331331
}
332-
diff1 = diffLine.Content
333-
diff2 = compareDiffLine.Content
332+
diff1 = diffLine
333+
diff2 = compareDiffLine
334334
default:
335335
if strings.IndexByte(" +-", diffLine.Content[0]) > -1 {
336336
if diffLine.HasHighlightContent {
@@ -344,7 +344,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine, loc
344344
}
345345

346346
hcd := newHighlightCodeDiff()
347-
diffRecord := hcd.diffWithHighlight(diffSection.FileName, language, diff1[1:], diff2[1:])
347+
diffRecord := hcd.diffWithHighlight(diffSection.FileName, language, diff1.Content[1:], diff1.HighlightContent, diff2.Content[1:], diff2.HighlightContent)
348348
// it seems that Gitea doesn't need the line wrapper of Chroma, so do not add them back
349349
// if the line wrappers are still needed in the future, it can be added back by "diffToHTML(hcd.lineWrapperTags. ...)"
350350
diffHTML := diffToHTML(nil, diffRecord, diffLine.Type)

services/gitdiff/highlightdiff.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,17 @@ func (hcd *highlightCodeDiff) collectUsedRunes(code string) {
8686
}
8787
}
8888

89-
func (hcd *highlightCodeDiff) diffWithHighlight(filename, language, codeA, codeB string) []diffmatchpatch.Diff {
89+
func (hcd *highlightCodeDiff) diffWithHighlight(filename, language, codeA, highlightCodeA, codeB, highlightCodeB string) []diffmatchpatch.Diff {
9090
hcd.collectUsedRunes(codeA)
9191
hcd.collectUsedRunes(codeB)
9292

93-
highlightCodeA, _ := highlight.Code(filename, language, codeA)
94-
highlightCodeB, _ := highlight.Code(filename, language, codeB)
93+
if len(highlightCodeA) == 0 {
94+
highlightCodeA, _ = highlight.Code(filename, language, codeA)
95+
}
96+
97+
if len(highlightCodeB) == 0 {
98+
highlightCodeB, _ = highlight.Code(filename, language, codeB)
99+
}
95100

96101
highlightCodeA = hcd.convertToPlaceholders(highlightCodeA)
97102
highlightCodeB = hcd.convertToPlaceholders(highlightCodeB)

services/gitdiff/highlightdiff_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func TestDiffWithHighlight(t *testing.T) {
1616
hcd := newHighlightCodeDiff()
1717
diffs := hcd.diffWithHighlight(
1818
"main.v", "",
19-
" run('<>')\n",
20-
" run(db)\n",
19+
" run('<>')\n", "",
20+
" run(db)\n", "",
2121
)
2222

2323
expected := ` <span class="n">run</span><span class="o">(</span><span class="removed-code"><span class="k">&#39;</span><span class="o">&lt;</span><span class="o">&gt;</span><span class="k">&#39;</span></span><span class="o">)</span>`
@@ -50,8 +50,8 @@ func TestDiffWithHighlightPlaceholder(t *testing.T) {
5050
hcd := newHighlightCodeDiff()
5151
diffs := hcd.diffWithHighlight(
5252
"main.js", "",
53-
"a='\U00100000'",
54-
"a='\U0010FFFD''",
53+
"a='\U00100000'", "",
54+
"a='\U0010FFFD''", "",
5555
)
5656
assert.Equal(t, "", hcd.placeholderTokenMap[0x00100000])
5757
assert.Equal(t, "", hcd.placeholderTokenMap[0x0010FFFD])
@@ -63,8 +63,8 @@ func TestDiffWithHighlightPlaceholder(t *testing.T) {
6363
hcd = newHighlightCodeDiff()
6464
diffs = hcd.diffWithHighlight(
6565
"main.js", "",
66-
"a='\U00100000'",
67-
"a='\U0010FFFD'",
66+
"a='\U00100000'", "",
67+
"a='\U0010FFFD'", "",
6868
)
6969
expected = fmt.Sprintf(`<span class="nx">a</span><span class="o">=</span><span class="s1">&#39;</span><span class="added-code">%s</span>&#39;`, "\U0010FFFD")
7070
output = diffToHTML(nil, diffs, DiffLineAdd)
@@ -76,8 +76,8 @@ func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
7676
hcd.placeholderMaxCount = 0
7777
diffs := hcd.diffWithHighlight(
7878
"main.js", "",
79-
"'",
80-
``,
79+
"'", "",
80+
``, "",
8181
)
8282
output := diffToHTML(nil, diffs, DiffLineDel)
8383
expected := fmt.Sprintf(`<span class="removed-code">%s#39;</span>`, "\uFFFD")
@@ -87,8 +87,8 @@ func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
8787
hcd.placeholderMaxCount = 0
8888
diffs = hcd.diffWithHighlight(
8989
"main.js", "",
90-
"a < b",
91-
"a > b",
90+
"a < b", "",
91+
"a > b", "",
9292
)
9393
output = diffToHTML(nil, diffs, DiffLineDel)
9494
expected = fmt.Sprintf(`a %s<span class="removed-code">l</span>t; b`, "\uFFFD")
@@ -106,8 +106,8 @@ func TestDiffWithHighlightTagMatch(t *testing.T) {
106106
hcd.placeholderMaxCount = i
107107
diffs := hcd.diffWithHighlight(
108108
"main.js", "",
109-
"a='1'",
110-
"b='2'",
109+
"a='1'", "",
110+
"b='2'", "",
111111
)
112112
totalOverflow += hcd.placeholderOverflowCount
113113

0 commit comments

Comments
 (0)