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.
This commit is contained in:
Jöran Karl
2026-03-14 15:20:21 +01:00
parent bcd6c81f50
commit 4f32b47075
2 changed files with 31 additions and 4 deletions

View File

@@ -186,11 +186,19 @@ func validateParsedSettings() error {
} }
} }
} else { } else {
if _, e := glob.Compile(k); e != nil { tk := strings.TrimPrefix(k, "glob:")
err = errors.New("Error with glob setting " + k + ": " + e.Error()) if _, e := glob.Compile(tk); e != nil {
err = errors.New("Error with glob setting " + tk + ": " + e.Error())
delete(parsedSettings, k) delete(parsedSettings, k)
continue continue
} }
if !strings.HasPrefix(k, "glob:") {
// Support non-prefixed glob settings but internally convert
// them to prefixed ones for simplicity.
delete(parsedSettings, k)
k = "glob:" + k
parsedSettings[k] = v
}
for k1, v1 := range v.(map[string]any) { for k1, v1 := range v.(map[string]any) {
if _, ok := defaults[k1]; ok { if _, ok := defaults[k1]; ok {
if e := verifySetting(k1, v1, defaults[k1]); e != nil { if e := verifySetting(k1, v1, defaults[k1]); e != nil {
@@ -312,8 +320,9 @@ func InitGlobalSettings() error {
// Must be called after ReadSettings // Must be called after ReadSettings
func UpdatePathGlobLocals(settings map[string]any, path string) { func UpdatePathGlobLocals(settings map[string]any, path string) {
for k, v := range parsedSettings { for k, v := range parsedSettings {
if strings.HasPrefix(reflect.TypeOf(v).String(), "map") && !strings.HasPrefix(k, "ft:") { if strings.HasPrefix(reflect.TypeOf(v).String(), "map") && strings.HasPrefix(k, "glob:") {
g, _ := glob.Compile(k) tk := strings.TrimPrefix(k, "glob:")
g, _ := glob.Compile(tk)
if g.MatchString(path) { if g.MatchString(path) {
for k1, v1 := range v.(map[string]any) { for k1, v1 := range v.(map[string]any) {
settings[k1] = v1 settings[k1] = v1

View File

@@ -669,6 +669,21 @@ all files except Go files, and `tabsize` 4 for all files except Ruby files:
Or similarly you can match with globs: Or similarly you can match with globs:
```json
{
"glob:*.go": {
"tabstospaces": false
},
"glob:*.rb": {
"tabsize": 2
},
"tabstospaces": true,
"tabsize": 4
}
```
You can also omit the `glob:` prefix before globs:
```json ```json
{ {
"*.go": { "*.go": {
@@ -681,3 +696,6 @@ Or similarly you can match with globs:
"tabsize": 4 "tabsize": 4
} }
``` ```
But it is generally more recommended to use the `glob:` prefix, as it avoids
potential conflicts with option names.