Skip to content

Commit a3071b1

Browse files
committed
action/command: Refactor reload & the filetype change
1 parent 724e294 commit a3071b1

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

internal/action/command.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,7 @@ func reloadRuntime(reloadPlugins bool) {
408408
screen.TermMessage(err)
409409
}
410410
for _, b := range buffer.OpenBuffers {
411-
config.InitLocalSettings(b.Settings, b.Path)
412-
for k, v := range b.Settings {
413-
if _, ok := b.LocalSettings[k]; ok {
414-
// reload should not override local settings
415-
continue
416-
}
417-
b.DoSetOptionNative(k, v)
418-
}
419-
b.UpdateRules()
411+
b.ReloadSettings(true)
420412
}
421413
}
422414

internal/buffer/settings.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@ import (
88
"github.com/zyedidia/micro/v2/internal/screen"
99
)
1010

11+
func (b *Buffer) ReloadSettings(reloadFiletype bool) {
12+
settings := config.ParsedSettings()
13+
14+
if _, ok := b.LocalSettings["filetype"]; !ok && reloadFiletype {
15+
// need to update filetype before updating other settings based on it
16+
b.Settings["filetype"] = "unknown"
17+
if v, ok := settings["filetype"]; ok {
18+
b.Settings["filetype"] = v
19+
}
20+
}
21+
22+
// update syntax rules, which will also update filetype if needed
23+
b.UpdateRules()
24+
settings["filetype"] = b.Settings["filetype"]
25+
26+
config.InitLocalSettings(settings, b.Path)
27+
for k, v := range config.DefaultCommonSettings() {
28+
if k == "filetype" {
29+
// prevent recursion
30+
continue
31+
}
32+
if _, ok := config.VolatileSettings[k]; ok {
33+
// reload should not override volatile settings
34+
continue
35+
}
36+
if _, ok := b.LocalSettings[k]; ok {
37+
// reload should not override local settings
38+
continue
39+
}
40+
if _, ok := settings[k]; ok {
41+
b.DoSetOptionNative(k, settings[k])
42+
} else {
43+
b.DoSetOptionNative(k, v)
44+
}
45+
}
46+
}
47+
1148
func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
1249
if reflect.DeepEqual(b.Settings[option], nativeValue) {
1350
return
@@ -31,28 +68,7 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
3168
} else if option == "statusline" {
3269
screen.Redraw()
3370
} else if option == "filetype" {
34-
settings := config.ParsedSettings()
35-
settings["filetype"] = nativeValue
36-
config.InitLocalSettings(settings, b.Path)
37-
for k, v := range config.DefaultCommonSettings() {
38-
if k == "filetype" {
39-
continue
40-
}
41-
if _, ok := config.VolatileSettings[k]; ok {
42-
// filetype should not override volatile settings
43-
continue
44-
}
45-
if _, ok := b.LocalSettings[k]; ok {
46-
// filetype should not override local settings
47-
continue
48-
}
49-
if _, ok := settings[k]; ok {
50-
b.DoSetOptionNative(k, settings[k])
51-
} else {
52-
b.DoSetOptionNative(k, v)
53-
}
54-
}
55-
b.UpdateRules()
71+
b.ReloadSettings(false)
5672
} else if option == "fileformat" {
5773
switch b.Settings["fileformat"].(string) {
5874
case "unix":

0 commit comments

Comments
 (0)