]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Improve autoclose plugin auto tabbing
[micro.git] / cmd / micro / autocomplete.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "strings"
7         // "github.com/mitchellh/go-homedir"
8 )
9
10 // This file is meant (for now) for autocompletion in command mode, not
11 // while coding. This helps micro autocomplete commands and then filenames
12 // for example with `vsplit filename`.
13
14 // FileComplete autocompletes filenames
15 func FileComplete(input string) (string, []string) {
16         dirs := strings.Split(input, "/")
17         var files []os.FileInfo
18         var err error
19         if len(dirs) > 1 {
20                 // home, _ := homedir.Dir()
21
22                 // directories := strings.Join(dirs[:len(dirs)-1], "/")
23                 // if strings.HasPrefix(directories, "~") {
24                 //      directories = strings.Replace(directories, "~", home)
25                 // }
26                 files, err = ioutil.ReadDir(strings.Join(dirs[:len(dirs)-1], "/"))
27         } else {
28                 files, err = ioutil.ReadDir(".")
29         }
30         var suggestions []string
31         if err != nil {
32                 return "", suggestions
33         }
34         for _, f := range files {
35                 name := f.Name()
36                 if f.IsDir() {
37                         name += "/"
38                 }
39                 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
40                         suggestions = append(suggestions, name)
41                 }
42         }
43
44         var chosen string
45         if len(suggestions) == 1 {
46                 if len(dirs) > 1 {
47                         chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0]
48                 } else {
49                         chosen = suggestions[0]
50                 }
51         }
52
53         return chosen, suggestions
54 }
55
56 // CommandComplete autocompletes commands
57 func CommandComplete(input string) (string, []string) {
58         var suggestions []string
59         for cmd := range commands {
60                 if strings.HasPrefix(cmd, input) {
61                         suggestions = append(suggestions, cmd)
62                 }
63         }
64
65         var chosen string
66         if len(suggestions) == 1 {
67                 chosen = suggestions[0]
68         }
69         return chosen, suggestions
70 }
71
72 // HelpComplete autocompletes help topics
73 func HelpComplete(input string) (string, []string) {
74         var suggestions []string
75
76         for _, topic := range helpFiles {
77                 if strings.HasPrefix(topic, input) {
78
79                         suggestions = append(suggestions, topic)
80                 }
81         }
82
83         var chosen string
84         if len(suggestions) == 1 {
85                 chosen = suggestions[0]
86         }
87         return chosen, suggestions
88 }
89
90 // OptionComplete autocompletes options
91 func OptionComplete(input string) (string, []string) {
92         var suggestions []string
93         for option := range settings {
94                 if strings.HasPrefix(option, input) {
95                         suggestions = append(suggestions, option)
96                 }
97         }
98
99         var chosen string
100         if len(suggestions) == 1 {
101                 chosen = suggestions[0]
102         }
103         return chosen, suggestions
104 }