Skip to content

Commit 21a9fe9

Browse files
committed
internal/imports: force to repair imports group regardless of how they were originally grouped
1 parent 85edb9e commit 21a9fe9

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

cmd/goimports/goimports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var (
5454

5555
func init() {
5656
flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)")
57+
flag.BoolVar(&options.Env.RepairGroup, "repair-group", false, "force to repair imports group regardless of how they were originally grouped")
5758
flag.StringVar(&options.Env.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
5859
flag.BoolVar(&options.FormatOnly, "format-only", false, "if true, don't fix imports and only format. In this mode, goimports is effectively gofmt, with the addition that imports are grouped into sections.")
5960
}

internal/imports/fix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ func getAllCandidates(filename string, env *ProcessEnv) ([]ImportFix, error) {
614614
type ProcessEnv struct {
615615
LocalPrefix string
616616
Debug bool
617+
RepairGroup bool
617618

618619
// If non-empty, these will be used instead of the
619620
// process-wide values.

internal/imports/sortimports.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ func sortImports(env *ProcessEnv, fset *token.FileSet, f *ast.File) {
3737
// Identify and sort runs of specs on successive lines.
3838
i := 0
3939
specs := d.Specs[:0]
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(env, fset, f, d.Specs[i:j])...)
44-
i = j
40+
// If repairGroup is true, treats all specs in the same import group.
41+
if !env.RepairGroup {
42+
for j, s := range d.Specs {
43+
if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
44+
// j begins a new run. End this one.
45+
specs = append(specs, sortSpecs(env, fset, f, d.Specs[i:j])...)
46+
i = j
47+
}
4548
}
4649
}
4750
specs = append(specs, sortSpecs(env, fset, f, d.Specs[i:])...)

0 commit comments

Comments
 (0)