@@ -123,22 +123,34 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
123
123
comments := f .Comments [cstart :cend ]
124
124
125
125
// Assign each comment to the import spec preceding it.
126
- importComment := map [* ImportSpec ][]* CommentGroup {}
126
+ importComments := map [* ImportSpec ][]* CommentGroup {}
127
127
specIndex := 0
128
128
for _ , g := range comments {
129
129
for specIndex + 1 < len (specs ) && pos [specIndex + 1 ].Start <= g .Pos () {
130
130
specIndex ++
131
131
}
132
132
s := specs [specIndex ].(* ImportSpec )
133
- importComment [s ] = append (importComment [s ], g )
133
+ importComments [s ] = append (importComments [s ], g )
134
134
}
135
135
136
136
// Sort the import specs by import path.
137
137
// Remove duplicates, when possible without data loss.
138
138
// Reassign the import paths to have the same position sequence.
139
139
// Reassign each comment to abut the end of its spec.
140
140
// Sort the comments by new position.
141
- sort .Sort (byImportSpec (specs ))
141
+ sort .Slice (specs , func (i , j int ) bool {
142
+ ipath := importPath (specs [i ])
143
+ jpath := importPath (specs [j ])
144
+ if ipath != jpath {
145
+ return ipath < jpath
146
+ }
147
+ iname := importName (specs [i ])
148
+ jname := importName (specs [j ])
149
+ if iname != jname {
150
+ return iname < jname
151
+ }
152
+ return importComment (specs [i ]) < importComment (specs [j ])
153
+ })
142
154
143
155
// Dedup. Thanks to our sorting, we can just consider
144
156
// adjacent pairs of imports.
@@ -161,38 +173,16 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
161
173
}
162
174
s .Path .ValuePos = pos [i ].Start
163
175
s .EndPos = pos [i ].End
164
- for _ , g := range importComment [s ] {
176
+ for _ , g := range importComments [s ] {
165
177
for _ , c := range g .List {
166
178
c .Slash = pos [i ].End
167
179
}
168
180
}
169
181
}
170
182
171
- sort .Sort (byCommentPos (comments ))
183
+ sort .Slice (comments , func (i , j int ) bool {
184
+ return comments [i ].Pos () < comments [j ].Pos ()
185
+ })
172
186
173
187
return specs
174
188
}
175
-
176
- type byImportSpec []Spec // slice of *ImportSpec
177
-
178
- func (x byImportSpec ) Len () int { return len (x ) }
179
- func (x byImportSpec ) Swap (i , j int ) { x [i ], x [j ] = x [j ], x [i ] }
180
- func (x byImportSpec ) Less (i , j int ) bool {
181
- ipath := importPath (x [i ])
182
- jpath := importPath (x [j ])
183
- if ipath != jpath {
184
- return ipath < jpath
185
- }
186
- iname := importName (x [i ])
187
- jname := importName (x [j ])
188
- if iname != jname {
189
- return iname < jname
190
- }
191
- return importComment (x [i ]) < importComment (x [j ])
192
- }
193
-
194
- type byCommentPos []* CommentGroup
195
-
196
- func (x byCommentPos ) Len () int { return len (x ) }
197
- func (x byCommentPos ) Swap (i , j int ) { x [i ], x [j ] = x [j ], x [i ] }
198
- func (x byCommentPos ) Less (i , j int ) bool { return x [i ].Pos () < x [j ].Pos () }
0 commit comments