Allow reusing the token from chroma lexer

This commit is contained in:
Kovid Goyal
2025-01-04 17:11:10 +05:30
parent e34a899ea6
commit 98c1e0f7aa

View File

@@ -182,7 +182,19 @@ func resolved_chroma_style() *chroma.Style {
return style
}
var tokens_map map[string][]chroma.Token
var mu sync.Mutex
func highlight_file(path string) (highlighted string, err error) {
defer func() {
if r := recover(); r != nil {
e, ok := r.(error)
if !ok {
e = fmt.Errorf("%v", r)
}
err = e
}
}()
filename_for_detection := filepath.Base(path)
ext := filepath.Ext(filename_for_detection)
if ext != "" {
@@ -196,6 +208,13 @@ func highlight_file(path string) (highlighted string, err error) {
if err != nil {
return "", err
}
mu.Lock()
if tokens_map == nil {
tokens_map = make(map[string][]chroma.Token)
}
tokens := tokens_map[path]
mu.Unlock()
if tokens == nil {
lexer := lexers.Match(filename_for_detection)
if lexer == nil {
lexer = lexers.Analyse(text)
@@ -208,10 +227,15 @@ func highlight_file(path string) (highlighted string, err error) {
if err != nil {
return "", err
}
tokens = iterator.Tokens()
mu.Lock()
tokens_map[path] = tokens
mu.Unlock()
}
formatter := chroma.FormatterFunc(ansi_formatter)
w := strings.Builder{}
w.Grow(len(text) * 2)
err = formatter.Format(&w, resolved_chroma_style(), iterator)
err = formatter.Format(&w, resolved_chroma_style(), chroma.Literator(tokens...))
// os.WriteFile(filepath.Base(path+".highlighted"), []byte(w.String()), 0o600)
return w.String(), err
}