Skip to content

Commit

Permalink
docs: note about Get() being slow due to file matching
Browse files Browse the repository at this point in the history
Fixes #714
  • Loading branch information
alecthomas committed Sep 21, 2023
1 parent ccd8d68 commit 9087c63
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lexers/lexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func Names(withAliases bool) []string {
}

// Get a Lexer by name, alias or file extension.
//
// Note that this if there isn't an exact match on name or alias, this will
// call Match(), so it is not efficient.
func Get(name string) chroma.Lexer {
return GlobalLexerRegistry.Get(name)
}
Expand All @@ -40,6 +43,9 @@ func MatchMimeType(mimeType string) chroma.Lexer {
}

// Match returns the first lexer matching filename.
//
// Note that this iterates over all file patterns in all lexers, so it's not
// particularly efficient.
func Match(filename string) chroma.Lexer {
return GlobalLexerRegistry.Match(filename)
}
Expand Down
1 change: 0 additions & 1 deletion lexers/lexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func FileTestAnalysis(t *testing.T, lexer chroma.Lexer, actualFilepath, expected
assert.NoError(t, err)
assert.NoError(t, f.Close())
} else {
// fail via an assertion of string comparison for nicer diff output
assert.Equal(t, string(expectedData), actualData.String())
}
}
Expand Down
2 changes: 2 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func (l *LexerRegistry) MatchMimeType(mimeType string) Lexer {
}

// Match returns the first lexer matching filename.
//
// Note that this iterates over all file patterns in all lexers, so is not fast.
func (l *LexerRegistry) Match(filename string) Lexer {
filename = filepath.Base(filename)
matched := PrioritisedLexers{}
Expand Down

0 comments on commit 9087c63

Please sign in to comment.