-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
258 lines (230 loc) · 7.02 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"regexp"
"sort"
"strings"
)
const (
exitBadFlags = 1
exitBadStdin = 2
exitInternalError = 3
versionStr = "go-groups version 1.1.3 (2020-10-14)"
)
var (
importStartRegex = regexp.MustCompile(`^\s*import\s*\(\s*$`)
importEndRegex = regexp.MustCompile(`^\s*\)\s*$`)
// any whitespace + any unicode_letter_or_underscore + any unicode_letter_or_underscore_or_unicode number + any whitespace + quote + any + quote + any.
groupedImportRegex = regexp.MustCompile(`^\s*[\p{L}_\\.]*[\s*[\p{L}_\p{N}]*\s*".*".*$`)
externalImport = regexp.MustCompile(`"([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?\/([\p{L}_\-\p{N}]*)\/?.*"`)
// see https://golang.org/pkg/cmd/go/internal/generate/ for details.
generatedRegex = regexp.MustCompile(`^// Code generated .* DO NOT EDIT.$`)
list = flag.Bool("l", false, "list files whose formatting differs")
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
version = flag.Bool("v", false, "display the version of go-groups")
noFormat = flag.Bool("f", false, "disables the automatic gofmt style fixes")
genCode = flag.Bool("g", false, "include generated code in analysis")
)
func main() {
flag.Usage = usage
flag.Parse()
if *version {
fmt.Println(versionStr)
os.Exit(0)
}
// stdin invocation
if flag.NArg() == 0 {
if *write {
_, _ = fmt.Fprintln(os.Stderr, "error: cannot use -w with standard input")
os.Exit(exitBadStdin)
}
if err := processFile("<standard input>", os.Stdin, os.Stdout, !*noFormat, *genCode); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "failed to parse stdin: "+err.Error())
os.Exit(exitBadFlags)
}
return
}
for i := 0; i < flag.NArg(); i++ {
path := flag.Arg(i)
switch dir, err := os.Stat(path); {
case err != nil:
_, _ = fmt.Fprintln(os.Stderr, "no files matching '"+path+"': "+err.Error())
os.Exit(exitBadFlags)
case dir.IsDir():
if err := walkDir(path); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "failed processing path "+path+": "+err.Error())
os.Exit(exitInternalError)
}
default:
_ = processFile(path, nil, os.Stdout, !*noFormat, *genCode)
}
}
}
func usage() {
_, _ = fmt.Fprintf(os.Stderr, "usage: %s [flags] [path ...]\n", os.Args[0])
flag.PrintDefaults()
}
type importGroup struct {
lineStart int
lineEnd int
lines []importLine
}
type importLine struct {
line string
contentAbove string
contentBelow string
}
func parse(src []byte) (result []byte, rewritten bool) {
groups := make([]importGroup, 0, 1)
contents := make(map[int]string, 128)
scanner := bufio.NewScanner(bytes.NewReader(src))
lines := make([]string, 0, 128)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
insideImports := false
var n int
var group importGroup
scanner = bufio.NewScanner(bytes.NewReader(src))
for n = 0; scanner.Scan(); n++ {
line := scanner.Text()
if insideImports {
if importEndRegex.MatchString(line) {
insideImports = false
group.lineEnd = n
groups = append(groups, group)
} else if groupedImportRegex.MatchString(line) {
importLine := importLine{
line: line,
}
var above, below int
for above = n - 1; above > 0; above-- {
if groupedImportRegex.MatchString(lines[above]) || importStartRegex.MatchString(lines[above]) {
above++
break
}
}
for below = n + 1; below < len(lines); below++ {
if importEndRegex.MatchString(lines[below]) {
below--
break
}
// if we hit an import beneath us, assume it owns the non-import content above itself, not us
if groupedImportRegex.MatchString(lines[below]) {
below = n
break
}
}
if above != n {
importLine.contentAbove = strings.Join(filterNewlines(lines[above:n]), "\n")
}
if below != n {
importLine.contentBelow = strings.Join(filterNewlines(lines[n+1:below+1]), "\n")
}
group.lines = append(group.lines, importLine)
}
} else if importStartRegex.MatchString(line) {
insideImports = true
group = importGroup{
lineStart: n,
}
} else {
contents[n] = line
}
}
// nothing to do
if len(groups) == 0 {
return []byte{}, false
}
for i, group := range groups {
groups[i] = regroupImportGroups(group)
}
fileBytes := fixupFile(contents, n, groups)
return fileBytes, true
}
func filterNewlines(lines []string) []string {
filtered := make([]string, 0, len(lines))
for _, line := range lines {
if strings.TrimSpace(line) != "" {
filtered = append(filtered, line)
}
}
return filtered
}
func fixupFile(contents map[int]string, numLines int, groups []importGroup) []byte {
buffer := bytes.NewBufferString("")
for i := 0; i < numLines; i++ {
line, ok := contents[i]
if ok {
buffer.WriteString(line)
buffer.WriteString("\n")
continue
}
for _, group := range groups {
if group.lineStart != i {
continue
}
buffer.WriteString("import (\n")
leadingWhitespace := true
for _, importLine := range group.lines {
if leadingWhitespace && strings.TrimSpace(importLine.line) == "" {
// skip empty leading import lines
} else {
if importLine.contentAbove != "" {
buffer.WriteString(importLine.contentAbove)
buffer.WriteString("\n")
}
buffer.WriteString(importLine.line)
buffer.WriteString("\n")
if importLine.contentBelow != "" {
buffer.WriteString(importLine.contentBelow)
buffer.WriteString("\n")
}
leadingWhitespace = false
}
}
buffer.WriteString(")\n")
i = group.lineEnd
break
}
}
return buffer.Bytes()
}
// regroupImportGroups iterates each line of the import group and sorts the imports
// standard library imports are grouped together and sorted alphabetically
// each second-level external import is grouped together (e.g github.com/pkg.* is one group)
// each of these second-level groups is discovered and sorted alphabetically
// then each import is matched with their group and the list of lines to be written is built up.
func regroupImportGroups(group importGroup) importGroup {
standardImports := make(Imports, 0, len(group.lines))
sortedKeys := make([]string, 0)
groupNames := make(map[string]Imports)
for _, importLine := range group.lines {
matches := externalImport.FindStringSubmatch(importLine.line)
if matches != nil && strings.ContainsAny(importLine.line, ".") {
groupName := strings.Join(matches[1:], "")
if groupNames[groupName] == nil {
groupNames[groupName] = make(Imports, 0, 1)
sortedKeys = append(sortedKeys, groupName)
}
groupNames[groupName] = append(groupNames[groupName], importLine)
} else {
standardImports = append(standardImports, importLine)
}
}
sort.Sort(standardImports)
sort.Strings(sortedKeys)
group.lines = standardImports
for _, groupName := range sortedKeys {
imports := groupNames[groupName]
sort.Sort(imports)
group.lines = append(group.lines, importLine{})
group.lines = append(group.lines, imports...)
}
return group
}