]> git.lizzy.rs Git - micro.git/blobdiff - internal/config/rtfiles.go
Perl syntax improvement (#2359)
[micro.git] / internal / config / rtfiles.go
index 6070e4eb81fbcabe24c2f28fddf8b7be693f5040..397a85398dc049624b653c2d622e347c0a483625 100644 (file)
@@ -9,6 +9,8 @@ import (
        "path/filepath"
        "regexp"
        "strings"
+
+       rt "github.com/zyedidia/micro/v2/runtime"
 )
 
 const (
@@ -17,10 +19,13 @@ const (
        RTHelp         = 2
        RTPlugin       = 3
        RTSyntaxHeader = 4
-       NumTypes       = 5 // How many filetypes are there
 )
 
-type RTFiletype byte
+var (
+       NumTypes = 5 // How many filetypes are there
+)
+
+type RTFiletype int
 
 // RuntimeFile allows the program to read runtime data like colorschemes or syntax files
 type RuntimeFile interface {
@@ -31,8 +36,21 @@ type RuntimeFile interface {
 }
 
 // allFiles contains all available files, mapped by filetype
-var allFiles [NumTypes][]RuntimeFile
-var realFiles [NumTypes][]RuntimeFile
+var allFiles [][]RuntimeFile
+var realFiles [][]RuntimeFile
+
+func init() {
+       allFiles = make([][]RuntimeFile, NumTypes)
+       realFiles = make([][]RuntimeFile, NumTypes)
+}
+
+// NewRTFiletype creates a new RTFiletype
+func NewRTFiletype() int {
+       NumTypes++
+       allFiles = append(allFiles, []RuntimeFile{})
+       realFiles = append(realFiles, []RuntimeFile{})
+       return NumTypes - 1
+}
 
 // some file on filesystem
 type realFile string
@@ -74,7 +92,7 @@ func (af assetFile) Name() string {
 }
 
 func (af assetFile) Data() ([]byte, error) {
-       return Asset(string(af))
+       return rt.Asset(string(af))
 }
 
 func (nf namedFile) Name() string {
@@ -107,7 +125,7 @@ func AddRuntimeFilesFromDirectory(fileType RTFiletype, directory, pattern string
 // AddRuntimeFilesFromAssets registers each file from the given asset-directory for
 // the filetype which matches the file-pattern
 func AddRuntimeFilesFromAssets(fileType RTFiletype, directory, pattern string) {
-       files, err := AssetDir(directory)
+       files, err := rt.AssetDir(directory)
        if err != nil {
                return
        }
@@ -176,12 +194,15 @@ func InitRuntimeFiles() {
                        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 f.Name() == "info.json" {
-                                       data, err := ioutil.ReadFile(filepath.Join(plugdir, d.Name(), "info.json"))
+                               } 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.Info, _ = NewPluginInfo(data)
                                        p.Name = p.Info.Name
                                }
                        }
@@ -195,9 +216,9 @@ func InitRuntimeFiles() {
        }
 
        plugdir = filepath.Join("runtime", "plugins")
-       if files, err := AssetDir(plugdir); err == nil {
+       if files, err := rt.AssetDir(plugdir); err == nil {
                for _, d := range files {
-                       if srcs, err := AssetDir(filepath.Join(plugdir, d)); err == nil {
+                       if srcs, err := rt.AssetDir(filepath.Join(plugdir, d)); err == nil {
                                p := new(Plugin)
                                p.Name = d
                                p.DirName = d
@@ -205,12 +226,15 @@ func InitRuntimeFiles() {
                                for _, f := range srcs {
                                        if strings.HasSuffix(f, ".lua") {
                                                p.Srcs = append(p.Srcs, assetFile(filepath.Join(plugdir, d, f)))
-                                       } else if f == "info.json" {
-                                               data, err := Asset(filepath.Join(plugdir, d, "info.json"))
+                                       } 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.Info, _ = NewPluginInfo(data)
                                                p.Name = p.Info.Name
                                        }
                                }