]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Update docs and readme
[micro.git] / cmd / micro / autocomplete.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "strings"
7
8         "github.com/mitchellh/go-homedir"
9 )
10
11 var pluginCompletions []func(string) []string
12
13 // This file is meant (for now) for autocompletion in command mode, not
14 // while coding. This helps micro autocomplete commands and then filenames
15 // for example with `vsplit filename`.
16
17 // FileComplete autocompletes filenames
18 func FileComplete(input string) (string, []string) {
19         var sep string = string(os.PathSeparator)
20         dirs := strings.Split(input, sep)
21
22         var files []os.FileInfo
23         var err error
24         if len(dirs) > 1 {
25                 home, _ := homedir.Dir()
26
27                 directories := strings.Join(dirs[:len(dirs)-1], sep) + sep
28
29                 if strings.HasPrefix(directories, "~") {
30                         directories = strings.Replace(directories, "~", home, 1)
31                 }
32                 files, err = ioutil.ReadDir(directories)
33         } else {
34                 files, err = ioutil.ReadDir(".")
35         }
36
37         var suggestions []string
38         if err != nil {
39                 return "", suggestions
40         }
41         for _, f := range files {
42                 name := f.Name()
43                 if f.IsDir() {
44                         name += sep
45                 }
46                 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
47                         suggestions = append(suggestions, name)
48                 }
49         }
50
51         var chosen string
52         if len(suggestions) == 1 {
53                 if len(dirs) > 1 {
54                         chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0]
55                 } else {
56                         chosen = suggestions[0]
57                 }
58         } else {
59                 if len(dirs) > 1 {
60                         chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep
61                 }
62         }
63
64         return chosen, suggestions
65 }
66
67 // CommandComplete autocompletes commands
68 func CommandComplete(input string) (string, []string) {
69         var suggestions []string
70         for cmd := range commands {
71                 if strings.HasPrefix(cmd, input) {
72                         suggestions = append(suggestions, cmd)
73                 }
74         }
75
76         var chosen string
77         if len(suggestions) == 1 {
78                 chosen = suggestions[0]
79         }
80         return chosen, suggestions
81 }
82
83 // HelpComplete autocompletes help topics
84 func HelpComplete(input string) (string, []string) {
85         var suggestions []string
86
87         for _, file := range ListRuntimeFiles(RTHelp) {
88                 topic := file.Name()
89                 if strings.HasPrefix(topic, input) {
90                         suggestions = append(suggestions, topic)
91                 }
92         }
93
94         var chosen string
95         if len(suggestions) == 1 {
96                 chosen = suggestions[0]
97         }
98         return chosen, suggestions
99 }
100
101 func contains(s []string, e string) bool {
102         for _, a := range s {
103                 if a == e {
104                         return true
105                 }
106         }
107         return false
108 }
109
110 // OptionComplete autocompletes options
111 func OptionComplete(input string) (string, []string) {
112         var suggestions []string
113         localSettings := DefaultLocalSettings()
114         for option := range globalSettings {
115                 if strings.HasPrefix(option, input) {
116                         suggestions = append(suggestions, option)
117                 }
118         }
119         for option := range localSettings {
120                 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
121                         suggestions = append(suggestions, option)
122                 }
123         }
124
125         var chosen string
126         if len(suggestions) == 1 {
127                 chosen = suggestions[0]
128         }
129         return chosen, suggestions
130 }
131
132 // MakeCompletion registeres a function from a plugin for autocomplete commands
133 func MakeCompletion(function string) Completion {
134         pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
135         return Completion(-len(pluginCompletions))
136 }
137
138 // PluginComplete autocompletes from plugin function
139 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
140         idx := int(-complete) - 1
141
142         if len(pluginCompletions) <= idx {
143                 return "", nil
144         }
145         suggestions = pluginCompletions[idx](input)
146
147         if len(suggestions) == 1 {
148                 chosen = suggestions[0]
149         }
150         return
151 }
152
153 func PluginCmdComplete(input string) (chosen string, suggestions []string) {
154         for _, cmd := range []string{"install", "remove", "search", "update", "list"} {
155                 if strings.HasPrefix(cmd, input) {
156                         suggestions = append(suggestions, cmd)
157                 }
158         }
159
160         if len(suggestions) == 1 {
161                 chosen = suggestions[0]
162         }
163         return chosen, suggestions
164 }
165
166 func PluginNameComplete(input string) (chosen string, suggestions []string) {
167         for _, pp := range GetAllPluginPackages() {
168                 if strings.HasPrefix(pp.Name, input) {
169                         suggestions = append(suggestions, pp.Name)
170                 }
171         }
172
173         if len(suggestions) == 1 {
174                 chosen = suggestions[0]
175         }
176         return chosen, suggestions
177 }