aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authormrsdizzie <info@mrsdizzie.com>2020-07-11 01:43:12 -0400
committerGitHub <noreply@github.com>2020-07-11 13:43:12 +0800
commit8d081950e61d8f3d43544d566e810b448e8516a6 (patch)
treea59e00786162c05efa7202ba360e1d1adca1f688 /modules
parent6a62884782b29653bcd4462188e263018e5e21e8 (diff)
downloadgitea-8d081950e61d8f3d43544d566e810b448e8516a6.tar.gz
gitea-8d081950e61d8f3d43544d566e810b448e8516a6.zip
Ensure syntax highlighting is the same inside diffs (#12205)
Make sure to end up with the same syntax highlighting inside various sections of diffs by processing the code first before detecting specific changes between the lines. Also try and make sure that when highlighting individual lines in a diff that it is tokenized the same as it would be when part of an entire file with more context. Fixes: #12190
Diffstat (limited to 'modules')
-rw-r--r--modules/highlight/highlight.go12
1 files changed, 8 insertions, 4 deletions
diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go
index 31448ccd63..a2bf93ee92 100644
--- a/modules/highlight/highlight.go
+++ b/modules/highlight/highlight.go
@@ -14,7 +14,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
- "github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
@@ -44,9 +43,12 @@ func NewContext() {
func Code(fileName, code string) string {
NewContext()
- if code == "" {
+ // diff view newline will be passed as empty, change to literal \n so it can be copied
+ // preserve literal newline in blame view
+ if code == "" || code == "\n" {
return "\n"
}
+
if len(code) > sizeLimit {
return code
}
@@ -72,7 +74,7 @@ func Code(fileName, code string) string {
lexer = lexers.Fallback
}
- iterator, err := lexer.Tokenise(&chroma.TokeniseOptions{State: "root", Nested: true}, string(code))
+ iterator, err := lexer.Tokenise(nil, string(code))
if err != nil {
log.Error("Can't tokenize code: %v", err)
return code
@@ -85,7 +87,9 @@ func Code(fileName, code string) string {
}
htmlw.Flush()
- return htmlbuf.String()
+ // Chroma will add newlines for certain lexers in order to highlight them properly
+ // Once highlighted, strip them here so they don't cause copy/paste trouble in HTML output
+ return strings.TrimSuffix(htmlbuf.String(), "\n")
}
// File returns map with line lumbers and HTML version of code with chroma syntax highlighting classes