...
Source file
src/go/ast/import.go
Documentation: go/ast
1
2
3
4
5 package ast
6
7 import (
8 "go/token"
9 "sort"
10 "strconv"
11 )
12
13
14
15 func SortImports(fset *token.FileSet, f *File) {
16 for _, d := range f.Decls {
17 d, ok := d.(*GenDecl)
18 if !ok || d.Tok != token.IMPORT {
19
20
21 break
22 }
23
24 if !d.Lparen.IsValid() {
25
26 continue
27 }
28
29
30 i := 0
31 specs := d.Specs[:0]
32 for j, s := range d.Specs {
33 if j > i && lineAt(fset, s.Pos()) > 1+lineAt(fset, d.Specs[j-1].End()) {
34
35 specs = append(specs, sortSpecs(fset, f, d.Specs[i:j])...)
36 i = j
37 }
38 }
39 specs = append(specs, sortSpecs(fset, f, d.Specs[i:])...)
40 d.Specs = specs
41
42
43 if len(d.Specs) > 0 {
44 lastSpec := d.Specs[len(d.Specs)-1]
45 lastLine := lineAt(fset, lastSpec.Pos())
46 rParenLine := lineAt(fset, d.Rparen)
47 for rParenLine > lastLine+1 {
48 rParenLine--
49 fset.File(d.Rparen).MergeLine(rParenLine)
50 }
51 }
52 }
53 }
54
55 func lineAt(fset *token.FileSet, pos token.Pos) int {
56 return fset.PositionFor(pos, false).Line
57 }
58
59 func importPath(s Spec) string {
60 t, err := strconv.Unquote(s.(*ImportSpec).Path.Value)
61 if err == nil {
62 return t
63 }
64 return ""
65 }
66
67 func importName(s Spec) string {
68 n := s.(*ImportSpec).Name
69 if n == nil {
70 return ""
71 }
72 return n.Name
73 }
74
75 func importComment(s Spec) string {
76 c := s.(*ImportSpec).Comment
77 if c == nil {
78 return ""
79 }
80 return c.Text()
81 }
82
83
84 func collapse(prev, next Spec) bool {
85 if importPath(next) != importPath(prev) || importName(next) != importName(prev) {
86 return false
87 }
88 return prev.(*ImportSpec).Comment == nil
89 }
90
91 type posSpan struct {
92 Start token.Pos
93 End token.Pos
94 }
95
96 type cgPos struct {
97 left bool
98 cg *CommentGroup
99 }
100
101 func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
102
103
104
105 if len(specs) <= 1 {
106 return specs
107 }
108
109
110 pos := make([]posSpan, len(specs))
111 for i, s := range specs {
112 pos[i] = posSpan{s.Pos(), s.End()}
113 }
114
115
116 begSpecs := pos[0].Start
117 endSpecs := pos[len(pos)-1].End
118 beg := fset.File(begSpecs).LineStart(lineAt(fset, begSpecs))
119 endLine := lineAt(fset, endSpecs)
120 endFile := fset.File(endSpecs)
121 var end token.Pos
122 if endLine == endFile.LineCount() {
123 end = endSpecs
124 } else {
125 end = endFile.LineStart(endLine + 1)
126 }
127 first := len(f.Comments)
128 last := -1
129 for i, g := range f.Comments {
130 if g.End() >= end {
131 break
132 }
133
134 if beg <= g.Pos() {
135
136 if i < first {
137 first = i
138 }
139 if i > last {
140 last = i
141 }
142 }
143 }
144
145 var comments []*CommentGroup
146 if last >= 0 {
147 comments = f.Comments[first : last+1]
148 }
149
150
151 importComments := map[*ImportSpec][]cgPos{}
152 specIndex := 0
153 for _, g := range comments {
154 for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() {
155 specIndex++
156 }
157 var left bool
158
159 if specIndex == 0 && pos[specIndex].Start > g.Pos() {
160 left = true
161 } else if specIndex+1 < len(specs) &&
162 lineAt(fset, pos[specIndex].Start)+1 == lineAt(fset, g.Pos()) {
163 specIndex++
164 left = true
165 }
166 s := specs[specIndex].(*ImportSpec)
167 importComments[s] = append(importComments[s], cgPos{left: left, cg: g})
168 }
169
170
171
172
173
174
175 sort.Slice(specs, func(i, j int) bool {
176 ipath := importPath(specs[i])
177 jpath := importPath(specs[j])
178 if ipath != jpath {
179 return ipath < jpath
180 }
181 iname := importName(specs[i])
182 jname := importName(specs[j])
183 if iname != jname {
184 return iname < jname
185 }
186 return importComment(specs[i]) < importComment(specs[j])
187 })
188
189
190
191 deduped := specs[:0]
192 for i, s := range specs {
193 if i == len(specs)-1 || !collapse(s, specs[i+1]) {
194 deduped = append(deduped, s)
195 } else {
196 p := s.Pos()
197 fset.File(p).MergeLine(lineAt(fset, p))
198 }
199 }
200 specs = deduped
201
202
203 for i, s := range specs {
204 s := s.(*ImportSpec)
205 if s.Name != nil {
206 s.Name.NamePos = pos[i].Start
207 }
208 s.Path.ValuePos = pos[i].Start
209 s.EndPos = pos[i].End
210 for _, g := range importComments[s] {
211 for _, c := range g.cg.List {
212 if g.left {
213 c.Slash = pos[i].Start - 1
214 } else {
215
216
217
218
219 c.Slash = pos[i].End
220 }
221 }
222 }
223 }
224
225 sort.Slice(comments, func(i, j int) bool {
226 return comments[i].Pos() < comments[j].Pos()
227 })
228
229 return specs
230 }
231
View as plain text