From 05529596cf321ae4b36a46ac88876d0e012fc832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 26 Jul 2024 21:06:06 +0200 Subject: [PATCH] action/command: Prevent overwriting settings set locally or by plugin - on `reload` - on `filetype` change --- internal/action/command.go | 7 +++---- internal/buffer/buffer.go | 3 +++ internal/buffer/settings.go | 25 +++++++++++++++++-------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/internal/action/command.go b/internal/action/command.go index 502365aa..d29aeb03 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -410,7 +410,7 @@ func reloadRuntime(reloadPlugins bool) { for _, b := range buffer.OpenBuffers { config.InitLocalSettings(b.Settings, b.Path) for k, v := range b.Settings { - b.SetOptionNative(k, v) + b.DoSetOptionNative(k, v) } b.UpdateRules() } @@ -610,9 +610,8 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error { // ...at last check the buffer locals for _, b := range buffer.OpenBuffers { - if err := b.SetOptionNative(option, nativeValue); err != nil { - return err - } + b.DoSetOptionNative(option, nativeValue) + delete(b.LocalSettings, option) } return config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json")) diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index 15cfe335..e8f6e98b 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -86,6 +86,8 @@ type SharedBuffer struct { // Settings customized by the user Settings map[string]interface{} + // LocalSettings customized by the user for this buffer only + LocalSettings map[string]bool Suggestions []string Completions []string @@ -326,6 +328,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT // assigning the filetype. settings := config.DefaultCommonSettings() b.Settings = config.DefaultCommonSettings() + b.LocalSettings = make(map[string]bool) for k, v := range config.GlobalSettings { if _, ok := config.DefaultGlobalOnlySettings[k]; !ok { // make sure setting is not global-only diff --git a/internal/buffer/settings.go b/internal/buffer/settings.go index 8d0e8aa1..9fc1cfd8 100644 --- a/internal/buffer/settings.go +++ b/internal/buffer/settings.go @@ -8,13 +8,9 @@ import ( "github.com/zyedidia/micro/v2/internal/screen" ) -func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { - if err := config.OptionIsValid(option, nativeValue); err != nil { - return err - } - +func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) { if reflect.DeepEqual(b.Settings[option], nativeValue) { - return nil + return } b.Settings[option] = nativeValue @@ -46,10 +42,14 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { // filetype should not override volatile settings continue } + if _, ok := b.LocalSettings[k]; ok { + // filetype should not override local settings + continue + } if _, ok := settings[k]; ok { - b.SetOptionNative(k, settings[k]) + b.DoSetOptionNative(k, settings[k]) } else { - b.SetOptionNative(k, v) + b.DoSetOptionNative(k, v) } } b.UpdateRules() @@ -101,6 +101,15 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { if b.OptionCallback != nil { 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 }