]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Merge branch 'params' of https://github.com/boombuler/micro into boombuler-params
[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         var files []os.FileInfo
22         var err error
23         if len(dirs) > 1 {
24                 home, _ := homedir.Dir()
25
26                 directories := strings.Join(dirs[:len(dirs)-1], sep)
27                 if strings.HasPrefix(directories, "~") {
28                         directories = strings.Replace(directories, "~", home, 1)
29                 }
30                 files, err = ioutil.ReadDir(directories)
31         } else {
32                 files, err = ioutil.ReadDir(".")
33         }
34         var suggestions []string
35         if err != nil {
36                 return "", suggestions
37         }
38         for _, f := range files {
39                 name := f.Name()
40                 if f.IsDir() {
41                         name += sep
42                 }
43                 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
44                         suggestions = append(suggestions, name)
45                 }
46         }
47
48         var chosen string
49         if len(suggestions) == 1 {
50                 if len(dirs) > 1 {
51                         chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0]
52                 } else {
53                         chosen = suggestions[0]
54                 }
55         } else {
56                 if len(dirs) > 1 {
57                         chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep
58                 }
59         }
60
61         return chosen, suggestions
62 }
63
64 // CommandComplete autocompletes commands
65 func CommandComplete(input string) (string, []string) {
66         var suggestions []string
67         for cmd := range commands {
68                 if strings.HasPrefix(cmd, input) {
69                         suggestions = append(suggestions, cmd)
70                 }
71         }
72
73         var chosen string
74         if len(suggestions) == 1 {
75                 chosen = suggestions[0]
76         }
77         return chosen, suggestions
78 }
79
80 // HelpComplete autocompletes help topics
81 func HelpComplete(input string) (string, []string) {
82         var suggestions []string
83
84         for _, topic := range helpFiles {
85                 if strings.HasPrefix(topic, input) {
86
87                         suggestions = append(suggestions, topic)
88                 }
89         }
90
91         var chosen string
92         if len(suggestions) == 1 {
93                 chosen = suggestions[0]
94         }
95         return chosen, suggestions
96 }
97
98 func contains(s []string, e string) bool {
99         for _, a := range s {
100                 if a == e {
101                         return true
102                 }
103         }
104         return false
105 }
106
107 // OptionComplete autocompletes options
108 func OptionComplete(input string) (string, []string) {
109         var suggestions []string
110         localSettings := DefaultLocalSettings()
111         for option := range globalSettings {
112                 if strings.HasPrefix(option, input) {
113                         suggestions = append(suggestions, option)
114                 }
115         }
116         for option := range localSettings {
117                 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
118                         suggestions = append(suggestions, option)
119                 }
120         }
121
122         var chosen string
123         if len(suggestions) == 1 {
124                 chosen = suggestions[0]
125         }
126         return chosen, suggestions
127 }
128
129 // MakeCompletion registeres a function from a plugin for autocomplete commands
130 func MakeCompletion(function string) Completion {
131         pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
132         return Completion(-len(pluginCompletions))
133 }
134
135 // PluginComplete autocompletes from plugin function
136 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
137         idx := int(-complete) - 1
138
139         if len(pluginCompletions) <= idx {
140                 return "", nil
141         }
142         suggestions = pluginCompletions[idx](input)
143
144         if len(suggestions) == 1 {
145                 chosen = suggestions[0]
146         }
147         return
148 }