]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
OutdentSelection works on all cases (whatever tabsize or tabstospace values)
[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 // ColorschemeComplete tab-completes names of colorschemes.
102 func ColorschemeComplete(input string) (string, []string) {
103         var suggestions []string
104         files := ListRuntimeFiles(RTColorscheme)
105
106         for _, f := range files {
107                 if strings.HasPrefix(f.Name(), input) {
108                         suggestions = append(suggestions, f.Name())
109                 }
110         }
111
112         var chosen string
113         if len(suggestions) == 1 {
114                 chosen = suggestions[0]
115         }
116
117         return chosen, suggestions
118 }
119
120 func contains(s []string, e string) bool {
121         for _, a := range s {
122                 if a == e {
123                         return true
124                 }
125         }
126         return false
127 }
128
129 // OptionComplete autocompletes options
130 func OptionComplete(input string) (string, []string) {
131         var suggestions []string
132         localSettings := DefaultLocalSettings()
133         for option := range globalSettings {
134                 if strings.HasPrefix(option, input) {
135                         suggestions = append(suggestions, option)
136                 }
137         }
138         for option := range localSettings {
139                 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
140                         suggestions = append(suggestions, option)
141                 }
142         }
143
144         var chosen string
145         if len(suggestions) == 1 {
146                 chosen = suggestions[0]
147         }
148         return chosen, suggestions
149 }
150
151 // MakeCompletion registers a function from a plugin for autocomplete commands
152 func MakeCompletion(function string) Completion {
153         pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
154         return Completion(-len(pluginCompletions))
155 }
156
157 // PluginComplete autocompletes from plugin function
158 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
159         idx := int(-complete) - 1
160
161         if len(pluginCompletions) <= idx {
162                 return "", nil
163         }
164         suggestions = pluginCompletions[idx](input)
165
166         if len(suggestions) == 1 {
167                 chosen = suggestions[0]
168         }
169         return
170 }
171
172 func PluginCmdComplete(input string) (chosen string, suggestions []string) {
173         for _, cmd := range []string{"install", "remove", "search", "update", "list"} {
174                 if strings.HasPrefix(cmd, input) {
175                         suggestions = append(suggestions, cmd)
176                 }
177         }
178
179         if len(suggestions) == 1 {
180                 chosen = suggestions[0]
181         }
182         return chosen, suggestions
183 }
184
185 func PluginNameComplete(input string) (chosen string, suggestions []string) {
186         for _, pp := range GetAllPluginPackages() {
187                 if strings.HasPrefix(pp.Name, input) {
188                         suggestions = append(suggestions, pp.Name)
189                 }
190         }
191
192         if len(suggestions) == 1 {
193                 chosen = suggestions[0]
194         }
195         return chosen, suggestions
196 }