Skip to content

Commit fcc3616

Browse files
committed
add possibility to ignore custom user's import grouping
1 parent d00ac6d commit fcc3616

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

cmd/goimports/goimports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
func init() {
4949
flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)")
5050
flag.StringVar(&imports.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
51+
flag.BoolVar(&options.IgnoreGrouping, "ungroup", false, "ignore user's custom import grouping")
5152
}
5253

5354
func report(err error) {

imports/imports.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type Options struct {
3636
TabWidth int // Tab width (8 if nil *Options provided)
3737

3838
FormatOnly bool // Disable the insertion and deletion of imports
39+
40+
IgnoreGrouping bool // Ignore user's custom import grouping
3941
}
4042

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

71-
sortImports(fileSet, file)
73+
sortImports(fileSet, file, opt.IgnoreGrouping)
7274
imps := astutil.Imports(fileSet, file)
7375
var spacesBefore []string // import paths we need spaces before
7476
for _, impSection := range imps {
@@ -85,7 +87,6 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) {
8587
}
8688
lastGroup = groupNum
8789
}
88-
8990
}
9091

9192
printerMode := printer.UseSpaces

imports/sortimports.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// sortImports sorts runs of consecutive import lines in import blocks in f.
1717
// It also removes duplicate imports when it is possible to do so without data loss.
18-
func sortImports(fset *token.FileSet, f *ast.File) {
18+
func sortImports(fset *token.FileSet, f *ast.File, ignoreGroups bool) {
1919
for i, d := range f.Decls {
2020
d, ok := d.(*ast.GenDecl)
2121
if !ok || d.Tok != token.IMPORT {
@@ -37,11 +37,13 @@ func sortImports(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(fset, f, d.Specs[i:j])...)
44-
i = j
40+
if !ignoreGroups {
41+
for j, s := range d.Specs {
42+
if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
43+
// j begins a new run. End this one.
44+
specs = append(specs, sortSpecs(fset, f, d.Specs[i:j])...)
45+
i = j
46+
}
4547
}
4648
}
4749
specs = append(specs, sortSpecs(fset, f, d.Specs[i:])...)

0 commit comments

Comments
 (0)