-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
append-snippet.go
54 lines (48 loc) · 1.02 KB
/
append-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
package snippets
import (
"bytes"
"go/ast"
"go/printer"
"go/token"
"margo.sh/golang/cursor"
"margo.sh/mg"
)
func AppendSnippet(cx *cursor.CurCtx) []mg.Completion {
if !cx.Scope.Is(cursor.ExprScope) {
return nil
}
cl := func(sel string) []mg.Completion {
if sel == "" {
sel = "s"
}
return []mg.Completion{
mg.Completion{
Query: `append`,
Title: `append(` + sel + `, ...)`,
Src: `append(${1:` + sel + `}, ${2})$0`,
},
mg.Completion{
Query: `append:len`,
Title: `append(` + sel + `[:len:len], ...)`,
Src: `append(${1:` + sel + `}[:len(${1:` + sel + `}):len(${1:` + sel + `})], ${2})$0`,
},
}
}
if !cx.Scope.Is(cursor.AssignmentScope) {
return cl("")
}
var asn *ast.AssignStmt
if !cx.Set(&asn) || len(asn.Lhs) != 1 || len(asn.Rhs) > 1 {
return cl("")
}
sel := ""
switch x := asn.Lhs[0].(type) {
case *ast.Ident:
sel = x.Name
case *ast.SelectorExpr:
buf := &bytes.Buffer{}
printer.Fprint(buf, token.NewFileSet(), x)
sel = buf.String()
}
return cl(sel)
}