-
Notifications
You must be signed in to change notification settings - Fork 0
/
imports_builder.go
260 lines (219 loc) · 6.67 KB
/
imports_builder.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
package gofancyimports
import (
"go/ast"
"go/token"
"strings"
"github.com/NonLogicalDev/gofancyimports/internal/astutils"
"github.com/NonLogicalDev/gofancyimports/pkg/types"
)
func buildImportDecls(offset token.Pos, decls []types.ImportDeclaration) ([]ast.Decl, []token.Pos, token.Pos) {
var astDecls []ast.Decl
if len(decls) == 0 {
return nil, nil, 0
}
var newLines []token.Pos
for _, d := range decls {
decl, nl, newOffset := buildImportDecl(offset, d)
newLines = append(newLines, nl...)
astDecls = append(astDecls, decl)
offset = newOffset
}
return astDecls, newLines, offset
}
func buildImportDecl(offset token.Pos, decl types.ImportDeclaration) (ast.Decl, []token.Pos, token.Pos) {
var newLines []token.Pos
astDecl := &ast.GenDecl{
Tok: token.IMPORT,
}
// Prepare Doc comment for the import group.
var astCommentList []*ast.Comment
for _, cg := range decl.LeadingComments {
astCommentList = append(astCommentList, copyCommentList(cg.List)...)
}
if decl.Doc != nil {
astCommentList = append(astCommentList, copyCommentList(decl.Doc.List)...)
}
for _, cg := range decl.DetachedComments {
astCommentList = append(astCommentList, copyCommentList(cg.List)...)
}
astDecl.Doc = buildCombinedCommentGroup(offset, astCommentList)
// Place the doc comment at the current offset if it exists and calculate newlines.
if astDecl.Doc != nil {
newLines = buildCommentListNewlines(astDecl.Doc.List, newLines)
// Move offset to the next character after comment end.
offset = astDecl.Doc.End() + 1
// Assert newline at the end of the comment group.
newLines = append(newLines, offset)
}
// Place declaration at the offset.
astutils.ShiftGenDeclPos(offset-astDecl.Pos(), astDecl)
// Ensure declaration starts from a new line.
newLines = append(newLines, offset)
// After adjusting the position move offset past the import keyword.
offset += token.Pos(len(token.IMPORT.String()))
// Handle single import case (no parenthesis should be added).
if len(decl.ImportGroups) == 1 && len(decl.ImportGroups[0].Specs) == 1 {
g := decl.ImportGroups[0]
s := g.Specs[0]
astSpec := copyImportSpec(s)
var astSpecDoc *ast.CommentGroup
if g.Doc != nil {
astSpecDoc = buildCombinedCommentGroup(offset, g.Doc.List)
}
offset, newLines = buildImportSpec(offset, astSpec, astSpecDoc, newLines)
// Populate the import declaration with adjusted specs.
astDecl.Specs = []ast.Spec{astSpec}
// Make sure we have a newline at the end of the declaration.
newLines = append(newLines, offset)
offset++
} else {
// Force add parenthesis if it does not exist.
astDecl.Lparen = offset
offset++
for i, g := range decl.ImportGroups {
if len(g.Specs) == 0 {
continue
}
if i != 0 {
// And an extra spacer line to separate groups from each other.
newLines = append(newLines, offset)
offset++
}
for specIdx, s := range g.Specs {
astSpec := copyImportSpec(s)
var astSpecDoc *ast.CommentGroup
if specIdx == 0 && g.Doc != nil {
astSpecDoc = buildCombinedCommentGroup(offset, g.Doc.List)
}
offset, newLines = buildImportSpec(offset, astSpec, astSpecDoc, newLines)
astDecl.Specs = append(astDecl.Specs, astSpec)
// Make sure there is a newline at the end of the spec.
newLines = append(newLines, offset)
offset++
}
}
astDecl.Rparen = offset
offset = astDecl.End() + 1
// Make sure we have a newline at the end of the declaration.
newLines = append(newLines, offset)
offset++
}
return astDecl, newLines, offset
}
func buildImportSpec(offset token.Pos, astSpec *ast.ImportSpec, astSpecDoc *ast.CommentGroup, newLines []token.Pos) (token.Pos, []token.Pos) {
// For the first astDecl in the group, attach the doc comment.
if astSpecDoc != nil {
astSpec.Doc = copyCommentGroup(astSpecDoc)
astutils.ShiftCommentGroupPos(offset-astSpec.Doc.Pos(), astSpec.Doc)
newLines = buildCommentListNewlines(astSpec.Doc.List, newLines)
offset = astSpec.Doc.End() + 1
// Assert newline at the end of the comment group.
newLines = append(newLines, offset)
}
// Place the identity value
if astSpec.Name != nil {
astSpec.Name.NamePos = offset
offset += specSpan(astSpec.Name) + 1
}
// Place the path value
astSpec.Path.ValuePos = offset
offset += specSpan(astSpec.Path) + 1
// Place the line comment at the end of the astSpec.
if astSpec.Comment != nil {
astutils.ShiftCommentGroupPos(offset-astSpec.Comment.Pos(), astSpec.Comment)
offset += specSpan(astSpec.Comment) + 1
}
return offset, newLines
}
func buildCombinedCommentGroup(offset token.Pos, astCommentList []*ast.Comment) *ast.CommentGroup {
// Adjust comment positions, so that they are relatively correct.
// They will be shifted into correct place later.
for _, c := range astCommentList {
c.Slash = offset
cRange := astutils.ASTNodeRangeWithComments(c)
offset += (cRange.End - cRange.Pos) + 2
}
if len(astCommentList) == 0 {
return nil
}
return &ast.CommentGroup{
List: astCommentList,
}
}
func buildCommentListNewlines(comments []*ast.Comment, newLines []token.Pos) []token.Pos {
for _, c := range comments {
// Ensure all comments start from new line.
// c.Pos() reports correct offset since we have already aligned comments earlier.
newLines = append(newLines, c.Pos())
// All newlines inside the comments need to be preserved otherwise printer will not be happy.
for _, idx := range findAllIndexes(c.Text, "\n") {
newLines = append(newLines, c.Pos()+token.Pos(idx))
}
}
return newLines
}
func copyComment(c *ast.Comment) *ast.Comment {
if c == nil {
return nil
}
return &ast.Comment{
Text: c.Text,
}
}
func copyCommentList(cs []*ast.Comment) (r []*ast.Comment) {
r = make([]*ast.Comment, len(cs))
for i, c := range cs {
r[i] = copyComment(c)
}
return r
}
func copyCommentGroup(cg *ast.CommentGroup) *ast.CommentGroup {
if cg == nil {
return nil
}
return &ast.CommentGroup{List: copyCommentList(cg.List)}
}
func copyImportSpec(is *ast.ImportSpec) *ast.ImportSpec {
if is == nil {
return nil
}
return &ast.ImportSpec{
Doc: copyCommentGroup(is.Doc),
Name: copyIdent(is.Name),
Path: copyBasicLit(is.Path),
Comment: copyCommentGroup(is.Comment),
}
}
func copyBasicLit(l *ast.BasicLit) *ast.BasicLit {
if l == nil {
return nil
}
return &ast.BasicLit{
Kind: l.Kind,
Value: l.Value,
}
}
func copyIdent(id *ast.Ident) *ast.Ident {
if id == nil {
return nil
}
return &ast.Ident{
Name: id.Name,
Obj: id.Obj,
}
}
func specSpan(node ast.Node) token.Pos {
return node.End() - node.Pos()
}
func findAllIndexes(s string, c string) []int {
var r []int
for i := 0; i < len(s); {
idx := strings.Index(s[i:], c)
if idx < 0 {
break
}
r = append(r, i+idx)
i += idx + len(c)
}
return r
}