1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Hacked up copy of go/ast/import.go
16 // sortImports sorts runs of consecutive import lines in import blocks in f.
17 // It also removes duplicate imports when it is possible to do so without data loss.
18 func sortImports(localPrefix string, fset *token.FileSet, f *ast.File) {
19 for i, d := range f.Decls {
20 d, ok := d.(*ast.GenDecl)
21 if !ok || d.Tok != token.IMPORT {
22 // Not an import declaration, so we're done.
23 // Imports are always first.
27 if len(d.Specs) == 0 {
28 // Empty import block, remove it.
29 f.Decls = append(f.Decls[:i], f.Decls[i+1:]...)
32 if !d.Lparen.IsValid() {
33 // Not a block: sorted by default.
37 // Identify and sort runs of specs on successive lines.
40 for j, s := range d.Specs {
41 if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
42 // j begins a new run. End this one.
43 specs = append(specs, sortSpecs(localPrefix, fset, f, d.Specs[i:j])...)
47 specs = append(specs, sortSpecs(localPrefix, fset, f, d.Specs[i:])...)
50 // Deduping can leave a blank line before the rparen; clean that up.
52 lastSpec := d.Specs[len(d.Specs)-1]
53 lastLine := fset.Position(lastSpec.Pos()).Line
54 if rParenLine := fset.Position(d.Rparen).Line; rParenLine > lastLine+1 {
55 fset.File(d.Rparen).MergeLine(rParenLine - 1)
61 // mergeImports merges all the import declarations into the first one.
62 // Taken from golang.org/x/tools/ast/astutil.
63 func mergeImports(fset *token.FileSet, f *ast.File) {
64 if len(f.Decls) <= 1 {
68 // Merge all the import declarations into the first one.
69 var first *ast.GenDecl
70 for i := 0; i < len(f.Decls); i++ {
72 gen, ok := decl.(*ast.GenDecl)
73 if !ok || gen.Tok != token.IMPORT || declImports(gen, "C") {
78 continue // Don't touch the first one.
80 // We now know there is more than one package in this import
81 // declaration. Ensure that it ends up parenthesized.
82 first.Lparen = first.Pos()
83 // Move the imports of the other import declaration to the first one.
84 for _, spec := range gen.Specs {
85 spec.(*ast.ImportSpec).Path.ValuePos = first.Pos()
86 first.Specs = append(first.Specs, spec)
88 f.Decls = append(f.Decls[:i], f.Decls[i+1:]...)
93 // declImports reports whether gen contains an import of path.
94 // Taken from golang.org/x/tools/ast/astutil.
95 func declImports(gen *ast.GenDecl, path string) bool {
96 if gen.Tok != token.IMPORT {
99 for _, spec := range gen.Specs {
100 impspec := spec.(*ast.ImportSpec)
101 if importPath(impspec) == path {
108 func importPath(s ast.Spec) string {
109 t, err := strconv.Unquote(s.(*ast.ImportSpec).Path.Value)
116 func importName(s ast.Spec) string {
117 n := s.(*ast.ImportSpec).Name
124 func importComment(s ast.Spec) string {
125 c := s.(*ast.ImportSpec).Comment
132 // collapse indicates whether prev may be removed, leaving only next.
133 func collapse(prev, next ast.Spec) bool {
134 if importPath(next) != importPath(prev) || importName(next) != importName(prev) {
137 return prev.(*ast.ImportSpec).Comment == nil
140 type posSpan struct {
145 func sortSpecs(localPrefix string, fset *token.FileSet, f *ast.File, specs []ast.Spec) []ast.Spec {
146 // Can't short-circuit here even if specs are already sorted,
147 // since they might yet need deduplication.
148 // A lone import, however, may be safely ignored.
153 // Record positions for specs.
154 pos := make([]posSpan, len(specs))
155 for i, s := range specs {
156 pos[i] = posSpan{s.Pos(), s.End()}
159 // Identify comments in this range.
160 // Any comment from pos[0].Start to the final line counts.
161 lastLine := fset.Position(pos[len(pos)-1].End).Line
162 cstart := len(f.Comments)
163 cend := len(f.Comments)
164 for i, g := range f.Comments {
165 if g.Pos() < pos[0].Start {
171 if fset.Position(g.End()).Line > lastLine {
176 comments := f.Comments[cstart:cend]
178 // Assign each comment to the import spec preceding it.
179 importComment := map[*ast.ImportSpec][]*ast.CommentGroup{}
181 for _, g := range comments {
182 for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() {
185 s := specs[specIndex].(*ast.ImportSpec)
186 importComment[s] = append(importComment[s], g)
189 // Sort the import specs by import path.
190 // Remove duplicates, when possible without data loss.
191 // Reassign the import paths to have the same position sequence.
192 // Reassign each comment to abut the end of its spec.
193 // Sort the comments by new position.
194 sort.Sort(byImportSpec{localPrefix, specs})
196 // Dedup. Thanks to our sorting, we can just consider
197 // adjacent pairs of imports.
199 for i, s := range specs {
200 if i == len(specs)-1 || !collapse(s, specs[i+1]) {
201 deduped = append(deduped, s)
204 fset.File(p).MergeLine(fset.Position(p).Line)
209 // Fix up comment positions
210 for i, s := range specs {
211 s := s.(*ast.ImportSpec)
213 s.Name.NamePos = pos[i].Start
215 s.Path.ValuePos = pos[i].Start
216 s.EndPos = pos[i].End
217 nextSpecPos := pos[i].End
219 for _, g := range importComment[s] {
220 for _, c := range g.List {
222 nextSpecPos = c.End()
225 if i < len(specs)-1 {
226 pos[i+1].Start = nextSpecPos
227 pos[i+1].End = nextSpecPos
231 sort.Sort(byCommentPos(comments))
233 // Fixup comments can insert blank lines, because import specs are on different lines.
234 // We remove those blank lines here by merging import spec to the first import spec line.
235 firstSpecLine := fset.Position(specs[0].Pos()).Line
236 for _, s := range specs[1:] {
238 line := fset.File(p).Line(p)
239 for previousLine := line - 1; previousLine >= firstSpecLine; {
240 fset.File(p).MergeLine(previousLine)
247 type byImportSpec struct {
249 specs []ast.Spec // slice of *ast.ImportSpec
252 func (x byImportSpec) Len() int { return len(x.specs) }
253 func (x byImportSpec) Swap(i, j int) { x.specs[i], x.specs[j] = x.specs[j], x.specs[i] }
254 func (x byImportSpec) Less(i, j int) bool {
255 ipath := importPath(x.specs[i])
256 jpath := importPath(x.specs[j])
258 igroup := importGroup(x.localPrefix, ipath)
259 jgroup := importGroup(x.localPrefix, jpath)
260 if igroup != jgroup {
261 return igroup < jgroup
267 iname := importName(x.specs[i])
268 jname := importName(x.specs[j])
273 return importComment(x.specs[i]) < importComment(x.specs[j])
276 type byCommentPos []*ast.CommentGroup
278 func (x byCommentPos) Len() int { return len(x) }
279 func (x byCommentPos) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
280 func (x byCommentPos) Less(i, j int) bool { return x[i].Pos() < x[j].Pos() }