-
-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathregistry.go
More file actions
220 lines (203 loc) · 5.32 KB
/
Copy pathregistry.go
File metadata and controls
220 lines (203 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package chroma
import (
"path/filepath"
"slices"
"strings"
)
var (
ignoredSuffixes = [...]string{
// Editor backups
"~", ".bak", ".old", ".orig",
// Debian and derivatives apt/dpkg/ucf backups
".dpkg-dist", ".dpkg-old", ".ucf-dist", ".ucf-new", ".ucf-old",
// Red Hat and derivatives rpm backups
".rpmnew", ".rpmorig", ".rpmsave",
// Build system input/template files
".in",
}
)
// LexerRegistry is a registry of Lexers.
//
// Registration is not synchronised: register all lexers (typically at init
// time) before using the registry concurrently.
type LexerRegistry struct {
Lexers Lexers
byName map[string]Lexer
byAlias map[string]Lexer
}
// NewLexerRegistry creates a new LexerRegistry of Lexers.
func NewLexerRegistry() *LexerRegistry {
return &LexerRegistry{
byName: map[string]Lexer{},
byAlias: map[string]Lexer{},
}
}
// Names of all lexers, optionally including aliases.
func (l *LexerRegistry) Names(withAliases bool) []string {
out := []string{}
for _, lexer := range l.Lexers {
config := lexer.Config()
out = append(out, config.Name)
if withAliases {
out = append(out, config.Aliases...)
}
}
slices.Sort(out)
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...)
}
slices.Sort(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 {
return lexer
}
if lexer := l.byAlias[name]; lexer != nil {
return lexer
}
if lexer := l.byName[strings.ToLower(name)]; lexer != nil {
return lexer
}
if lexer := l.byAlias[strings.ToLower(name)]; lexer != nil {
return lexer
}
candidates := PrioritisedLexers{}
// Try file extension.
if lexer := l.Match("filename." + name); lexer != nil {
candidates = append(candidates, lexer)
}
// Try exact filename.
if lexer := l.Match(name); lexer != nil {
candidates = append(candidates, lexer)
}
if len(candidates) == 0 {
return nil
}
return slices.MinFunc(candidates, compareLexersByPriority)
}
// MatchMimeType attempts to find a lexer for the given MIME type.
func (l *LexerRegistry) MatchMimeType(mimeType string) Lexer {
matched := PrioritisedLexers{}
for _, l := range l.Lexers {
for _, lmt := range l.Config().MimeTypes {
if mimeType == lmt {
matched = append(matched, l)
}
}
}
if len(matched) != 0 {
return slices.MinFunc(matched, compareLexersByPriority)
}
return nil
}
// 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)
// First, try primary filename matches.
matched := PrioritisedLexers{}
for _, lexer := range l.Lexers {
if matchGlobs(lexer.Config().Filenames, filename) {
matched = append(matched, lexer)
}
}
if len(matched) > 0 {
return slices.MinFunc(matched, compareLexersByPriority)
}
// Next, try filename aliases.
matched = nil
for _, lexer := range l.Lexers {
if matchGlobs(lexer.Config().AliasFilenames, filename) {
matched = append(matched, lexer)
}
}
if len(matched) > 0 {
return slices.MinFunc(matched, compareLexersByPriority)
}
return nil
}
// matchGlobs reports whether filename matches any of globs, either directly
// or with one of the ignoredSuffixes appended.
func matchGlobs(globs []string, filename string) bool {
for _, glob := range globs {
ok, err := filepath.Match(glob, filename)
if err != nil { // nolint
panic(err)
}
if ok {
return true
}
for _, suf := range &ignoredSuffixes {
ok, err := filepath.Match(glob+suf, filename)
if err != nil {
panic(err)
}
if ok {
return true
}
}
}
return false
}
// Analyse text content and return the "best" lexer..
func (l *LexerRegistry) Analyse(text string) Lexer {
var picked Lexer
highest := float32(0.0)
for _, lexer := range l.Lexers {
weight := lexer.AnalyseText(text)
if weight > highest {
picked = lexer
highest = weight
}
}
return picked
}
// Register a Lexer with the LexerRegistry. If the lexer is already registered
// it will be replaced.
//
// Not safe to call concurrently with any other method on the registry.
func (l *LexerRegistry) Register(lexer Lexer) Lexer {
lexer.SetRegistry(l)
config := lexer.Config()
// Consult byName before updating it so that registering a fresh lexer,
// the common case, does not scan the whole slice. byName is also keyed
// on lowercased names, so a hit only means a probable duplicate.
if l.byName[config.Name] == nil {
l.Lexers = append(l.Lexers, lexer)
} else {
replaced := false
for i, val := range l.Lexers {
if val != nil && val.Config().Name == config.Name {
l.Lexers[i] = lexer
replaced = true
break
}
}
if !replaced {
l.Lexers = append(l.Lexers, lexer)
}
}
l.byName[config.Name] = lexer
l.byName[strings.ToLower(config.Name)] = lexer
for _, alias := range config.Aliases {
l.byAlias[alias] = lexer
l.byAlias[strings.ToLower(alias)] = lexer
}
return lexer
}