"github.com/mitchellh/go-homedir"
)
+var pluginCompletions []func(string) []string
+
// This file is meant (for now) for autocompletion in command mode, not
// while coding. This helps micro autocomplete commands and then filenames
// for example with `vsplit filename`.
func FileComplete(input string) (string, []string) {
var sep string = string(os.PathSeparator)
dirs := strings.Split(input, sep)
+
var files []os.FileInfo
var err error
if len(dirs) > 1 {
home, _ := homedir.Dir()
- directories := strings.Join(dirs[:len(dirs)-1], sep)
+ directories := strings.Join(dirs[:len(dirs)-1], sep) + sep
+
if strings.HasPrefix(directories, "~") {
directories = strings.Replace(directories, "~", home, 1)
}
} else {
files, err = ioutil.ReadDir(".")
}
+
var suggestions []string
if err != nil {
return "", suggestions
func HelpComplete(input string) (string, []string) {
var suggestions []string
- for _, topic := range helpFiles {
+ for _, file := range ListRuntimeFiles(RTHelp) {
+ topic := file.Name()
if strings.HasPrefix(topic, input) {
-
suggestions = append(suggestions, topic)
}
}
}
return chosen, suggestions
}
+
+// MakeCompletion registeres a function from a plugin for autocomplete commands
+func MakeCompletion(function string) Completion {
+ pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
+ return Completion(-len(pluginCompletions))
+}
+
+// PluginComplete autocompletes from plugin function
+func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
+ idx := int(-complete) - 1
+
+ if len(pluginCompletions) <= idx {
+ return "", nil
+ }
+ suggestions = pluginCompletions[idx](input)
+
+ if len(suggestions) == 1 {
+ chosen = suggestions[0]
+ }
+ return
+}
+
+func PluginCmdComplete(input string) (chosen string, suggestions []string) {
+ for _, cmd := range []string{"install", "remove", "search", "update", "list"} {
+ if strings.HasPrefix(cmd, input) {
+ suggestions = append(suggestions, cmd)
+ }
+ }
+
+ if len(suggestions) == 1 {
+ chosen = suggestions[0]
+ }
+ return chosen, suggestions
+}
+
+func PluginNameComplete(input string) (chosen string, suggestions []string) {
+ for _, pp := range GetAllPluginPackages() {
+ if strings.HasPrefix(pp.Name, input) {
+ suggestions = append(suggestions, pp.Name)
+ }
+ }
+
+ if len(suggestions) == 1 {
+ chosen = suggestions[0]
+ }
+ return chosen, suggestions
+}