1package lexers
 2
 3import (
 4	"embed"
 5	"io/fs"
 6
 7	"github.com/alecthomas/chroma/v2"
 8)
 9
10//go:embed embedded
11var embedded embed.FS
12
13// GlobalLexerRegistry is the global LexerRegistry of Lexers.
14var GlobalLexerRegistry = func() *chroma.LexerRegistry {
15	reg := chroma.NewLexerRegistry()
16	// index(reg)
17	paths, err := fs.Glob(embedded, "embedded/*.xml")
18	if err != nil {
19		panic(err)
20	}
21	for _, path := range paths {
22		reg.Register(chroma.MustNewXMLLexer(embedded, path))
23	}
24	return reg
25}()
26
27// Names of all lexers, optionally including aliases.
28func Names(withAliases bool) []string {
29	return GlobalLexerRegistry.Names(withAliases)
30}
31
32// Aliases of all the lexers, and skip those lexers who do not have any aliases,
33// or show their name instead
34func Aliases(skipWithoutAliases bool) []string {
35	return GlobalLexerRegistry.Aliases(skipWithoutAliases)
36}
37
38// Get a Lexer by name, alias or file extension.
39//
40// Note that this if there isn't an exact match on name or alias, this will
41// call Match(), so it is not efficient.
42func Get(name string) chroma.Lexer {
43	return GlobalLexerRegistry.Get(name)
44}
45
46// MatchMimeType attempts to find a lexer for the given MIME type.
47func MatchMimeType(mimeType string) chroma.Lexer {
48	return GlobalLexerRegistry.MatchMimeType(mimeType)
49}
50
51// Match returns the first lexer matching filename.
52//
53// Note that this iterates over all file patterns in all lexers, so it's not
54// particularly efficient.
55func Match(filename string) chroma.Lexer {
56	return GlobalLexerRegistry.Match(filename)
57}
58
59// Register a Lexer with the global registry.
60func Register(lexer chroma.Lexer) chroma.Lexer {
61	return GlobalLexerRegistry.Register(lexer)
62}
63
64// Analyse text content and return the "best" lexer..
65func Analyse(text string) chroma.Lexer {
66	return GlobalLexerRegistry.Analyse(text)
67}
68
69// PlaintextRules is used for the fallback lexer as well as the explicit
70// plaintext lexer.
71func PlaintextRules() chroma.Rules {
72	return chroma.Rules{
73		"root": []chroma.Rule{
74			{`.+`, chroma.Text, nil},
75			{`\n`, chroma.Text, nil},
76		},
77	}
78}
79
80// Fallback lexer if no other is found.
81var Fallback chroma.Lexer = chroma.MustNewLexer(&chroma.Config{
82	Name:      "fallback",
83	Filenames: []string{"*"},
84	Priority:  -1,
85}, PlaintextRules)