Repository: daixiang0/gci
Pull request #106 (2022-08-29 08:27:29)

Support multiple import blocks


Patch:
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/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)
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
 


Test patch:
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"
+)