Skip to content

Commit

Permalink
fix: attempt to load lexer files before using Get()
Browse files Browse the repository at this point in the history
File loading runs after Get(), but Get() always succeeds so it would
never load a file.
  • Loading branch information
alecthomas committed Nov 12, 2023
1 parent fe96ea4 commit 09953ff
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions cmd/chroma/main.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 09953ff

Please sign in to comment.