diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 9186157..1bcdef2 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -139,20 +139,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err return nil, nil, err } - var head []byte - // use `import` as key to locate - importEnd := bytes.LastIndex(src[:headEnd], []byte{'t'}) - head = make([]byte, importEnd+1) - copy(head, src[:importEnd+1]) - // append `(\n\t` - head = append(head, []byte{utils.LeftParenthesis, utils.Linebreak, utils.Indent}...) - log.L().Debug(fmt.Sprintf("head: %s", head)) - tail := src[tailStart:] - // for test - if len(tail) == 0 { - tail = []byte{utils.RightParenthesis, utils.Linebreak} - } - firstWithIndex := true var body []byte @@ -170,10 +156,22 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err } } - if tail[0] != utils.Linebreak { - body = append(body, utils.Linebreak) + head := src[:headEnd] + tail := src[tailStart:] + + head = append(head, utils.Linebreak) + // add beginning of import block + head = append(head, `import (`...) + // add end of import block + body = append(body, []byte{utils.RightParenthesis, utils.Linebreak}...) + + log.L().Debug(fmt.Sprintf("head:\n%s", head)) + log.L().Debug(fmt.Sprintf("body:\n%s", body)) + if len(tail) > 20 { + log.L().Debug(fmt.Sprintf("tail:\n%s", tail[:20])) + } else { + log.L().Debug(fmt.Sprintf("tail:\n%s", tail)) } - log.L().Debug(fmt.Sprintf("body: %s", body)) var totalLen int slices := [][]byte{head, body, tail} @@ -185,6 +183,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err for _, s := range slices { i += copy(dist[i:], s) } + log.L().Debug(fmt.Sprintf("raw:\n%s", dist)) dist, err = goFormat.Source(dist) if err != nil { return nil, nil, err diff --git a/pkg/gci/gci_test.go b/pkg/gci/gci_test.go index e85c44b..3d89aa0 100644 --- a/pkg/gci/gci_test.go +++ b/pkg/gci/gci_test.go @@ -36,7 +36,7 @@ func TestRun(t *testing.T) { } for _, testFile := range testFiles { fileBaseName := strings.TrimSuffix(testFile, ".in.go") - t.Run(fileBaseName, func(t *testing.T) { + t.Run("pkg/gci/"+testFile, func(t *testing.T) { t.Parallel() gciCfg, err := config.InitializeGciConfigFromYAML(fileBaseName + ".cfg.yaml") diff --git a/pkg/gci/internal/testdata/drop-prefix-comments.cfg.yaml b/pkg/gci/internal/testdata/drop-prefix-comments.cfg.yaml deleted file mode 120000 index b0db9e7..0000000 --- a/pkg/gci/internal/testdata/drop-prefix-comments.cfg.yaml +++ /dev/null @@ -1 +0,0 @@ -common.cfg.yaml \ No newline at end of file diff --git a/pkg/gci/internal/testdata/drop-prefix-comments.in.go b/pkg/gci/internal/testdata/drop-prefix-comments.in.go deleted file mode 100644 index fbd1c05..0000000 --- a/pkg/gci/internal/testdata/drop-prefix-comments.in.go +++ /dev/null @@ -1,16 +0,0 @@ -package proc - -import ( - // Std imports - "os" - "context" // is required - "fmt" - // Std imports - - // -------------------------- - - // Github - "github.com/local/dlib/dexec" - "github.com/daixiang0/gci" - // Github -) diff --git a/pkg/gci/internal/testdata/drop-prefix-comments.out.go b/pkg/gci/internal/testdata/drop-prefix-comments.out.go deleted file mode 100644 index 223f546..0000000 --- a/pkg/gci/internal/testdata/drop-prefix-comments.out.go +++ /dev/null @@ -1,14 +0,0 @@ -package proc - -import ( - "context" // is required - "fmt" - // Std imports - "os" - - // Github - "github.com/local/dlib/dexec" - - "github.com/daixiang0/gci" - // Github -) diff --git a/pkg/gci/internal/testdata/multiple-imports.cfg.yaml b/pkg/gci/internal/testdata/multiple-imports.cfg.yaml deleted file mode 100644 index e666ab9..0000000 --- a/pkg/gci/internal/testdata/multiple-imports.cfg.yaml +++ /dev/null @@ -1,4 +0,0 @@ -sections: - - Standard - - Default - - Prefix(github.com/daixiang0) diff --git a/pkg/gci/internal/testdata/multiple-imports.cfg.yaml b/pkg/gci/internal/testdata/multiple-imports.cfg.yaml new file mode 120000 index 0000000..b0db9e7 --- /dev/null +++ b/pkg/gci/internal/testdata/multiple-imports.cfg.yaml @@ -0,0 +1 @@ +common.cfg.yaml \ No newline at end of file diff --git a/pkg/gci/internal/testdata/multiple-imports.in.go b/pkg/gci/internal/testdata/multiple-imports.in.go index a4ea568..d0143ca 100644 --- a/pkg/gci/internal/testdata/multiple-imports.in.go +++ b/pkg/gci/internal/testdata/multiple-imports.in.go @@ -9,3 +9,9 @@ import ( "github.com/daixiang0/test" ) + +import "math" + + +func main() { +} diff --git a/pkg/gci/internal/testdata/multiple-imports.out.go b/pkg/gci/internal/testdata/multiple-imports.out.go index 9463369..da6a5c9 100644 --- a/pkg/gci/internal/testdata/multiple-imports.out.go +++ b/pkg/gci/internal/testdata/multiple-imports.out.go @@ -3,7 +3,11 @@ package main import ( "context" "fmt" + "math" "os" "github.com/daixiang0/test" ) + +func main() { +} diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go index df4cfdc..7540d07 100644 --- a/pkg/parse/parse.go +++ b/pkg/parse/parse.go @@ -81,23 +81,53 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, error) { return nil, 0, 0, NoImportError{} } - var headEnd, tailStart int + var ( + // headEnd means the start of import block + headEnd int + // tailStart means the end + 1 of import block + tailStart int + // lastImportStart means the start of last import block + lastImportStart int + data ImportList + ) - var data ImportList - for i, imp := range f.Imports { + for i, d := range f.Decls { + switch d.(type) { + case *ast.GenDecl: + dd := d.(*ast.GenDecl) + if dd.Tok == token.IMPORT { + // there are two cases, both end with linebreak: + // 1. + // import ( + // "xxxx" + // ) + // 2. + // import "xxx" + if headEnd == 0 { + headEnd = int(d.Pos()) - 1 + } + tailStart = int(d.End()) + lastImportStart = i + } + } + } + + if len(f.Decls) > lastImportStart+1 { + tailStart = int(f.Decls[lastImportStart+1].Pos() - 1) + } + + for _, imp := range f.Imports { if imp.Path.Value == C { + if imp.Comment != nil { + headEnd = int(imp.Comment.End()) + } else { + headEnd = int(imp.Path.End()) + } continue } start, end, name := getImports(imp) - if headEnd == 0 { - headEnd = start - } - if i == len(f.Imports)-1 { - tailStart = end - } - data = append(data, &GciImports{ Start: start, End: end,