From 8707409958e964057de3508f7541e772d2363064 Mon Sep 17 00:00:00 2001 From: Loong Date: Tue, 30 Aug 2022 00:21:38 +0800 Subject: [PATCH 1/2] support multiple import blocks Signed-off-by: Loong --- Makefile | 2 +- pkg/gci/gci.go | 16 +++++++++++++++- .../internal/testdata/multiple-imports.cfg.yaml | 4 ++++ pkg/gci/internal/testdata/multiple-imports.in.go | 11 +++++++++++ .../internal/testdata/multiple-imports.out.go | 9 +++++++++ .../one-line-import-after-import.cfg.yaml | 4 ++++ .../testdata/one-line-import-after-import.in.go | 10 ++++++++++ .../testdata/one-line-import-after-import.out.go | 9 +++++++++ 8 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 pkg/gci/internal/testdata/multiple-imports.cfg.yaml create mode 100644 pkg/gci/internal/testdata/multiple-imports.in.go create mode 100644 pkg/gci/internal/testdata/multiple-imports.out.go create mode 100644 pkg/gci/internal/testdata/one-line-import-after-import.cfg.yaml create mode 100644 pkg/gci/internal/testdata/one-line-import-after-import.in.go create mode 100644 pkg/gci/internal/testdata/one-line-import-after-import.out.go diff --git a/Makefile b/Makefile index e69b4dc..4e75d57 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ build: clean @go build -v -trimpath -o ${BIN_OUTPUT} . test: clean - @go test -v -cover ./... + @go test -v -count=1 -cover ./... generate: @go generate ./... diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index c39de67..37c9ee1 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -138,8 +138,22 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err return nil, nil, err } - head := src[:headEnd] + var head []byte + if src[headEnd-1] == '\t' { + head = src[:headEnd] + } else { + // handle multiple import blocks + // cover `import ` to `import (` + head = make([]byte, headEnd) + copy(head, src[:headEnd]) + head = append(head, []byte{40, 10, 9}...) + } + tail := src[tailStart:] + // for test + if len(tail) == 0 { + tail = []byte(")\n") + } firstWithIndex := true diff --git a/pkg/gci/internal/testdata/multiple-imports.cfg.yaml b/pkg/gci/internal/testdata/multiple-imports.cfg.yaml new file mode 100644 index 0000000..e666ab9 --- /dev/null +++ b/pkg/gci/internal/testdata/multiple-imports.cfg.yaml @@ -0,0 +1,4 @@ +sections: + - Standard + - Default + - Prefix(github.com/daixiang0) diff --git a/pkg/gci/internal/testdata/multiple-imports.in.go b/pkg/gci/internal/testdata/multiple-imports.in.go new file mode 100644 index 0000000..a4ea568 --- /dev/null +++ b/pkg/gci/internal/testdata/multiple-imports.in.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +import "context" + +import ( + "os" + + "github.com/daixiang0/test" +) diff --git a/pkg/gci/internal/testdata/multiple-imports.out.go b/pkg/gci/internal/testdata/multiple-imports.out.go new file mode 100644 index 0000000..9463369 --- /dev/null +++ b/pkg/gci/internal/testdata/multiple-imports.out.go @@ -0,0 +1,9 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/daixiang0/test" +) diff --git a/pkg/gci/internal/testdata/one-line-import-after-import.cfg.yaml b/pkg/gci/internal/testdata/one-line-import-after-import.cfg.yaml new file mode 100644 index 0000000..e666ab9 --- /dev/null +++ b/pkg/gci/internal/testdata/one-line-import-after-import.cfg.yaml @@ -0,0 +1,4 @@ +sections: + - Standard + - Default + - Prefix(github.com/daixiang0) diff --git a/pkg/gci/internal/testdata/one-line-import-after-import.in.go b/pkg/gci/internal/testdata/one-line-import-after-import.in.go new file mode 100644 index 0000000..6de854d --- /dev/null +++ b/pkg/gci/internal/testdata/one-line-import-after-import.in.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "os" + + "github.com/daixiang0/test" +) + +import "context" diff --git a/pkg/gci/internal/testdata/one-line-import-after-import.out.go b/pkg/gci/internal/testdata/one-line-import-after-import.out.go new file mode 100644 index 0000000..9463369 --- /dev/null +++ b/pkg/gci/internal/testdata/one-line-import-after-import.out.go @@ -0,0 +1,9 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/daixiang0/test" +) From fd220c5a6639bf1b0b3f785aca5a39a39ca6c463 Mon Sep 17 00:00:00 2001 From: Loong Date: Tue, 30 Aug 2022 00:22:17 +0800 Subject: [PATCH 2/2] bump up version Signed-off-by: Loong --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 8ebe5a1..a733912 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( "github.com/daixiang0/gci/cmd/gci" ) -var version = "0.6.3" +var version = "0.7.0" func main() { e := gci.NewExecutor(version)