Skip to content

Commit

Permalink
add possibility to ignore custom user's import grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Dec 26, 2018
1 parent d00ac6d commit fcc3616
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/goimports/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
func init() {
flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)")
flag.StringVar(&imports.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
flag.BoolVar(&options.IgnoreGrouping, "ungroup", false, "ignore user's custom import grouping")
}

func report(err error) {
Expand Down
5 changes: 3 additions & 2 deletions imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Options struct {
TabWidth int // Tab width (8 if nil *Options provided)

FormatOnly bool // Disable the insertion and deletion of imports

IgnoreGrouping bool // Ignore user's custom import grouping
}

// Process formats and adjusts imports for the provided file.
Expand Down Expand Up @@ -68,7 +70,7 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) {
}
}

sortImports(fileSet, file)
sortImports(fileSet, file, opt.IgnoreGrouping)
imps := astutil.Imports(fileSet, file)
var spacesBefore []string // import paths we need spaces before
for _, impSection := range imps {
Expand All @@ -85,7 +87,6 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) {
}
lastGroup = groupNum
}

}

printerMode := printer.UseSpaces
Expand Down
14 changes: 8 additions & 6 deletions imports/sortimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// sortImports sorts runs of consecutive import lines in import blocks in f.
// It also removes duplicate imports when it is possible to do so without data loss.
func sortImports(fset *token.FileSet, f *ast.File) {
func sortImports(fset *token.FileSet, f *ast.File, ignoreGroups bool) {
for i, d := range f.Decls {
d, ok := d.(*ast.GenDecl)
if !ok || d.Tok != token.IMPORT {
Expand All @@ -37,11 +37,13 @@ func sortImports(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(fset, f, d.Specs[i:j])...)
i = j
if !ignoreGroups {
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(fset, f, d.Specs[i:j])...)
i = j
}
}
}
specs = append(specs, sortSpecs(fset, f, d.Specs[i:])...)
Expand Down

0 comments on commit fcc3616

Please sign in to comment.