Skip to content

Commit

Permalink
command: Fix reload command to correctly initialize and reload all …
Browse files Browse the repository at this point in the history
…runtime files (#3062)

* rtfiles: Initialize all-/realFiles and Plugins in InitRuntimeFiles

* command: Reload plugins at ReloadCmd too

* command: Don't reload plugins in case of ReloadConfig()

* rtfiles: Split InitRuntimeFiles() into one func for assets and one for plugins

* rtfiles: Remove the unnecessary init function

With this modification the InitRuntimeFiles() and InitPlugins() (if needed)
must be called first, otherwise uninitialized runtime file variables are most
likely.
  • Loading branch information
JoeKar committed Mar 22, 2024
1 parent bb1f4da commit a57d29a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmd/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ func main() {
}

config.InitRuntimeFiles()
config.InitPlugins()

err = config.ReadSettings()
if err != nil {
screen.TermMessage(err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/micro/micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func startup(args []string) (tcell.SimulationScreen, error) {
}

config.InitRuntimeFiles()
config.InitPlugins()

err = config.ReadSettings()
if err != nil {
return nil, err
Expand Down
45 changes: 42 additions & 3 deletions internal/action/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,30 @@ func (h *BufPane) ToggleLogCmd(args []string) {
}
}

// ReloadCmd reloads all files (syntax files, colorschemes...)
// ReloadCmd reloads all files (syntax files, colorschemes, plugins...)
func (h *BufPane) ReloadCmd(args []string) {
ReloadConfig()
reloadRuntime(true)
}

// ReloadConfig reloads only the configuration
func ReloadConfig() {
reloadRuntime(false)
}

func reloadRuntime(reloadPlugins bool) {
if reloadPlugins {
err := config.RunPluginFn("deinit")
if err != nil {
screen.TermMessage(err)
}
}

config.InitRuntimeFiles()

if reloadPlugins {
config.InitPlugins()
}

err := config.ReadSettings()
if err != nil {
screen.TermMessage(err)
Expand All @@ -344,14 +361,36 @@ func ReloadConfig() {
if err != nil {
screen.TermMessage(err)
}

if reloadPlugins {
err = config.LoadAllPlugins()
if err != nil {
screen.TermMessage(err)
}
}

InitBindings()
InitCommands()

if reloadPlugins {
err = config.RunPluginFn("preinit")
if err != nil {
screen.TermMessage(err)
}
err = config.RunPluginFn("init")
if err != nil {
screen.TermMessage(err)
}
err = config.RunPluginFn("postinit")
if err != nil {
screen.TermMessage(err)
}
}

err = config.InitColorscheme()
if err != nil {
screen.TermMessage(err)
}

for _, b := range buffer.OpenBuffers {
b.UpdateRules()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type operation struct {

func init() {
ulua.L = lua.NewState()
config.InitRuntimeFiles()
config.InitPlugins()
config.InitGlobalSettings()
config.GlobalSettings["backup"] = false
config.GlobalSettings["fastdirty"] = true
Expand Down
2 changes: 1 addition & 1 deletion internal/buffer/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
}
}
}
}
}

if b.OptionCallback != nil {
b.OptionCallback(option, nativeValue)
Expand Down
9 changes: 8 additions & 1 deletion internal/config/rtfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type RuntimeFile interface {
var allFiles [][]RuntimeFile
var realFiles [][]RuntimeFile

func init() {
func initRuntimeVars() {
allFiles = make([][]RuntimeFile, NumTypes)
realFiles = make([][]RuntimeFile, NumTypes)
}
Expand Down Expand Up @@ -173,12 +173,19 @@ func InitRuntimeFiles() {
AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
}

initRuntimeVars()

add(RTColorscheme, "colorschemes", "*.micro")
add(RTSyntax, "syntax", "*.yaml")
add(RTSyntaxHeader, "syntax", "*.hdr")
add(RTHelp, "help", "*.md")
}

// InitPlugins initializes the plugins
func InitPlugins() {
Plugins = Plugins[:0]
initlua := filepath.Join(ConfigDir, "init.lua")

if _, err := os.Stat(initlua); !os.IsNotExist(err) {
p := new(Plugin)
p.Name = "initlua"
Expand Down
1 change: 1 addition & 0 deletions internal/config/rtfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

func init() {
InitRuntimeFiles()
InitPlugins()
}

func TestAddFile(t *testing.T) {
Expand Down

0 comments on commit a57d29a

Please sign in to comment.