-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_path.go
70 lines (57 loc) · 1.19 KB
/
package_path.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package predictor
import (
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/posener/complete"
"github.com/sirkon/jsonexec"
"github.com/sirkon/ttgenlib/internal/ordmap"
)
type PackagePath struct{}
// Predict to satisfy complete.Predictor.
func (p PackagePath) Predict(args complete.Args) []string {
var dst struct {
Path string
Dir string
}
if err := jsonexec.Run(&dst, "go", "list", "-m", "--json"); err != nil {
return nil
}
dir, basePrefix := filepath.Split(args.Last)
if dir == "" {
dir = "./"
}
reldirs := ordmap.New[string, bool]()
err := filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
if info == nil {
return nil
}
if info.IsDir() {
switch path {
case ".git", ".idea":
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
d, _ := filepath.Split(path)
d = strings.TrimRight(d, string(os.PathSeparator))
if reldirs.Has(d) {
return nil
}
_, db := filepath.Split(d)
if !strings.HasPrefix(db, basePrefix) {
return nil
}
reldirs.Set(d, true)
return nil
})
if err != nil {
return nil
}
return reldirs.Keys()
}
var _ complete.Predictor = PackagePath{}