Skip to content

Commit

Permalink
command: Don't reload plugins in case of ReloadConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKar committed Mar 18, 2024
1 parent e7b1859 commit 53ad8ee
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 67 deletions.
2 changes: 1 addition & 1 deletion cmd/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func main() {
screen.TermMessage(err)
}

config.InitRuntimeFiles()
config.InitRuntimeFiles(true)
err = config.ReadSettings()
if err != nil {
screen.TermMessage(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/micro/micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func startup(args []string) (tcell.SimulationScreen, error) {
return nil, err
}

config.InitRuntimeFiles()
config.InitRuntimeFiles(true)
err = config.ReadSettings()
if err != nil {
return nil, err
Expand Down
14 changes: 9 additions & 5 deletions internal/action/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,15 @@ func ReloadConfig() {
}

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

config.InitRuntimeFiles(reloadAll)

err := config.ReadSettings()
if err != nil {
screen.TermMessage(err)
Expand All @@ -351,10 +359,6 @@ func reloadRuntime(reloadAll bool) {
}

if reloadAll {
err = config.RunPluginFn("deinit")
if err != nil {
screen.TermMessage(err)
}
err = config.LoadAllPlugins()
if err != nil {
screen.TermMessage(err)
Expand Down
4 changes: 2 additions & 2 deletions internal/buffer/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
} else if option == "statusline" {
screen.Redraw()
} else if option == "filetype" {
config.InitRuntimeFiles()
config.InitRuntimeFiles(false)
err := config.ReadSettings()
if err != nil {
screen.TermMessage(err)
Expand Down 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
118 changes: 61 additions & 57 deletions internal/config/rtfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ var allFiles [][]RuntimeFile
var realFiles [][]RuntimeFile

func init() {
initRuntimeVars()
initRuntimeVars(true)
}

func initRuntimeVars() {
func initRuntimeVars(reloadAll bool) {
allFiles = make([][]RuntimeFile, NumTypes)
realFiles = make([][]RuntimeFile, NumTypes)
Plugins = Plugins[:0]
if reloadAll {
Plugins = Plugins[:0]
}
}

// NewRTFiletype creates a new RTFiletype
Expand Down Expand Up @@ -164,78 +166,47 @@ func ListRealRuntimeFiles(fileType RTFiletype) []RuntimeFile {
}

// InitRuntimeFiles initializes all assets file and the config directory
func InitRuntimeFiles() {
func InitRuntimeFiles(reloadAll bool) {
add := func(fileType RTFiletype, dir, pattern string) {
AddRuntimeFilesFromDirectory(fileType, filepath.Join(ConfigDir, dir), pattern)
AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
}

initRuntimeVars()
initRuntimeVars(reloadAll)

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

initlua := filepath.Join(ConfigDir, "init.lua")
if _, err := os.Stat(initlua); !os.IsNotExist(err) {
p := new(Plugin)
p.Name = "initlua"
p.DirName = "initlua"
p.Srcs = append(p.Srcs, realFile(initlua))
Plugins = append(Plugins, p)
}

// Search ConfigDir for plugin-scripts
plugdir := filepath.Join(ConfigDir, "plug")
files, _ := ioutil.ReadDir(plugdir)

isID := regexp.MustCompile(`^[_A-Za-z0-9]+$`).MatchString

for _, d := range files {
plugpath := filepath.Join(plugdir, d.Name())
if stat, err := os.Stat(plugpath); err == nil && stat.IsDir() {
srcs, _ := ioutil.ReadDir(plugpath)
if reloadAll {
initlua := filepath.Join(ConfigDir, "init.lua")
if _, err := os.Stat(initlua); !os.IsNotExist(err) {
p := new(Plugin)
p.Name = d.Name()
p.DirName = d.Name()
for _, f := range srcs {
if strings.HasSuffix(f.Name(), ".lua") {
p.Srcs = append(p.Srcs, realFile(filepath.Join(plugdir, d.Name(), f.Name())))
} else if strings.HasSuffix(f.Name(), ".json") {
data, err := ioutil.ReadFile(filepath.Join(plugdir, d.Name(), f.Name()))
if err != nil {
continue
}
p.Info, err = NewPluginInfo(data)
if err != nil {
continue
}
p.Name = p.Info.Name
}
}

if !isID(p.Name) || len(p.Srcs) <= 0 {
log.Println(p.Name, "is not a plugin")
continue
}
p.Name = "initlua"
p.DirName = "initlua"
p.Srcs = append(p.Srcs, realFile(initlua))
Plugins = append(Plugins, p)
}
}

plugdir = filepath.Join("runtime", "plugins")
if files, err := rt.AssetDir(plugdir); err == nil {
// Search ConfigDir for plugin-scripts
plugdir := filepath.Join(ConfigDir, "plug")
files, _ := ioutil.ReadDir(plugdir)

isID := regexp.MustCompile(`^[_A-Za-z0-9]+$`).MatchString

for _, d := range files {
if srcs, err := rt.AssetDir(filepath.Join(plugdir, d)); err == nil {
plugpath := filepath.Join(plugdir, d.Name())
if stat, err := os.Stat(plugpath); err == nil && stat.IsDir() {
srcs, _ := ioutil.ReadDir(plugpath)
p := new(Plugin)
p.Name = d
p.DirName = d
p.Default = true
p.Name = d.Name()
p.DirName = d.Name()
for _, f := range srcs {
if strings.HasSuffix(f, ".lua") {
p.Srcs = append(p.Srcs, assetFile(filepath.Join(plugdir, d, f)))
} else if strings.HasSuffix(f, ".json") {
data, err := rt.Asset(filepath.Join(plugdir, d, f))
if strings.HasSuffix(f.Name(), ".lua") {
p.Srcs = append(p.Srcs, realFile(filepath.Join(plugdir, d.Name(), f.Name())))
} else if strings.HasSuffix(f.Name(), ".json") {
data, err := ioutil.ReadFile(filepath.Join(plugdir, d.Name(), f.Name()))
if err != nil {
continue
}
Expand All @@ -246,13 +217,46 @@ func InitRuntimeFiles() {
p.Name = p.Info.Name
}
}

if !isID(p.Name) || len(p.Srcs) <= 0 {
log.Println(p.Name, "is not a plugin")
continue
}
Plugins = append(Plugins, p)
}
}

plugdir = filepath.Join("runtime", "plugins")
if files, err := rt.AssetDir(plugdir); err == nil {
for _, d := range files {
if srcs, err := rt.AssetDir(filepath.Join(plugdir, d)); err == nil {
p := new(Plugin)
p.Name = d
p.DirName = d
p.Default = true
for _, f := range srcs {
if strings.HasSuffix(f, ".lua") {
p.Srcs = append(p.Srcs, assetFile(filepath.Join(plugdir, d, f)))
} else if strings.HasSuffix(f, ".json") {
data, err := rt.Asset(filepath.Join(plugdir, d, f))
if err != nil {
continue
}
p.Info, err = NewPluginInfo(data)
if err != nil {
continue
}
p.Name = p.Info.Name
}
}
if !isID(p.Name) || len(p.Srcs) <= 0 {
log.Println(p.Name, "is not a plugin")
continue
}
Plugins = append(Plugins, p)
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/rtfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
InitRuntimeFiles()
InitRuntimeFiles(true)
}

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

0 comments on commit 53ad8ee

Please sign in to comment.