Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize detect import blocks #120

Merged
merged 1 commit into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 16 additions & 17 deletions pkg/gci/gci.go
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/gci/gci_test.go
Expand Up @@ -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")
Expand Down
1 change: 0 additions & 1 deletion pkg/gci/internal/testdata/drop-prefix-comments.cfg.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/gci/internal/testdata/drop-prefix-comments.in.go

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/gci/internal/testdata/drop-prefix-comments.out.go

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/gci/internal/testdata/multiple-imports.cfg.yaml

This file was deleted.

1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/multiple-imports.cfg.yaml
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/multiple-imports.in.go
Expand Up @@ -9,3 +9,9 @@ import (

"github.com/daixiang0/test"
)

import "math"


func main() {
}
4 changes: 4 additions & 0 deletions pkg/gci/internal/testdata/multiple-imports.out.go
Expand Up @@ -3,7 +3,11 @@ package main
import (
"context"
"fmt"
"math"
"os"

"github.com/daixiang0/test"
)

func main() {
}
50 changes: 40 additions & 10 deletions pkg/parse/parse.go
Expand Up @@ -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,
Expand Down