]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Merge pull request #282 from boombuler/windows
[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 // This file is meant (for now) for autocompletion in command mode, not
12 // while coding. This helps micro autocomplete commands and then filenames
13 // for example with `vsplit filename`.
14
15 // FileComplete autocompletes filenames
16 func FileComplete(input string) (string, []string) {
17         var sep string = string(os.PathSeparator)
18         dirs := strings.Split(input, sep)
19         var files []os.FileInfo
20         var err error
21         if len(dirs) > 1 {
22                 home, _ := homedir.Dir()
23
24                 directories := strings.Join(dirs[:len(dirs)-1], sep)
25                 if strings.HasPrefix(directories, "~") {
26                         directories = strings.Replace(directories, "~", home, 1)
27                 }
28                 files, err = ioutil.ReadDir(directories)
29         } else {
30                 files, err = ioutil.ReadDir(".")
31         }
32         var suggestions []string
33         if err != nil {
34                 return "", suggestions
35         }
36         for _, f := range files {
37                 name := f.Name()
38                 if f.IsDir() {
39                         name += sep
40                 }
41                 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
42                         suggestions = append(suggestions, name)
43                 }
44         }
45
46         var chosen string
47         if len(suggestions) == 1 {
48                 if len(dirs) > 1 {
49                         chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0]
50                 } else {
51                         chosen = suggestions[0]
52                 }
53         } else {
54                 if len(dirs) > 1 {
55                         chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep
56                 }
57         }
58
59         return chosen, suggestions
60 }
61
62 // CommandComplete autocompletes commands
63 func CommandComplete(input string) (string, []string) {
64         var suggestions []string
65         for cmd := range commands {
66                 if strings.HasPrefix(cmd, input) {
67                         suggestions = append(suggestions, cmd)
68                 }
69         }
70
71         var chosen string
72         if len(suggestions) == 1 {
73                 chosen = suggestions[0]
74         }
75         return chosen, suggestions
76 }
77
78 // HelpComplete autocompletes help topics
79 func HelpComplete(input string) (string, []string) {
80         var suggestions []string
81
82         for _, topic := range helpFiles {
83                 if strings.HasPrefix(topic, input) {
84
85                         suggestions = append(suggestions, topic)
86                 }
87         }
88
89         var chosen string
90         if len(suggestions) == 1 {
91                 chosen = suggestions[0]
92         }
93         return chosen, suggestions
94 }
95
96 func contains(s []string, e string) bool {
97         for _, a := range s {
98                 if a == e {
99                         return true
100                 }
101         }
102         return false
103 }
104
105 // OptionComplete autocompletes options
106 func OptionComplete(input string) (string, []string) {
107         var suggestions []string
108         localSettings := DefaultLocalSettings()
109         for option := range globalSettings {
110                 if strings.HasPrefix(option, input) {
111                         suggestions = append(suggestions, option)
112                 }
113         }
114         for option := range localSettings {
115                 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
116                         suggestions = append(suggestions, option)
117                 }
118         }
119
120         var chosen string
121         if len(suggestions) == 1 {
122                 chosen = suggestions[0]
123         }
124         return chosen, suggestions
125 }