/
imports.go
146 lines (127 loc) · 3.2 KB
/
imports.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
// Copyright (c) 2016 Palantir Technologies Inc. All rights reserved.
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.
package amalgomate
import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/token"
"io"
"regexp"
"strconv"
"strings"
)
// importBreakPaths returns a slice that contains the import paths before which a line breaks should be inserted.
func importBreakPaths(file *ast.File) []string {
var output []string
for _, decl := range file.Decls {
if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.IMPORT {
for srcIndex, currSpec := range gen.Specs {
if srcIndex > 0 {
// if there was a previous element, check if the group has changed
currGroup := importGroup(importPath(currSpec))
prevGroup := importGroup(importPath(gen.Specs[srcIndex-1]))
if currGroup != prevGroup {
// if group has changed, add path to the output
output = append(output, importPath(gen.Specs[srcIndex]))
}
}
}
// assume that only one import token block exists
break
}
}
return output
}
// from github.com/palantir/go-palantir/ptimports
func importGroup(importPath string) int {
switch {
case inStandardLibrary(importPath):
return 0
default:
return 1
}
}
func inStandardLibrary(importPath string) bool {
return !strings.Contains(importPath, ".")
}
type importSlice []ast.Spec
func (x importSlice) Len() int { return len(x) }
func (x importSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x importSlice) Less(i, j int) bool {
ipath := importPath(x[i])
jpath := importPath(x[j])
igroup := importGroup(ipath)
jgroup := importGroup(jpath)
if igroup != jgroup {
return igroup < jgroup
}
if ipath != jpath {
return ipath < jpath
}
iname := importName(x[i])
jname := importName(x[j])
if iname != jname {
return iname < jname
}
return importComment(x[i]) < importComment(x[j])
}
func importPath(s ast.Spec) string {
t, err := strconv.Unquote(s.(*ast.ImportSpec).Path.Value)
if err == nil {
return t
}
return ""
}
func importName(s ast.Spec) string {
n := s.(*ast.ImportSpec).Name
if n == nil {
return ""
}
return n.Name
}
func importComment(s ast.Spec) string {
c := s.(*ast.ImportSpec).Comment
if c == nil {
return ""
}
return c.Text()
}
// from golang.org/x/tools/cmd/goimports/imports.go
var impLine = regexp.MustCompile(`^\s+(?:[\w\.]+\s+)?"(.+)"`)
func addImportSpaces(r io.Reader, breaks []string) []byte {
var out bytes.Buffer
sc := bufio.NewScanner(r)
inImports := false
done := false
for sc.Scan() {
s := sc.Text()
if !inImports && !done && strings.HasPrefix(s, "import") {
inImports = true
}
if inImports && (s == ")" ||
strings.HasPrefix(s, "var") ||
strings.HasPrefix(s, "func") ||
strings.HasPrefix(s, "const") ||
strings.HasPrefix(s, "type")) {
done = true
inImports = false
}
if inImports && len(breaks) > 0 {
if m := impLine.FindStringSubmatch(s); m != nil {
if m[1] == breaks[0] {
if err := out.WriteByte('\n'); err != nil {
panic(fmt.Errorf("failed to write newline in addImportSpaces"))
}
breaks = breaks[1:]
}
}
}
if !inImports || s != "" {
fmt.Fprintln(&out, s)
}
}
return out.Bytes()
}