/
imports.go
379 lines (346 loc) · 8.5 KB
/
imports.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Adapted from golang.org/x/tools/go/ast/astutil/imports.go
// and from gofix's import insertion code.
package refactor
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"sort"
"strconv"
"strings"
)
func deleteUnusedImports(s *Snapshot, p *Package, text []byte) []byte {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "out.go", text, parser.ParseComments)
if err != nil {
return text
}
used := make(map[string]bool)
Walk(file, func(stack []ast.Node) {
if id, ok := stack[0].(*ast.Ident); ok && id.Obj == nil {
if _, ok := stack[1].(*ast.SelectorExpr); !ok {
return
}
used[id.Name] = true
}
})
match := func(name, pkg string) bool {
if name == "" {
p1 := s.pkgGraph.byPath(pkg)
if p1 == nil {
panic("NO IMPORT: " + pkg)
}
name = p1.Name
}
return !used[name]
}
buf := NewBufferAt(s, 1, text)
for _, decl := range file.Decls {
gen, ok := decl.(*ast.GenDecl)
if !ok || gen.Tok != token.IMPORT {
continue
}
complete := true
any := false
for _, spec := range gen.Specs {
spec := spec.(*ast.ImportSpec)
if !match(importName(spec), importPath(spec)) {
complete = false
continue
}
any = true
}
if complete {
buf.Delete(nodeRange(decl, text))
continue
}
if !any {
continue
}
for _, spec := range gen.Specs {
spec := spec.(*ast.ImportSpec)
if match(importName(spec), importPath(spec)) {
buf.Delete(nodeRange(spec, text))
}
}
}
return buf.Bytes()
}
var (
slashSlash = []byte("//")
starSlash = []byte("*/")
)
func nodeRange(n ast.Node, text []byte) (pos, end token.Pos) {
startFile, endFile := token.Pos(1), token.Pos(1+len(text))
pos = n.Pos()
end = n.End()
// Include space and comments following the node.
for end < endFile && text[end-startFile] == ' ' {
end++
}
if bytes.HasPrefix(text[end-startFile:], slashSlash) {
i := bytes.IndexByte(text[end-startFile:], '\n')
if i >= 0 {
end += token.Pos(i)
} else {
end = endFile
}
}
if end > n.End() && end < endFile && text[end-startFile] != '\n' {
// If we consumed spaces but did not reach a newline,
// put a space back to avoid joining tokens.
end--
}
// Include tabs preceding the node, to beginning of line.
// (If there are spaces before the node, it means something else
// precedes the node on the line, so don't bother removing anything.)
for pos > startFile && text[pos-startFile-1] == '\t' {
pos--
}
// Include comments "attached" to this node,
// but stopping at a blank line.
// Reading comments backward is a bit tricky:
// if we see a */, we need to stop and assume
// we don't know the state of the world.
for pos > startFile && text[pos-startFile-1] == '\n' {
i := bytes.LastIndexByte(text[:pos-startFile-1], '\n') + 1
line := text[i : pos-startFile]
line = bytes.TrimSpace(line)
if !bytes.HasPrefix(line, slashSlash) || bytes.Contains(line, starSlash) {
break
}
pos = startFile + token.Pos(i)
}
// Consume final \n if we are deleting the whole line.
if (pos == startFile || text[pos-startFile-1] == '\n') && end < endFile && text[end-startFile] == '\n' {
end++
}
return pos, end
}
// importName returns the name of s,
// or "" if the import is not named.
func importName(s *ast.ImportSpec) string {
if s.Name == nil {
return ""
}
return s.Name.Name
}
// importPath returns the unquoted import path of s,
// or "" if the path is not properly quoted.
func importPath(s *ast.ImportSpec) string {
t, err := strconv.Unquote(s.Path.Value)
if err != nil {
return ""
}
return t
}
func (s *Snapshot) NeedImport(pos token.Pos, id string, pkg *types.Package) string {
_, file := s.FileAt(pos)
if file == nil {
fmt.Println(s.Position(pos))
panic("no file")
}
want := id
if id == "" {
want = pkg.Name()
}
names := []string{want, want + "pkg", want + "_"}
for _, id := range names {
for _, imp := range file.Imports {
if importPath(imp) == pkg.Path() {
name := importName(imp)
if name == "" {
name = pkg.Name()
}
if name == id {
if obj := s.LookupAt(name, pos); obj == nil {
return name
} else if obj, ok := obj.(*types.PkgName); ok && obj.Pkg().Path() != pkg.Path() {
return name
}
}
}
}
}
want = ""
for _, id := range names {
if obj := s.LookupAt(id, pos); obj == nil {
want = id
break
} else if obj, ok := obj.(*types.PkgName); ok && obj.Pkg().Path() != pkg.Path() {
want = id
break
}
}
if want == "" {
s.ErrorAt(pos, "package name %s is shadowed", pkg.Name())
want = pkg.Name()
}
ed := s.editAt(file.Package)
key := NewImport{want, pkg}
for _, p := range ed.AddImports {
if p == key {
return want
}
}
ed.AddImports = append(ed.AddImports, key)
return want
}
func (s *Snapshot) addImports() {
for file, ed := range s.edits {
s.addImportList(file, ed.AddImports)
}
}
func (s *Snapshot) addImportList(file string, list []NewImport) {
_, f := s.FileByName(file)
if f == nil {
return
}
imps := f.Decls
for i, d := range f.Decls {
if d, ok := d.(*ast.GenDecl); !ok || d.Tok != token.IMPORT {
imps = f.Decls[:i]
break
}
}
// Assign each import to an import statement.
needs := make(map[*ast.ImportSpec][]NewImport)
impOf := make(map[*ast.ImportSpec]*ast.GenDecl)
var firstImp *ast.GenDecl
for _, need := range list {
// Find an import decl to add to.
// Same logic as go fix.
// Find an import decl to add to.
var (
bestMatch = -1
bestSpec *ast.ImportSpec
)
for i := range imps {
imp := imps[i].(*ast.GenDecl)
// Do not add to import "C", to avoid disrupting the
// association with its doc comment, breaking cgo.
if declImports(imp, "C") {
continue
}
if firstImp == nil {
firstImp = imp
}
// Compute longest shared prefix with imports in this block.
for j := range imp.Specs {
spec := imp.Specs[j].(*ast.ImportSpec)
impOf[spec] = imp
n := matchLen(importPath(spec), need.pkg.Path())
if n > bestMatch {
bestMatch = n
bestSpec = spec
}
}
}
needs[bestSpec] = append(needs[bestSpec], need)
}
makeBlock := func(imp *ast.GenDecl) {
if imp.Lparen == token.NoPos {
imp.Lparen = imp.TokPos + token.Pos(len("import"))
s.InsertAt(imp.Lparen, "(")
imp.Rparen = imp.End()
s.InsertAt(imp.Rparen+1, ")") // TODO: skip over comment; the +1 is to avoid conflict with the InsertAt in needs[nil] below
}
}
// Add imports near each target spec.
for i := range imps {
imp := imps[i].(*ast.GenDecl)
for j := range imp.Specs {
spec := imp.Specs[j].(*ast.ImportSpec)
if needs[spec] == nil {
continue
}
makeBlock(impOf[spec])
for _, need := range needs[spec] {
id := need.id
if id == need.pkg.Name() {
id = ""
}
s.InsertAt(spec.Pos(), fmt.Sprintf("%s %q\n", id, need.pkg.Path()))
}
}
}
if needs[nil] != nil {
// Imports we didn't know what to do with.
// Add new block to first (non-C) import, if any.
var buf bytes.Buffer
kind := -1
all := needs[nil]
sort.Slice(all, func(i, j int) bool {
if ki, kj := pathKind(all[i].pkg.Path()), pathKind(all[j].pkg.Path()); ki != kj {
return ki < kj
}
return all[i].pkg.Path() < all[j].pkg.Path()
})
for _, need := range all {
if k := pathKind(need.pkg.Path()); k != kind {
buf.WriteString("\n")
kind = k
}
id := need.id
if id == need.pkg.Name() {
id = ""
}
fmt.Fprintf(&buf, "%s %q\n", id, need.pkg.Path())
}
imp := firstImp
if imp != nil {
makeBlock(imp)
s.InsertAt(imp.Rparen, buf.String())
} else {
pos := f.Name.End()
if len(imps) > 0 {
pos = imps[len(imps)-1].End()
}
if len(needs[nil]) == 1 {
s.InsertAt(pos, "\nimport "+buf.String()[1:])
} else {
s.InsertAt(pos, "\nimport ("+buf.String()+")")
}
}
}
}
// declImports reports whether gen contains an import of path.
func declImports(gen *ast.GenDecl, path string) bool {
if gen.Tok != token.IMPORT {
return false
}
for _, spec := range gen.Specs {
impspec := spec.(*ast.ImportSpec)
if importPath(impspec) == path {
return true
}
}
return false
}
// matchLen returns the length of the longest prefix shared by x and y.
func matchLen(x, y string) int {
if pathKind(x) != pathKind(y) {
return -1
}
i := 0
for i < len(x) && i < len(y) && x[i] == y[i] {
i++
}
return i
}
func pathKind(x string) int {
first, _, _ := cut(x, "/")
if strings.Contains(first, ".") {
return 2
}
if first == "cmd" {
return 1
}
return 0
}