1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-19 21:10:15 +02:00

fix: attempt to load lexer files before using Get()

File loading runs after Get(), but Get() always succeeds so it would
never load a file.
This commit is contained in:
Alec Thomas 2023-11-13 07:56:30 +11:00
parent fe96ea49ef
commit 09953ff50f

View File

@ -366,11 +366,19 @@ func selexer(path, contents string) (lexer chroma.Lexer, err error) {
return lexers.Analyse(contents), nil
}
if lexer := lexers.Get(cli.Lexer); lexer != nil {
return lexer, nil
if _, err := os.Stat(cli.Lexer); err == nil {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
path, err := filepath.Rel(cwd, cli.Lexer)
if err != nil {
return nil, err
}
return chroma.NewXMLLexer(os.DirFS("."), path)
}
lexerPath, err := filepath.Abs(cli.Lexer)
return chroma.NewXMLLexer(os.DirFS("/"), lexerPath)
return lexers.Get(cli.Lexer), nil
}
func format(ctx *kong.Context, w io.Writer, style *chroma.Style, it chroma.Iterator) {