Skip to content

Commit 3f1f808

Browse files
Full-file syntax highlighting for diff pages (#33766)
Fix #33358, fix #21970 This adds a step in the `GitDiffForRender` that does syntax highlighting for the entire file and then only references lines from that syntax highlighted code. This allows things like multi-line comments to be syntax highlighted correctly. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 6f13331 commit 3f1f808

File tree

14 files changed

+362
-324
lines changed

14 files changed

+362
-324
lines changed

modules/git/blob.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package git
77
import (
88
"bytes"
99
"encoding/base64"
10+
"errors"
1011
"io"
1112

1213
"code.gitea.io/gitea/modules/typesniffer"
@@ -34,8 +35,9 @@ func (b *Blob) GetBlobContent(limit int64) (string, error) {
3435
return string(buf), err
3536
}
3637

37-
// GetBlobLineCount gets line count of the blob
38-
func (b *Blob) GetBlobLineCount() (int, error) {
38+
// GetBlobLineCount gets line count of the blob.
39+
// It will also try to write the content to w if it's not nil, then we could pre-fetch the content without reading it again.
40+
func (b *Blob) GetBlobLineCount(w io.Writer) (int, error) {
3941
reader, err := b.DataAsync()
4042
if err != nil {
4143
return 0, err
@@ -44,20 +46,20 @@ func (b *Blob) GetBlobLineCount() (int, error) {
4446
buf := make([]byte, 32*1024)
4547
count := 1
4648
lineSep := []byte{'\n'}
47-
48-
c, err := reader.Read(buf)
49-
if c == 0 && err == io.EOF {
50-
return 0, nil
51-
}
5249
for {
50+
c, err := reader.Read(buf)
51+
if w != nil {
52+
if _, err := w.Write(buf[:c]); err != nil {
53+
return count, err
54+
}
55+
}
5356
count += bytes.Count(buf[:c], lineSep)
5457
switch {
55-
case err == io.EOF:
58+
case errors.Is(err, io.EOF):
5659
return count, nil
5760
case err != nil:
5861
return count, err
5962
}
60-
c, err = reader.Read(buf)
6163
}
6264
}
6365

modules/highlight/highlight.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
gohtml "html"
1212
"html/template"
1313
"io"
14+
"path"
1415
"path/filepath"
1516
"strings"
1617
"sync"
@@ -83,7 +84,7 @@ func Code(fileName, language, code string) (output template.HTML, lexerName stri
8384
}
8485

8586
if lexer == nil {
86-
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
87+
if val, ok := highlightMapping[path.Ext(fileName)]; ok {
8788
// use mapped value to find lexer
8889
lexer = lexers.Get(val)
8990
}

routers/api/v1/repo/pull.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,7 @@ func GetPullRequestFiles(ctx *context.APIContext) {
15911591
maxLines := setting.Git.MaxGitDiffLines
15921592

15931593
// FIXME: If there are too many files in the repo, may cause some unpredictable issues.
1594-
// FIXME: it doesn't need to call "GetDiff" to do various parsing and highlighting
1595-
diff, err := gitdiff.GetDiff(ctx, baseGitRepo,
1594+
diff, err := gitdiff.GetDiffForAPI(ctx, baseGitRepo,
15961595
&gitdiff.DiffOptions{
15971596
BeforeCommitID: startCommitID,
15981597
AfterCommitID: endCommitID,

routers/web/repo/blame.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func RefBlame(ctx *context.Context) {
9797
return
9898
}
9999

100-
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
100+
ctx.Data["NumLines"], err = blob.GetBlobLineCount(nil)
101101
if err != nil {
102102
ctx.NotFound(err)
103103
return

routers/web/repo/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func Diff(ctx *context.Context) {
314314
maxLines, maxFiles = -1, -1
315315
}
316316

317-
diff, err := gitdiff.GetDiff(ctx, gitRepo, &gitdiff.DiffOptions{
317+
diff, err := gitdiff.GetDiffForRender(ctx, gitRepo, &gitdiff.DiffOptions{
318318
AfterCommitID: commitID,
319319
SkipTo: ctx.FormString("skip-to"),
320320
MaxLines: maxLines,

routers/web/repo/compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ func PrepareCompareDiff(
614614

615615
fileOnly := ctx.FormBool("file-only")
616616

617-
diff, err := gitdiff.GetDiff(ctx, ci.HeadGitRepo,
617+
diff, err := gitdiff.GetDiffForRender(ctx, ci.HeadGitRepo,
618618
&gitdiff.DiffOptions{
619619
BeforeCommitID: beforeCommitID,
620620
AfterCommitID: headCommitID,

routers/web/repo/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
749749
diffOptions.BeforeCommitID = startCommitID
750750
}
751751

752-
diff, err := gitdiff.GetDiff(ctx, gitRepo, diffOptions, files...)
752+
diff, err := gitdiff.GetDiffForRender(ctx, gitRepo, diffOptions, files...)
753753
if err != nil {
754754
ctx.ServerError("GetDiff", err)
755755
return

0 commit comments

Comments
 (0)