mirror of
https://github.com/alecthomas/chroma.git
synced 2025-10-08 22:52:04 +02:00
Add lexers.Aliases() method to display only lexers' aliases (#1057)
`lexers.Aliases()` method shows all the aliases for all the available lexers, or their name if none are found. Using a flag, lexers without aliases can be skipped entirely. This request addresses https://github.com/alecthomas/chroma/issues/1048. Signed-off-by: LuBashQ <cristian.lupascu.nicolae@gmail.com>
This commit is contained in:
committed by
GitHub
parent
7feb358d4b
commit
62197e9ddb
@@ -29,6 +29,12 @@ func Names(withAliases bool) []string {
|
||||
return GlobalLexerRegistry.Names(withAliases)
|
||||
}
|
||||
|
||||
// Aliases of all the lexers, and skip those lexers who do not have any aliases,
|
||||
// or show their name instead
|
||||
func Aliases(skipWithoutAliases bool) []string {
|
||||
return GlobalLexerRegistry.Aliases(skipWithoutAliases)
|
||||
}
|
||||
|
||||
// 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
|
||||
|
@@ -44,6 +44,19 @@ func TestGet(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAliases(t *testing.T) {
|
||||
t.Run("UseNameIfNoAliases", func(t *testing.T) {
|
||||
expected := lexers.GlobalLexerRegistry.Aliases(false)
|
||||
actual := lexers.Aliases(false)
|
||||
assert.Equal(t, expected, actual)
|
||||
})
|
||||
t.Run("SkipIfNoAliases", func(t *testing.T) {
|
||||
expected := lexers.GlobalLexerRegistry.Aliases(true)
|
||||
actual := lexers.Aliases(true)
|
||||
assert.Equal(t, expected, actual)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGlobs(t *testing.T) {
|
||||
filename := "main.go"
|
||||
for _, lexer := range lexers.GlobalLexerRegistry.Lexers {
|
||||
|
18
registry.go
18
registry.go
@@ -48,6 +48,24 @@ func (l *LexerRegistry) Names(withAliases bool) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// Aliases of all the lexers, and skip those lexers who do not have any aliases,
|
||||
// or show their name instead
|
||||
func (l *LexerRegistry) Aliases(skipWithoutAliases bool) []string {
|
||||
out := []string{}
|
||||
for _, lexer := range l.Lexers {
|
||||
config := lexer.Config()
|
||||
if len(config.Aliases) == 0 {
|
||||
if skipWithoutAliases {
|
||||
continue
|
||||
}
|
||||
out = append(out, config.Name)
|
||||
}
|
||||
out = append(out, config.Aliases...)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// Get a Lexer by name, alias or file extension.
|
||||
func (l *LexerRegistry) Get(name string) Lexer {
|
||||
if lexer := l.byName[name]; lexer != nil {
|
||||
|
Reference in New Issue
Block a user