action/command: Refactor reload & the filetype change

This commit is contained in:
Jöran Karl 2024-08-12 20:26:51 +02:00
parent 724e29446f
commit a3071b173b
2 changed files with 39 additions and 31 deletions

View File

@ -408,15 +408,7 @@ func reloadRuntime(reloadPlugins bool) {
screen.TermMessage(err)
}
for _, b := range buffer.OpenBuffers {
config.InitLocalSettings(b.Settings, b.Path)
for k, v := range b.Settings {
if _, ok := b.LocalSettings[k]; ok {
// reload should not override local settings
continue
}
b.DoSetOptionNative(k, v)
}
b.UpdateRules()
b.ReloadSettings(true)
}
}

View File

@ -8,6 +8,43 @@ import (
"github.com/zyedidia/micro/v2/internal/screen"
)
func (b *Buffer) ReloadSettings(reloadFiletype bool) {
settings := config.ParsedSettings()
if _, ok := b.LocalSettings["filetype"]; !ok && reloadFiletype {
// need to update filetype before updating other settings based on it
b.Settings["filetype"] = "unknown"
if v, ok := settings["filetype"]; ok {
b.Settings["filetype"] = v
}
}
// update syntax rules, which will also update filetype if needed
b.UpdateRules()
settings["filetype"] = b.Settings["filetype"]
config.InitLocalSettings(settings, b.Path)
for k, v := range config.DefaultCommonSettings() {
if k == "filetype" {
// prevent recursion
continue
}
if _, ok := config.VolatileSettings[k]; ok {
// reload should not override volatile settings
continue
}
if _, ok := b.LocalSettings[k]; ok {
// reload should not override local settings
continue
}
if _, ok := settings[k]; ok {
b.DoSetOptionNative(k, settings[k])
} else {
b.DoSetOptionNative(k, v)
}
}
}
func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
if reflect.DeepEqual(b.Settings[option], nativeValue) {
return
@ -31,28 +68,7 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
} else if option == "statusline" {
screen.Redraw()
} else if option == "filetype" {
settings := config.ParsedSettings()
settings["filetype"] = nativeValue
config.InitLocalSettings(settings, b.Path)
for k, v := range config.DefaultCommonSettings() {
if k == "filetype" {
continue
}
if _, ok := config.VolatileSettings[k]; ok {
// 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.DoSetOptionNative(k, settings[k])
} else {
b.DoSetOptionNative(k, v)
}
}
b.UpdateRules()
b.ReloadSettings(false)
} else if option == "fileformat" {
switch b.Settings["fileformat"].(string) {
case "unix":