action/command: Prevent overwriting settings set locally or by plugin

- on `reload`
- on `filetype` change
This commit is contained in:
Jöran Karl 2024-07-26 21:06:06 +02:00
parent f661b64e0a
commit 05529596cf
3 changed files with 23 additions and 12 deletions

View File

@ -410,7 +410,7 @@ func reloadRuntime(reloadPlugins bool) {
for _, b := range buffer.OpenBuffers { for _, b := range buffer.OpenBuffers {
config.InitLocalSettings(b.Settings, b.Path) config.InitLocalSettings(b.Settings, b.Path)
for k, v := range b.Settings { for k, v := range b.Settings {
b.SetOptionNative(k, v) b.DoSetOptionNative(k, v)
} }
b.UpdateRules() b.UpdateRules()
} }
@ -610,9 +610,8 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
// ...at last check the buffer locals // ...at last check the buffer locals
for _, b := range buffer.OpenBuffers { for _, b := range buffer.OpenBuffers {
if err := b.SetOptionNative(option, nativeValue); err != nil { b.DoSetOptionNative(option, nativeValue)
return err delete(b.LocalSettings, option)
}
} }
return config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json")) return config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))

View File

@ -86,6 +86,8 @@ type SharedBuffer struct {
// Settings customized by the user // Settings customized by the user
Settings map[string]interface{} Settings map[string]interface{}
// LocalSettings customized by the user for this buffer only
LocalSettings map[string]bool
Suggestions []string Suggestions []string
Completions []string Completions []string
@ -326,6 +328,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
// assigning the filetype. // assigning the filetype.
settings := config.DefaultCommonSettings() settings := config.DefaultCommonSettings()
b.Settings = config.DefaultCommonSettings() b.Settings = config.DefaultCommonSettings()
b.LocalSettings = make(map[string]bool)
for k, v := range config.GlobalSettings { for k, v := range config.GlobalSettings {
if _, ok := config.DefaultGlobalOnlySettings[k]; !ok { if _, ok := config.DefaultGlobalOnlySettings[k]; !ok {
// make sure setting is not global-only // make sure setting is not global-only

View File

@ -8,13 +8,9 @@ import (
"github.com/zyedidia/micro/v2/internal/screen" "github.com/zyedidia/micro/v2/internal/screen"
) )
func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
if err := config.OptionIsValid(option, nativeValue); err != nil {
return err
}
if reflect.DeepEqual(b.Settings[option], nativeValue) { if reflect.DeepEqual(b.Settings[option], nativeValue) {
return nil return
} }
b.Settings[option] = nativeValue b.Settings[option] = nativeValue
@ -46,10 +42,14 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
// filetype should not override volatile settings // filetype should not override volatile settings
continue continue
} }
if _, ok := b.LocalSettings[k]; ok {
// filetype should not override local settings
continue
}
if _, ok := settings[k]; ok { if _, ok := settings[k]; ok {
b.SetOptionNative(k, settings[k]) b.DoSetOptionNative(k, settings[k])
} else { } else {
b.SetOptionNative(k, v) b.DoSetOptionNative(k, v)
} }
} }
b.UpdateRules() b.UpdateRules()
@ -101,6 +101,15 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
if b.OptionCallback != nil { if b.OptionCallback != nil {
b.OptionCallback(option, nativeValue) b.OptionCallback(option, nativeValue)
} }
}
func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
if err := config.OptionIsValid(option, nativeValue); err != nil {
return err
}
b.DoSetOptionNative(option, nativeValue)
b.LocalSettings[option] = true
return nil return nil
} }