forked from dsnet/gotab
/
default.go
55 lines (48 loc) · 1.05 KB
/
default.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.
package main
import (
"fmt"
"os"
"path"
"strings"
)
func (cmpl *completer) handleDefault(t *tokenizer) {
if tok, old := t.Next(); old {
cmpl.handleDefault(t)
} else {
// go <cmd> <pkg>
cmpl.suggestPackages(tok)
// go <cmd> <path>
cmpl.suggestPaths(tok)
}
}
func (cmpl *completer) suggestPaths(tok string) (cnt int) {
root, base := path.Split(tok)
if isDir(tok) {
root, base = tok, ""
}
fis, _ := os.ReadDir(root)
if root == "" {
fis, _ = os.ReadDir(".")
}
for _, fi := range fis {
if strings.Index(fi.Name(), base) != 0 {
// Skip paths don't match the partial token
continue
}
if strings.Index(fi.Name(), ".") == 0 && len(base) == 0 {
// Skip hidden folders unless there is a partial match
continue
}
if fi.IsDir() {
fmt.Println(root + fi.Name() + string(os.PathSeparator))
cnt++
} else {
fmt.Println(root + fi.Name() + " ")
cnt++
}
}
return cnt
}