forked from davidrjenni/A
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
340 lines (308 loc) · 10.3 KB
/
commands.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2017 David R. Jenni. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/cmd/guru/serial"
)
type gomodifytagsOutput struct {
Start int `json:"start"`
End int `json:"end"`
Lines []string `json:"lines"`
Errs []string `json:"errors"`
}
// addTags adds tags to the selected struct fields
// using github.com/fatih/gomodifytags.
func addTags(s selection, args []string) {
if len(args) < 1 {
log.Fatal(`Usage: A addtags <tags> [options]
<tags>: comma-separated tags to add, e.g. json,xml
[options]: options to add, e.g. 'json=omitempty'`)
}
arguments := []string{
"-file", s.filename(), "-modified", "-format", "json", "-line", s.lineSel(), "-add-tags", args[0],
}
if len(args) > 1 {
arguments = append(arguments, "-add-options", args[1])
}
buf := runWithStdin(s.archive(), "gomodifytags", arguments...)
var out gomodifytagsOutput
if err := json.Unmarshal([]byte(buf), &out); err != nil {
log.Fatal(err)
}
if err := s.win.Addr("%d,%d", out.Start, out.End); err != nil {
log.Fatal(err)
}
if _, err := s.win.Write("data", []byte(strings.Join(out.Lines, "\n")+"\n")); err != nil {
log.Fatal(err)
}
showAddr(s.win, s.start)
if len(out.Errs) != 0 {
fmt.Fprintln(os.Stderr, strings.Join(out.Errs, "\n"))
}
}
// callees shows possible targets of the selected function call
// using golang.org/x/tools/cmd/guru.
func callees(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-scope", scope(args), "-modified", "callees", s.pos()))
}
// callers shows possible callers of the selected function
// using golang.org/x/tools/cmd/guru.
func callers(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-scope", scope(args), "-modified", "callers", s.pos()))
}
// callstack shows the path from the callgraph root to the selected function
// using golang.org/x/tools/cmd/guru.
func callstack(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-scope", scope(args), "-modified", "callstack", s.pos()))
}
// definition shows declaration of selected identifier
// using golang.org/x/tools/cmd/guru.
func definition(s selection, args []string) {
var gd serial.Definition
js := runWithStdin(s.archive(), "guru", "-json", "-modified", "definition", s.pos())
if err := json.Unmarshal([]byte(js), &gd); err != nil {
log.Fatalf("failed to unmarshal guru json: %v\n", err)
}
if err := plumbText(gd.ObjPos); err != nil {
fmt.Println(gd.ObjPos)
log.Fatalf("failed to plumb: %v\n", err)
}
}
// describe describes the selected syntax: definition, methods, etc.
// using golang.org/x/tools/cmd/guru.
func describe(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-modified", "describe", s.pos()))
}
// godoc shows documentation for items in Go source code
// using github.com/zmb3/gogetdoc.
func godoc(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "gogetdoc", "-modified", "-pos", s.pos()))
}
// extract extracts statements to a new function/method
// using github.com/godoctor/godoctor.
func extract(s selection, args []string) {
if len(args) < 1 {
log.Fatal("Usage: A ex <name>")
}
pos := fmt.Sprintf("%d,%d", s.start, s.end-s.start)
stdin := bytes.NewReader(s.body)
code := runWithStdin(stdin, "godoctor", "-scope", ".", "-complete", "-file", s.filename(), "-pos", pos, "extract", args[0])
if i := strings.Index(code, "\n"); i != -1 {
code = code[i+1:]
}
writeBody(s.win, code)
showAddr(s.win, s.start)
}
type output struct {
Start int `json:"start"`
End int `json:"end"`
Code string `json:"code"`
}
// fillstruct fills a struct literal with default values
// using github.com/davidrjenni/reftools/cmd/fillstruct.
func fillstruct(s selection, args []string) {
buf := runWithStdin(s.archive(), "fillstruct", "-modified", "-file", s.filename(), "-offset", fmt.Sprintf("%d", s.start), "-line", fmt.Sprintf("%d", s.startLine))
var res []output
if err := json.Unmarshal([]byte(buf), &res); err != nil {
log.Fatal(err)
}
for _, out := range res {
if err := s.win.Addr("#%d,#%d", out.Start, out.End); err != nil {
log.Fatal(err)
}
if _, err := s.win.Write("data", []byte(out.Code)); err != nil {
log.Fatal(err)
}
}
showAddr(s.win, s.start)
}
// fillswitch fills a (type) switch statement with case statements.
// using github.com/davidrjenni/reftools/cmd/fillswitch.
func fillswitch(s selection, args []string) {
buf := runWithStdin(s.archive(), "fillswitch", "-modified", "-file", s.filename(), "-offset", fmt.Sprintf("%d", s.start), "-line", fmt.Sprintf("%d", s.startLine))
var res []output
if err := json.Unmarshal([]byte(buf), &res); err != nil {
log.Fatal(err)
}
for _, out := range res {
if err := s.win.Addr("#%d,#%d", out.Start, out.End); err != nil {
log.Fatal(err)
}
if _, err := s.win.Write("data", []byte(out.Code)); err != nil {
log.Fatal(err)
}
}
showAddr(s.win, s.start)
}
// freevars shows free variables of the selection
// using golang.org/x/tools/cmd/guru.
func freevars(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-modified", "freevars", s.sel()))
}
// impl generates method stubs for implementing an interface
// using github.com/josharian/impl.
func impl(s selection, args []string) {
if len(args) < 2 {
log.Fatal("Usage: A impl <recv> <iface>")
}
l := len(args) - 1
code := run("impl", strings.Join(args[0:l], " "), args[l:][0])
end := ""
if s.start < len(s.body)-1 {
end = string(s.body[s.start+1:])
}
writeBody(s.win, string(s.body[:s.start])+"\n"+code+end)
showAddr(s.win, s.start)
}
// implements shows the 'implements' relation for the selected type or method
// using golang.org/x/tools/cmd/guru.
func implements(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-modified", "-scope", scope(args), "implements", s.pos()))
}
// peers shows send/receive corresponding to selected channel op
// using golang.org/x/tools/cmd/guru.
func peers(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-modified", "-scope", scope(args), "peers", s.sel()))
}
// pointsto shows variables the selected pointer may point to
// using golang.org/x/tools/cmd/guru.
func pointsto(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-modified", "-scope", scope(args), "pointsto", s.sel()))
}
type posShortener string
func newPosShortener() posShortener {
wd, err := os.Getwd()
if err != nil {
return posShortener("")
}
return posShortener(wd)
}
// Do shortens the pos (of the form "file:line:col") by converting the
// file part to a relative path if it is shorter.
func (ps posShortener) do(pos string) string {
if ps == "" {
return pos
}
i := bytes.LastIndexByte([]byte(pos), ':')
if i < 0 {
return pos
}
i = bytes.LastIndexByte([]byte(pos[:i]), ':')
if i < 0 {
return pos
}
fpath, addr := pos[:i], pos[i:]
rel, err := filepath.Rel(string(ps), fpath)
if err != nil || len(rel) > len(fpath) {
return pos
}
return rel + addr
}
// referrers shows all refs to the entity denoted by selected identifier
// using golang.org/x/tools/cmd/guru.
func referrers(s selection, args []string) {
ps := newPosShortener()
var init serial.ReferrersInitial
js := runWithStdin(s.archive(), "guru", "-json", "-modified", "referrers", s.pos())
dec := json.NewDecoder(strings.NewReader(js))
if err := dec.Decode(&init); err != nil {
log.Fatalf("failed to unmarshal ReferrersInitial: %v\n", err)
}
fmt.Printf("%v: references to %v\n", ps.do(init.ObjPos), init.Desc)
for {
var pkg serial.ReferrersPackage
if err := dec.Decode(&pkg); err == io.EOF {
break
} else if err != nil {
log.Fatalf("failed to unmarshal ReferrersPackage: %v\n", err)
}
for _, r := range pkg.Refs {
fmt.Printf("%v: %v\n", ps.do(r.Pos), r.Text)
}
}
}
// rename renames the selected identifier
// using golang.org/x/tools/cmd/gorename.
func rename(s selection, args []string) {
if len(args) < 1 {
log.Fatal("Usage: A rn <name>")
}
run("gorename", "-offset", s.pos(), "-to", args[0])
reloadShowAddr(s.win, s.start)
}
// rmTags removes tags the selected struct fields
// using github.com/fatih/gomodifytags.
func rmTags(s selection, args []string) {
if len(args) < 1 {
log.Fatal(`Usage: A rmtags <tags> [options]
<tags>: comma-separated tags to remove, e.g. json,xml
[options]: options to remove, e.g. 'json=omitempty'`)
}
arguments := []string{
"-file", s.filename(), "-modified", "-format", "json", "-line", s.lineSel(), "-remove-tags", args[0],
}
if len(args) > 1 {
arguments = append(arguments, "-remove-options", args[1])
}
buf := runWithStdin(s.archive(), "gomodifytags", arguments...)
var out gomodifytagsOutput
if err := json.Unmarshal([]byte(buf), &out); err != nil {
log.Fatal(err)
}
if err := s.win.Addr("%d,%d", out.Start, out.End); err != nil {
log.Fatal(err)
}
if _, err := s.win.Write("data", []byte(strings.Join(out.Lines, "\n")+"\n")); err != nil {
log.Fatal(err)
}
showAddr(s.win, s.start)
if len(out.Errs) != 0 {
fmt.Fprintln(os.Stderr, strings.Join(out.Errs, "\n"))
}
}
// share uploads the selected code to play.golang.org
// and prints the generated URL.
func share(s selection, args []string) {
body := bytes.NewReader(s.body[s.start:s.end])
req, err := http.NewRequest("POST", "https://play.golang.org/share", body)
if err != nil {
log.Fatalf("cannot send snippet: %v", err)
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("cannot send snippet: %v", err)
}
defer rsp.Body.Close()
id, err := ioutil.ReadAll(rsp.Body)
if err != nil {
log.Fatalf("cannot send snippet: %v", err)
}
fmt.Printf("https://play.golang.org/p/%s\n", id)
}
// what shows basic information about the selected syntax node
// using golang.org/x/tools/cmd/guru.
func what(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-modified", "what", s.pos()))
}
// whicherrs shows possible values of the selected error variable
// using golang.org/x/tools/cmd/guru.
func whicherrs(s selection, args []string) {
fmt.Println(runWithStdin(s.archive(), "guru", "-modified", "-scope", scope(args), "whicherrs", s.pos()))
}
func scope(args []string) string {
if len(args) == 0 {
return "."
}
return args[0]
}