forked from josharian/impl
/
implementer.go
291 lines (237 loc) · 5.92 KB
/
implementer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package impl
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"strings"
)
// TODO implement impl.Func == ast.FuncDecl checking
// ErrMethodExists will be returned when a method should be created but an
// existing method already exists for the given receiver.
type ErrMethodExists struct {
Wanted Func
Exists ast.FuncDecl
}
func (e *ErrMethodExists) Error() string {
args := []string{}
for _, p := range e.Wanted.Params {
args = append(args, p.Name+" "+p.Type)
}
ret := []string{}
for _, r := range e.Wanted.Res {
ret = append(ret, r.Name+" "+r.Type)
}
sig := fmt.Sprintf("%s(%s) (%s)", e.Wanted.Name, strings.Join(args, ", "), strings.Join(ret, ", "))
return fmt.Sprintf("wanted to create Method %q, but this method name already exists for the receiver", sig)
}
// Get some ordinal ast.Ident.Name from a given ast.Node. A negative will return
// the last identifier in the tree.
func getIdent(node ast.Node, ord int) string {
ident := ""
n := 0
ast.Inspect(node, func(child ast.Node) bool {
if child, ok := child.(*ast.Ident); ok {
ident = child.Name
n++
}
return ord < 0 || n == ord
})
return ident
}
// As a shortcut we parse the receiver expression, then just take the last
// identifier specified in the resulting ast
func getType(recv string) (string, error) {
a, err := parser.ParseExpr(recv)
if err != nil {
return "", err
}
return getIdent(a, -1), nil
}
// An Implementer can, for a certain directory, create and/or update
// implementation with Go source code for a particular interface
type Implementer struct {
Recv, IFace, Dir string
funcs []Func
recvName string
typeDecl *ast.GenDecl
methods map[string]*ast.FuncDecl
found bool
file map[string]*ast.Package
fset *token.FileSet
buf *bytes.Buffer
}
// Visit implements ast.Visit
func (i *Implementer) Visit(node ast.Node) (w ast.Visitor) {
if node == nil {
return nil
}
switch node := node.(type) {
case *ast.GenDecl:
// Replace the type declaration reference until the top-level type
// declaration with matching type name is found.
if !i.found && node.Tok == token.TYPE {
i.typeDecl = node
return i
}
case *ast.TypeSpec:
if getIdent(node, 0) == i.recvName {
i.found = true
}
case *ast.FuncDecl:
if node.Recv != nil && node.Name != nil {
for _, r := range node.Recv.List {
name := getIdent(r.Type, -1)
if name == i.recvName {
i.methods[node.Name.Name] = node
}
}
}
case *ast.File:
//Continue parsing files
return i
}
return nil
}
// Position returns, if found, the token.Position of the end of the type
// declaration for the specified receiver.
func (i *Implementer) Position() (*token.Position, error) {
err := i.init()
if err != nil {
return nil, err
}
p := i.fset.Position(i.typeDecl.End())
return &p, nil
}
// GenStubs prints nicely formatted method stubs for fns using receiver
// expression recv. If the Implementer is not in a valid state, or an error
// occurs, the error will be returned.
func (i *Implementer) GenStubs() ([]byte, error) {
err := i.init()
if err != nil {
return nil, err
}
for _, fn := range i.funcs {
if _, ok := i.methods[fn.Name]; !ok {
meth := Method{Recv: i.Recv, Func: fn}
tmpl.Execute(i.buf, meth)
}
}
return format.Source(i.buf.Bytes())
}
// ensureOffset will ensure that, given a file:line:col generated position, the
// offset is correct for the file.
func ensureOffset(p *token.Position) error {
if p.Offset != 0 || (p.Line == 0 && p.Column == 0) {
return nil
}
bs, err := ioutil.ReadFile(p.Filename)
if err != nil {
return err
}
col, line := 1, 1
for i := range bs {
col++
if line == p.Line && col == p.Column {
p.Offset = i
return nil
}
if bs[i] == '\n' {
col = 0
line++
continue
}
}
return fmt.Errorf("Could not find %s", p)
}
// GenForPosition allows users to have more flexible stub generation, with the
// ability to specify exactly where the implementation should be generated. If
// the token.Position argument is nil, the generated code will be inserted
// immediately after the receiving type's declaration.
func (i *Implementer) GenForPosition(p *token.Position) ([]byte, error) {
src, err := i.GenStubs()
if err != nil {
return nil, err
}
err = ensureOffset(p)
if err != nil {
return nil, err
}
newline := []byte("\n\n")
src = bytes.Join([][]byte{newline, src, newline}, nil)
i.walk()
if !i.found {
return nil, fmt.Errorf("requested receiver not found")
}
if p == nil {
pp := i.fset.Position(i.typeDecl.End())
p = &pp
}
orig, err := ioutil.ReadFile(p.Filename)
if err != nil {
return nil, err
}
result := &bytes.Buffer{}
result.Write(orig[:p.Offset])
result.Write(src)
result.Write(orig[p.Offset:])
return format.Source(result.Bytes())
}
// validReceiver reports whether recv is a valid receiver expression.
func (i *Implementer) validateReceiver() error {
err := i.init()
if err != nil {
return err
}
if i.Recv == "" {
// The parse will parse empty receivers, but we don't want to accept them,
// since it won't generate a usable code snippet.
return fmt.Errorf("receiver was the empty string")
}
i.fset = token.NewFileSet()
i.file, err = parser.ParseDir(i.fset, i.Dir, nil, 0)
return err
}
func (i *Implementer) init() error {
if i.buf != nil {
// Already initialized
return nil
}
i.buf = &bytes.Buffer{}
i.file = map[string]*ast.Package{}
i.methods = map[string]*ast.FuncDecl{}
if i.Recv == "" || i.IFace == "" {
return fmt.Errorf("Receiver and interface must both be specified")
}
if i.Dir == "" {
i.Dir = "."
}
err := i.validateReceiver()
if err != nil {
return err
}
i.funcs, err = funcs(i.IFace)
if err != nil {
return err
}
return i.walk()
}
func (i *Implementer) walk() error {
if i.found {
return nil
}
var err error
i.recvName, err = getType(i.Recv)
if err != nil {
return err
}
for _, pkg := range i.file {
for _, file := range pkg.Files {
ast.Walk(i, file)
}
}
return nil
}