Skip to content

Commit

Permalink
Support multiple import blocks (#106)
Browse files Browse the repository at this point in the history
* support multiple import blocks

Signed-off-by: Loong <loong.dai@intel.com>

* bump up version

Signed-off-by: Loong <loong.dai@intel.com>
  • Loading branch information
daixiang0 committed Aug 29, 2022
1 parent 256e8b5 commit 4606d17
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -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 ./...
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion pkg/gci/gci.go
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions pkg/gci/internal/testdata/multiple-imports.cfg.yaml
@@ -0,0 +1,4 @@
sections:
- Standard
- Default
- Prefix(github.com/daixiang0)
11 changes: 11 additions & 0 deletions pkg/gci/internal/testdata/multiple-imports.in.go
@@ -0,0 +1,11 @@
package main

import "fmt"

import "context"

import (
"os"

"github.com/daixiang0/test"
)
9 changes: 9 additions & 0 deletions pkg/gci/internal/testdata/multiple-imports.out.go
@@ -0,0 +1,9 @@
package main

import (
"context"
"fmt"
"os"

"github.com/daixiang0/test"
)
@@ -0,0 +1,4 @@
sections:
- Standard
- Default
- Prefix(github.com/daixiang0)
10 changes: 10 additions & 0 deletions 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"
9 changes: 9 additions & 0 deletions 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"
)

0 comments on commit 4606d17

Please sign in to comment.