diff options
author | Dustin Firebaugh <dafirebaugh@gmail.com> | 2025-03-08 23:51:58 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-09 12:51:58 +0800 |
commit | 3f1f808b9eeb3f7cd923c6b89fbb57583202e76d (patch) | |
tree | 1eb4364b04aa43657f6846e5ee94c8bd7a405b1d /modules/git/blob.go | |
parent | 6f1333175461a6cf5497965c20b5d81b6e73c5d5 (diff) | |
download | gitea-3f1f808b9eeb3f7cd923c6b89fbb57583202e76d.tar.gz gitea-3f1f808b9eeb3f7cd923c6b89fbb57583202e76d.zip |
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 <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/git/blob.go')
-rw-r--r-- | modules/git/blob.go | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/modules/git/blob.go b/modules/git/blob.go index bcecb42e16..b7857dbbc6 100644 --- a/modules/git/blob.go +++ b/modules/git/blob.go @@ -7,6 +7,7 @@ package git import ( "bytes" "encoding/base64" + "errors" "io" "code.gitea.io/gitea/modules/typesniffer" @@ -34,8 +35,9 @@ func (b *Blob) GetBlobContent(limit int64) (string, error) { return string(buf), err } -// GetBlobLineCount gets line count of the blob -func (b *Blob) GetBlobLineCount() (int, error) { +// GetBlobLineCount gets line count of the blob. +// 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. +func (b *Blob) GetBlobLineCount(w io.Writer) (int, error) { reader, err := b.DataAsync() if err != nil { return 0, err @@ -44,20 +46,20 @@ func (b *Blob) GetBlobLineCount() (int, error) { buf := make([]byte, 32*1024) count := 1 lineSep := []byte{'\n'} - - c, err := reader.Read(buf) - if c == 0 && err == io.EOF { - return 0, nil - } for { + c, err := reader.Read(buf) + if w != nil { + if _, err := w.Write(buf[:c]); err != nil { + return count, err + } + } count += bytes.Count(buf[:c], lineSep) switch { - case err == io.EOF: + case errors.Is(err, io.EOF): return count, nil case err != nil: return count, err } - c, err = reader.Read(buf) } } |