Add function to load runtime files from a directory for a plugin

This commit is contained in:
Zachary Yedidia 2016-09-16 16:15:44 -04:00
parent ab36db7646
commit 243f99aeb1
6 changed files with 30 additions and 19 deletions

View file

@ -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)

View file

@ -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 {

View file

@ -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)

View file

@ -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())

View file

@ -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)
}
}

View file

@ -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")