Skip to content

Commit 8bb9de4

Browse files
committed
temp: add highlight fallback and FIXME comment
1 parent 6aa9099 commit 8bb9de4

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

services/gitdiff/gitdiff.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -294,21 +294,37 @@ type DiffInline struct {
294294
Content template.HTML
295295
}
296296

297-
// DiffInlineWithUnicodeEscape makes a DiffInline with hidden unicode characters escaped
297+
// DiffInlineWithUnicodeEscape makes a DiffInline with hidden Unicode characters escaped
298298
func DiffInlineWithUnicodeEscape(s template.HTML, locale translation.Locale) DiffInline {
299299
status, content := charset.EscapeControlHTML(s, locale)
300300
return DiffInline{EscapeStatus: status, Content: content}
301301
}
302302

303+
func (diffSection *DiffSection) getLineContentForRender(lineIdx int, diffLine *DiffLine, highlightLines map[int]template.HTML) template.HTML {
304+
h, ok := highlightLines[lineIdx-1]
305+
if ok {
306+
return h
307+
}
308+
if diffLine.Content == "" {
309+
return ""
310+
}
311+
if setting.Git.DisableDiffHighlight {
312+
return template.HTML(html.EscapeString(diffLine.Content[1:]))
313+
}
314+
315+
h, _ = highlight.Code(diffSection.Name, diffSection.file.Language, diffLine.Content[1:])
316+
return h
317+
}
318+
303319
func (diffSection *DiffSection) getDiffLineForRender(diffLineType DiffLineType, leftLine, rightLine *DiffLine, locale translation.Locale) DiffInline {
304320
hcd := newHighlightCodeDiff()
305321
var diff1, diff2, lineHTML template.HTML
306322
if leftLine != nil {
307-
diff1 = diffSection.file.highlightedOldLines[leftLine.LeftIdx-1]
323+
diff1 = diffSection.getLineContentForRender(leftLine.LeftIdx, leftLine, diffSection.file.highlightedOldLines)
308324
lineHTML = util.Iif(diffLineType == DiffLinePlain, diff1, "")
309325
}
310326
if rightLine != nil {
311-
diff2 = diffSection.file.highlightedNewLines[rightLine.RightIdx-1]
327+
diff2 = diffSection.getLineContentForRender(rightLine.RightIdx, rightLine, diffSection.file.highlightedNewLines)
312328
lineHTML = util.Iif(diffLineType == DiffLinePlain, diff2, "")
313329
}
314330
if diffLineType != DiffLinePlain {
@@ -322,11 +338,6 @@ func (diffSection *DiffSection) getDiffLineForRender(diffLineType DiffLineType,
322338

323339
// GetComputedInlineDiffFor computes inline diff for the given line.
324340
func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine, locale translation.Locale) DiffInline {
325-
// FIXME: should check DisableDiffHighlight in other places, but not here
326-
if setting.Git.DisableDiffHighlight {
327-
return getLineContent(diffLine.Content[1:], locale)
328-
}
329-
330341
// try to find equivalent diff line. ignore, otherwise
331342
switch diffLine.Type {
332343
case DiffLineSection:
@@ -373,8 +384,8 @@ type DiffFile struct {
373384
IsSubmodule bool // if IsSubmodule==true, then there must be a SubmoduleDiffInfo
374385
SubmoduleDiffInfo *SubmoduleDiffInfo
375386

376-
highlightedOldLines []template.HTML
377-
highlightedNewLines []template.HTML
387+
highlightedOldLines map[int]template.HTML // TODO: in the future, we only need to store the related diff lines to save memory
388+
highlightedNewLines map[int]template.HTML
378389
}
379390

380391
// GetType returns type of diff file.
@@ -1250,8 +1261,16 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
12501261
diffFile.Sections = append(diffFile.Sections, tailSection)
12511262
}
12521263

1253-
diffFile.highlightedOldLines = highlightCodeLines(diffFile, limitedContent.LeftContent.buf.String())
1254-
diffFile.highlightedNewLines = highlightCodeLines(diffFile, limitedContent.RightContent.buf.String())
1264+
if !setting.Git.DisableDiffHighlight {
1265+
// FIXME: it's not right to highlight code here for all cases, because this function is also used for non-rendering purpose
1266+
// For example: API
1267+
if limitedContent.LeftContent != nil && limitedContent.LeftContent.buf.Len() < MaxDiffHighlightEntireFileSize {
1268+
diffFile.highlightedOldLines = highlightCodeLines(diffFile, limitedContent.LeftContent.buf.String())
1269+
}
1270+
if limitedContent.RightContent != nil && limitedContent.RightContent.buf.Len() < MaxDiffHighlightEntireFileSize {
1271+
diffFile.highlightedNewLines = highlightCodeLines(diffFile, limitedContent.RightContent.buf.String())
1272+
}
1273+
}
12551274
}
12561275

12571276
if opts.FileOnly {
@@ -1268,11 +1287,12 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
12681287
return diff, nil
12691288
}
12701289

1271-
func highlightCodeLines(diffFile *DiffFile, content string) (lines []template.HTML) {
1290+
func highlightCodeLines(diffFile *DiffFile, content string) map[int]template.HTML {
12721291
highlightedNewContent, _ := highlight.Code(diffFile.Name, diffFile.Language, content)
12731292
splitLines := strings.Split(string(highlightedNewContent), "\n")
1274-
for _, line := range splitLines {
1275-
lines = append(lines, template.HTML(line))
1293+
lines := make(map[int]template.HTML, len(splitLines))
1294+
for i, line := range splitLines {
1295+
lines[i] = template.HTML(line)
12761296
}
12771297
return lines
12781298
}

0 commit comments

Comments
 (0)