X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fautocomplete.go;h=47aeca7344821599c1eeec04dceec401aa3bcb44;hb=37ae99ccd9b7b3a658a2e773af74ca08c88c0d07;hp=52889d0f736a68ac7bd21da670ff8337a0fc76e1;hpb=1a9123630b5b97166d173c870abfdcbfee35d402;p=micro.git diff --git a/cmd/micro/autocomplete.go b/cmd/micro/autocomplete.go index 52889d0f..47aeca73 100644 --- a/cmd/micro/autocomplete.go +++ b/cmd/micro/autocomplete.go @@ -8,19 +8,24 @@ import ( "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`. // FileComplete autocompletes filenames func FileComplete(input string) (string, []string) { - dirs := strings.Split(input, "/") + 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], "/") + directories := strings.Join(dirs[:len(dirs)-1], sep) + sep + if strings.HasPrefix(directories, "~") { directories = strings.Replace(directories, "~", home, 1) } @@ -28,6 +33,7 @@ func FileComplete(input string) (string, []string) { } else { files, err = ioutil.ReadDir(".") } + var suggestions []string if err != nil { return "", suggestions @@ -35,7 +41,7 @@ func FileComplete(input string) (string, []string) { for _, f := range files { name := f.Name() if f.IsDir() { - name += "/" + name += sep } if strings.HasPrefix(name, dirs[len(dirs)-1]) { suggestions = append(suggestions, name) @@ -45,10 +51,14 @@ func FileComplete(input string) (string, []string) { var chosen string if len(suggestions) == 1 { if len(dirs) > 1 { - chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0] + chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0] } else { chosen = suggestions[0] } + } else { + if len(dirs) > 1 { + chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + } } return chosen, suggestions @@ -74,9 +84,9 @@ func CommandComplete(input string) (string, []string) { 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) } } @@ -88,6 +98,15 @@ func HelpComplete(input string) (string, []string) { return chosen, suggestions } +func contains(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} + // OptionComplete autocompletes options func OptionComplete(input string) (string, []string) { var suggestions []string @@ -98,7 +117,7 @@ func OptionComplete(input string) (string, []string) { } } for option := range localSettings { - if strings.HasPrefix(option, input) { + if strings.HasPrefix(option, input) && !contains(suggestions, option) { suggestions = append(suggestions, option) } } @@ -109,3 +128,50 @@ func OptionComplete(input string) (string, []string) { } 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 +}