Skip to content

Commit

Permalink
Allow reusing the token from chroma lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Jan 4, 2025
1 parent e34a899 commit 98c1e0f
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions kittens/diff/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -196,22 +208,34 @@ func highlight_file(path string) (highlighted string, err error) {
if err != nil {
return "", err
}
lexer := lexers.Match(filename_for_detection)
if lexer == nil {
lexer = lexers.Analyse(text)
mu.Lock()
if tokens_map == nil {
tokens_map = make(map[string][]chroma.Token)
}
if lexer == nil {
return "", fmt.Errorf("Cannot highlight %#v: %w", path, ErrNoLexer)
}
lexer = chroma.Coalesce(lexer)
iterator, err := lexer.Tokenise(nil, text)
if err != nil {
return "", err
tokens := tokens_map[path]
mu.Unlock()
if tokens == nil {
lexer := lexers.Match(filename_for_detection)
if lexer == nil {
lexer = lexers.Analyse(text)
}
if lexer == nil {
return "", fmt.Errorf("Cannot highlight %#v: %w", path, ErrNoLexer)
}
lexer = chroma.Coalesce(lexer)
iterator, err := lexer.Tokenise(nil, text)
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
}
Expand Down

0 comments on commit 98c1e0f

Please sign in to comment.