Skip to content

Commit

Permalink
internal/imports: force to repair imports group regardless of how the…
Browse files Browse the repository at this point in the history
…y were originally grouped
  • Loading branch information
zoumo committed Sep 5, 2019
1 parent 85edb9e commit 21a9fe9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/goimports/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (

func init() {
flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)")
flag.BoolVar(&options.Env.RepairGroup, "repair-group", false, "force to repair imports group regardless of how they were originally grouped")
flag.StringVar(&options.Env.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
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.")
}
Expand Down
1 change: 1 addition & 0 deletions internal/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ func getAllCandidates(filename string, env *ProcessEnv) ([]ImportFix, error) {
type ProcessEnv struct {
LocalPrefix string
Debug bool
RepairGroup bool

// If non-empty, these will be used instead of the
// process-wide values.
Expand Down
13 changes: 8 additions & 5 deletions internal/imports/sortimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ func sortImports(env *ProcessEnv, fset *token.FileSet, f *ast.File) {
// Identify and sort runs of specs on successive lines.
i := 0
specs := d.Specs[:0]
for j, s := range d.Specs {
if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
// j begins a new run. End this one.
specs = append(specs, sortSpecs(env, fset, f, d.Specs[i:j])...)
i = j
// If repairGroup is true, treats all specs in the same import group.
if !env.RepairGroup {
for j, s := range d.Specs {
if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
// j begins a new run. End this one.
specs = append(specs, sortSpecs(env, fset, f, d.Specs[i:j])...)
i = j
}
}
}
specs = append(specs, sortSpecs(env, fset, f, d.Specs[i:])...)
Expand Down

0 comments on commit 21a9fe9

Please sign in to comment.