Skip to content

Try to extend goimports #68

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -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