/
sortimports.go
113 lines (101 loc) · 2.86 KB
/
sortimports.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
// Copyright 2016 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Based on golang.org/x/tools/imports which bears the following license:
//
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ptimports
import (
"bytes"
"go/ast"
"go/parser"
"go/printer"
"go/token"
)
func fixImports(fset *token.FileSet, f *ast.File) (cImportDocs []*ast.CommentGroup, rErr error) {
imports, cImports, cImportsDocs := takeImports(f)
if imports == nil || len(imports.Specs) == 0 {
return
}
fixParens(imports)
f.Decls = append(cImports, append([]ast.Decl{imports}, f.Decls...)...)
var comments []*ast.CommentGroup
for _, fileComment := range f.Comments {
skip := false
for _, cImportComment := range cImportsDocs {
if fileComment == cImportComment {
skip = true
break
}
}
if skip {
continue
}
comments = append(comments, fileComment)
}
f.Comments = comments
buf := &bytes.Buffer{}
if err := printer.Fprint(buf, fset, f); err != nil {
return nil, err
}
newF, err := parser.ParseFile(fset, f.Name.Name, buf, parser.ParseComments)
if err != nil {
return nil, err
}
*f = *newF
return cImportsDocs, nil
}
func takeImports(f *ast.File) (imports *ast.GenDecl, cImports []ast.Decl, cImportDocs []*ast.CommentGroup) {
for len(f.Decls) > 0 {
d, ok := f.Decls[0].(*ast.GenDecl)
if !ok || d.Tok != token.IMPORT {
// Not an import declaration, so we're done.
// Import decls are always first.
break
}
cImport := false
if len(d.Specs) > 0 {
for _, spec := range d.Specs {
impSpec := spec.(*ast.ImportSpec)
if impSpec.Path.Value == `"C"` {
cImport = true
cImportDocs = append(cImportDocs, d.Doc)
d.Doc = nil
}
}
}
if cImport {
cImports = append(cImports, d)
} else if imports == nil {
imports = d
} else {
if imports.Doc == nil {
imports.Doc = d.Doc
} else if d.Doc != nil {
imports.Doc.List = append(imports.Doc.List, d.Doc.List...)
}
imports.Specs = append(imports.Specs, d.Specs...)
}
// Put back later in a single decl
f.Decls = f.Decls[1:]
}
return imports, cImports, cImportDocs
}
// All import decls require parens, even with only a single import.
func fixParens(d *ast.GenDecl) {
if !d.Lparen.IsValid() {
d.Lparen = d.Specs[0].Pos()
}
}