Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/imports: force to repair imports group regardless of how the… #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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