From 2e9423590509e8a5f29fbb2fec1f9ca33b940241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:43:59 +0100 Subject: [PATCH] buffer: Perform `filetype` callbacks on `ReloadSettings()` In `ReloadSettings()` the `filetype` can change upon globbed path given by the `settings.json` or by identifying a different `filetype` based on the file name given or pattern present inside the file. To prevent further recursion caused by checking the `filetype` again, its processing stops here and isn't considered in `DoSetOptionNative()` once again where the callbacks are usually triggered. --- internal/buffer/settings.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/internal/buffer/settings.go b/internal/buffer/settings.go index 661ba3fd..3db35e97 100644 --- a/internal/buffer/settings.go +++ b/internal/buffer/settings.go @@ -14,6 +14,8 @@ func (b *Buffer) ReloadSettings(reloadFiletype bool) { settings := config.ParsedSettings() config.UpdatePathGlobLocals(settings, b.AbsPath) + oldFiletype := b.Settings["filetype"].(string) + _, local := b.LocalSettings["filetype"] _, volatile := config.VolatileSettings["filetype"] if reloadFiletype && !local && !volatile { @@ -27,7 +29,13 @@ func (b *Buffer) ReloadSettings(reloadFiletype bool) { // update syntax rules, which will also update filetype if needed b.UpdateRules() - config.UpdateFileTypeLocals(settings, b.Settings["filetype"].(string)) + curFiletype := b.Settings["filetype"].(string) + if oldFiletype != curFiletype { + b.doCallbacks("filetype", oldFiletype, curFiletype) + } + + config.UpdateFileTypeLocals(settings, curFiletype) + for k, v := range config.DefaultCommonSettings() { if k == "filetype" { // prevent recursion @@ -119,15 +127,7 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) { } } - if b.OptionCallback != nil { - b.OptionCallback(option, nativeValue) - } - - if err := config.RunPluginFn("onBufferOptionChanged", - luar.New(ulua.L, b), luar.New(ulua.L, option), - luar.New(ulua.L, oldValue), luar.New(ulua.L, nativeValue)); err != nil { - screen.TermMessage(err) - } + b.doCallbacks(option, oldValue, nativeValue) } func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { @@ -154,3 +154,15 @@ func (b *Buffer) SetOption(option, value string) error { return b.SetOptionNative(option, nativeValue) } + +func (b *Buffer) doCallbacks(option string, oldValue interface{}, newValue interface{}) { + if b.OptionCallback != nil { + b.OptionCallback(option, newValue) + } + + if err := config.RunPluginFn("onBufferOptionChanged", + luar.New(ulua.L, b), luar.New(ulua.L, option), + luar.New(ulua.L, oldValue), luar.New(ulua.L, newValue)); err != nil { + screen.TermMessage(err) + } +}