mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 14:18:26 +02:00
Allow reusing the token from chroma lexer
This commit is contained in:
@@ -182,7 +182,19 @@ func resolved_chroma_style() *chroma.Style {
|
|||||||
return style
|
return style
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tokens_map map[string][]chroma.Token
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
func highlight_file(path string) (highlighted string, err error) {
|
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)
|
filename_for_detection := filepath.Base(path)
|
||||||
ext := filepath.Ext(filename_for_detection)
|
ext := filepath.Ext(filename_for_detection)
|
||||||
if ext != "" {
|
if ext != "" {
|
||||||
@@ -196,6 +208,13 @@ func highlight_file(path string) (highlighted string, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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)
|
lexer := lexers.Match(filename_for_detection)
|
||||||
if lexer == nil {
|
if lexer == nil {
|
||||||
lexer = lexers.Analyse(text)
|
lexer = lexers.Analyse(text)
|
||||||
@@ -208,10 +227,15 @@ func highlight_file(path string) (highlighted string, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
tokens = iterator.Tokens()
|
||||||
|
mu.Lock()
|
||||||
|
tokens_map[path] = tokens
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
formatter := chroma.FormatterFunc(ansi_formatter)
|
formatter := chroma.FormatterFunc(ansi_formatter)
|
||||||
w := strings.Builder{}
|
w := strings.Builder{}
|
||||||
w.Grow(len(text) * 2)
|
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)
|
// os.WriteFile(filepath.Base(path+".highlighted"), []byte(w.String()), 0o600)
|
||||||
return w.String(), err
|
return w.String(), err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user