File tree Expand file tree Collapse file tree 1 file changed +25
-4
lines changed
pkg/tui/components/tool/editfile Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Original file line number Diff line number Diff line change 44 "encoding/json"
55 "fmt"
66 "os"
7+ "path/filepath"
78 "strings"
9+ "sync"
810
911 "charm.land/lipgloss/v2"
1012 "github.com/alecthomas/chroma/v2"
@@ -25,6 +27,11 @@ const (
2527 minWidth = 80
2628)
2729
30+ var (
31+ lexerCache = make (map [string ]chroma.Lexer )
32+ lexerCacheMu sync.RWMutex
33+ )
34+
2835type chromaToken struct {
2936 Text string
3037 Style lipgloss.Style
@@ -127,11 +134,25 @@ func normalizeDiff(diff []*udiff.Hunk) []*udiff.Hunk {
127134}
128135
129136func syntaxHighlight (code , filePath string ) []chromaToken {
130- lexer := lexers .Match (filePath )
131- if lexer == nil {
132- lexer = lexers .Fallback
137+ ext := filepath .Ext (filePath )
138+
139+ // Try to get lexer from cache
140+ lexerCacheMu .RLock ()
141+ lexer , ok := lexerCache [ext ]
142+ lexerCacheMu .RUnlock ()
143+
144+ if ! ok {
145+ // Cache miss - compute and store
146+ lexer = lexers .Match (filePath )
147+ if lexer == nil {
148+ lexer = lexers .Fallback
149+ }
150+ lexer = chroma .Coalesce (lexer )
151+
152+ lexerCacheMu .Lock ()
153+ lexerCache [ext ] = lexer
154+ lexerCacheMu .Unlock ()
133155 }
134- lexer = chroma .Coalesce (lexer )
135156
136157 style := styles .ChromaStyle ()
137158 iterator , err := lexer .Tokenise (nil , code )
You can’t perform that action at this time.
0 commit comments