-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimports.go
78 lines (63 loc) · 1.63 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
package tracegen
import (
"fmt"
"go/token"
"strconv"
"strings"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
)
func addImport(pkg *decorator.Package, file *dst.File, imp string) {
for _, pkg := range pkg.Imports {
if pkg.Name == imp {
return
}
}
// Where to insert our import block within the file's Decl slice
index := 0
importSpec := &dst.ImportSpec{
Path: &dst.BasicLit{Kind: token.STRING, Value: fmt.Sprintf("%q", imp)},
}
for i, node := range file.Decls {
n, ok := node.(*dst.GenDecl)
if !ok {
continue
}
if n.Tok != token.IMPORT {
continue
}
if len(n.Specs) == 1 && mustUnquote(n.Specs[0].(*dst.ImportSpec).Path.Value) == "C" {
// If we're going to insert, it must be after the "C" import
index = i + 1
continue
}
// Insert our import into the first non-"C" import block
for j, spec := range n.Specs {
path := mustUnquote(spec.(*dst.ImportSpec).Path.Value)
if !strings.Contains(path, ".") || imp > path {
continue
}
importSpec.Decorations().Before = spec.Decorations().Before
spec.Decorations().Before = dst.NewLine
n.Specs = append(n.Specs[:j], append([]dst.Spec{importSpec}, n.Specs[j:]...)...)
return
}
n.Specs = append(n.Specs, importSpec)
return
}
gd := &dst.GenDecl{
Tok: token.IMPORT,
Specs: []dst.Spec{importSpec},
Decs: dst.GenDeclDecorations{
NodeDecs: dst.NodeDecs{Before: dst.EmptyLine, After: dst.EmptyLine},
},
}
file.Decls = append(file.Decls[:index], append([]dst.Decl{gd}, file.Decls[index:]...)...)
}
func mustUnquote(s string) string {
out, err := strconv.Unquote(s)
if err != nil {
panic(err)
}
return out
}