From 243f99aeb136ab15f5ed485510dbe7aa6a062c11 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Fri, 16 Sep 2016 16:15:44 -0400 Subject: [PATCH] Add function to load runtime files from a directory for a plugin --- cmd/micro/autocomplete.go | 2 +- cmd/micro/colorscheme.go | 4 ++-- cmd/micro/command.go | 2 +- cmd/micro/highlighter.go | 2 +- cmd/micro/rtfiles.go | 37 ++++++++++++++++++++++++------------- cmd/micro/view.go | 2 +- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cmd/micro/autocomplete.go b/cmd/micro/autocomplete.go index 4946b797..d0ee3e17 100644 --- a/cmd/micro/autocomplete.go +++ b/cmd/micro/autocomplete.go @@ -84,7 +84,7 @@ func CommandComplete(input string) (string, []string) { func HelpComplete(input string) (string, []string) { var suggestions []string - for _, file := range ListRuntimeFiles(FILE_Help) { + for _, file := range ListRuntimeFiles(RTHelp) { topic := file.Name() if strings.HasPrefix(topic, input) { suggestions = append(suggestions, topic) diff --git a/cmd/micro/colorscheme.go b/cmd/micro/colorscheme.go index 2332e434..c2a03cdc 100644 --- a/cmd/micro/colorscheme.go +++ b/cmd/micro/colorscheme.go @@ -17,7 +17,7 @@ var colorscheme Colorscheme // ColorschemeExists checks if a given colorscheme exists func ColorschemeExists(colorschemeName string) bool { - return FindRuntimeFile(FILE_ColorScheme, colorschemeName) != nil + return FindRuntimeFile(RTColorscheme, colorschemeName) != nil } // InitColorscheme picks and initializes the colorscheme when micro starts @@ -39,7 +39,7 @@ func LoadDefaultColorscheme() { // LoadColorscheme loads the given colorscheme from a directory func LoadColorscheme(colorschemeName string) { - file := FindRuntimeFile(FILE_ColorScheme, colorschemeName) + file := FindRuntimeFile(RTColorscheme, colorschemeName) if file == nil { TermMessage(colorschemeName, "is not a valid colorscheme") } else { diff --git a/cmd/micro/command.go b/cmd/micro/command.go index b687a6d7..f0cfe9b3 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -94,7 +94,7 @@ func Help(args []string) { CurView().openHelp("help") } else { helpPage := args[0] - if FindRuntimeFile(FILE_Help, helpPage) != nil { + if FindRuntimeFile(RTHelp, helpPage) != nil { CurView().openHelp(helpPage) } else { messenger.Error("Sorry, no help for ", helpPage) diff --git a/cmd/micro/highlighter.go b/cmd/micro/highlighter.go index c66385fb..413372d6 100644 --- a/cmd/micro/highlighter.go +++ b/cmd/micro/highlighter.go @@ -32,7 +32,7 @@ var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules func LoadSyntaxFiles() { InitColorscheme() syntaxFiles = make(map[[2]*regexp.Regexp]FileTypeRules) - for _, f := range ListRuntimeFiles(FILE_Syntax) { + for _, f := range ListRuntimeFiles(RTSyntax) { data, err := f.Data() if err != nil { TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error()) diff --git a/cmd/micro/rtfiles.go b/cmd/micro/rtfiles.go index 5b0f51da..3d0ea574 100644 --- a/cmd/micro/rtfiles.go +++ b/cmd/micro/rtfiles.go @@ -8,9 +8,9 @@ import ( ) const ( - FILE_ColorScheme = "colorscheme" - FILE_Syntax = "syntax" - FILE_Help = "help" + RTColorscheme = "colorscheme" + RTSyntax = "syntax" + RTHelp = "help" ) // RuntimeFile allows the program to read runtime data like colorschemes or syntax files @@ -78,7 +78,7 @@ func AddRuntimeFilesFromDirectory(fileType, directory, pattern string) { } } -// AddRuntimeFilesFromDirectory registers each file from the given asset-directory for +// AddRuntimeFilesFromAssets registers each file from the given asset-directory for // the filetype which matches the file-pattern func AddRuntimeFilesFromAssets(fileType, directory, pattern string) { files, err := AssetDir(directory) @@ -103,28 +103,27 @@ func FindRuntimeFile(fileType, name string) RuntimeFile { return nil } -// Lists all known runtime files for the given filetype +// ListRuntimeFiles lists all known runtime files for the given filetype func ListRuntimeFiles(fileType string) []RuntimeFile { if files, ok := allFiles[fileType]; ok { return files - } else { - return []RuntimeFile{} } + return []RuntimeFile{} } -// Initializes all assets file and the config directory +// InitRuntimeFiles initializes all assets file and the config directory func InitRuntimeFiles() { add := func(fileType, dir, pattern string) { AddRuntimeFilesFromDirectory(fileType, filepath.Join(configDir, dir), pattern) AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern) } - add(FILE_ColorScheme, "colorschemes", "*.micro") - add(FILE_Syntax, "syntax", "*.micro") - add(FILE_Help, "help", "*.md") + add(RTColorscheme, "colorschemes", "*.micro") + add(RTSyntax, "syntax", "*.micro") + add(RTHelp, "help", "*.md") } -// Allows plugin scripts to read the content of a runtime file +// PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file func PluginReadRuntimeFile(fileType, name string) string { if file := FindRuntimeFile(fileType, name); file != nil { if data, err := file.Data(); err == nil { @@ -134,7 +133,7 @@ func PluginReadRuntimeFile(fileType, name string) string { return "" } -// Allows plugins to lists all runtime files of the given type +// PluginListRuntimeFiles allows plugins to lists all runtime files of the given type func PluginListRuntimeFiles(fileType string) []string { files := ListRuntimeFiles(fileType) result := make([]string, len(files)) @@ -144,6 +143,7 @@ func PluginListRuntimeFiles(fileType string) []string { return result } +// PluginAddRuntimeFile adds a file to the runtime files for a plugin func PluginAddRuntimeFile(plugin, filetype, path string) { fullpath := configDir + "/plugins/" + plugin + "/" + path if _, err := os.Stat(fullpath); err == nil { @@ -153,3 +153,14 @@ func PluginAddRuntimeFile(plugin, filetype, path string) { AddRuntimeFile(filetype, assetFile(fullpath)) } } + +// PluginAddRuntimeFilesFromDirectory adds files from a directory to the runtime files for a plugin +func PluginAddRuntimeFilesFromDirectory(plugin, filetype, directory, pattern string) { + fullpath := filepath.Join(configDir, "plugins", plugin, directory) + if _, err := os.Stat(fullpath); err == nil { + AddRuntimeFilesFromDirectory(filetype, fullpath, pattern) + } else { + fullpath = path.Join("runtime", "plugins", plugin, directory) + AddRuntimeFilesFromAssets(filetype, fullpath, pattern) + } +} diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 3aaa7ef8..efefd8d5 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -529,7 +529,7 @@ func (v *View) ClearAllGutterMessages() { // Opens the given help page in a new horizontal split func (v *View) openHelp(helpPage string) { - if data, err := FindRuntimeFile(FILE_Help, helpPage).Data(); err != nil { + if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil { TermMessage("Unable to load help text", helpPage, "\n", err) } else { helpBuffer := NewBuffer(data, helpPage+".md")