Skip to content

Commit 4f32b47

Browse files
committed
settings: Add glob: as prefix for file globbing maps
This gives the advantage to differentiate internal options from user defined file globs with the same name.
1 parent bcd6c81 commit 4f32b47

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

internal/config/settings.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,19 @@ func validateParsedSettings() error {
186186
}
187187
}
188188
} else {
189-
if _, e := glob.Compile(k); e != nil {
190-
err = errors.New("Error with glob setting " + k + ": " + e.Error())
189+
tk := strings.TrimPrefix(k, "glob:")
190+
if _, e := glob.Compile(tk); e != nil {
191+
err = errors.New("Error with glob setting " + tk + ": " + e.Error())
191192
delete(parsedSettings, k)
192193
continue
193194
}
195+
if !strings.HasPrefix(k, "glob:") {
196+
// Support non-prefixed glob settings but internally convert
197+
// them to prefixed ones for simplicity.
198+
delete(parsedSettings, k)
199+
k = "glob:" + k
200+
parsedSettings[k] = v
201+
}
194202
for k1, v1 := range v.(map[string]any) {
195203
if _, ok := defaults[k1]; ok {
196204
if e := verifySetting(k1, v1, defaults[k1]); e != nil {
@@ -312,8 +320,9 @@ func InitGlobalSettings() error {
312320
// Must be called after ReadSettings
313321
func UpdatePathGlobLocals(settings map[string]any, path string) {
314322
for k, v := range parsedSettings {
315-
if strings.HasPrefix(reflect.TypeOf(v).String(), "map") && !strings.HasPrefix(k, "ft:") {
316-
g, _ := glob.Compile(k)
323+
if strings.HasPrefix(reflect.TypeOf(v).String(), "map") && strings.HasPrefix(k, "glob:") {
324+
tk := strings.TrimPrefix(k, "glob:")
325+
g, _ := glob.Compile(tk)
317326
if g.MatchString(path) {
318327
for k1, v1 := range v.(map[string]any) {
319328
settings[k1] = v1

runtime/help/options.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ all files except Go files, and `tabsize` 4 for all files except Ruby files:
669669

670670
Or similarly you can match with globs:
671671

672+
```json
673+
{
674+
"glob:*.go": {
675+
"tabstospaces": false
676+
},
677+
"glob:*.rb": {
678+
"tabsize": 2
679+
},
680+
"tabstospaces": true,
681+
"tabsize": 4
682+
}
683+
```
684+
685+
You can also omit the `glob:` prefix before globs:
686+
672687
```json
673688
{
674689
"*.go": {
@@ -681,3 +696,6 @@ Or similarly you can match with globs:
681696
"tabsize": 4
682697
}
683698
```
699+
700+
But it is generally more recommended to use the `glob:` prefix, as it avoids
701+
potential conflicts with option names.

0 commit comments

Comments
 (0)