-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
package-name-snippet.go
59 lines (49 loc) · 916 Bytes
/
package-name-snippet.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
package snippets
import (
"margo.sh/golang/cursor"
"margo.sh/golang/goutil"
"margo.sh/mg"
"regexp"
)
var (
pkgDirNamePat = regexp.MustCompile(`(\w+)\W*$`)
)
func PackageNameSnippet(cx *cursor.CurCtx) []mg.Completion {
if cx.PkgName != goutil.NilPkgName || cx.Scope != cursor.PackageScope {
return nil
}
var cl []mg.Completion
seen := map[string]bool{}
add := func(name string) {
if seen[name] {
return
}
seen[name] = true
cl = append(cl, mg.Completion{
Query: `package ` + name,
Src: `
package ` + name + `
$0
`,
})
}
dir := cx.View.Dir()
pkg, _ := goutil.BuildContext(cx.Ctx).ImportDir(dir, 0)
if pkg != nil && pkg.Name != "" {
add(pkg.Name)
} else {
add(pkgDirNamePat.FindString(dir))
}
cl = append(cl, mg.Completion{
Query: `package main`,
Title: `main{}`,
Src: `
package main
func main() {
$0
}
`,
})
add("main")
return cl
}