Skip to content

Commit 85f4d54

Browse files
authored
optimize detect import blocks (#120)
Signed-off-by: Loong <[email protected]>
1 parent 4f0de64 commit 85f4d54

9 files changed

+68
-63
lines changed

pkg/gci/gci.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
139139
return nil, nil, err
140140
}
141141

142-
var head []byte
143-
// use `import` as key to locate
144-
importEnd := bytes.LastIndex(src[:headEnd], []byte{'t'})
145-
head = make([]byte, importEnd+1)
146-
copy(head, src[:importEnd+1])
147-
// append `(\n\t`
148-
head = append(head, []byte{utils.LeftParenthesis, utils.Linebreak, utils.Indent}...)
149-
log.L().Debug(fmt.Sprintf("head: %s", head))
150-
tail := src[tailStart:]
151-
// for test
152-
if len(tail) == 0 {
153-
tail = []byte{utils.RightParenthesis, utils.Linebreak}
154-
}
155-
156142
firstWithIndex := true
157143

158144
var body []byte
@@ -170,10 +156,22 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
170156
}
171157
}
172158

173-
if tail[0] != utils.Linebreak {
174-
body = append(body, utils.Linebreak)
159+
head := src[:headEnd]
160+
tail := src[tailStart:]
161+
162+
head = append(head, utils.Linebreak)
163+
// add beginning of import block
164+
head = append(head, `import (`...)
165+
// add end of import block
166+
body = append(body, []byte{utils.RightParenthesis, utils.Linebreak}...)
167+
168+
log.L().Debug(fmt.Sprintf("head:\n%s", head))
169+
log.L().Debug(fmt.Sprintf("body:\n%s", body))
170+
if len(tail) > 20 {
171+
log.L().Debug(fmt.Sprintf("tail:\n%s", tail[:20]))
172+
} else {
173+
log.L().Debug(fmt.Sprintf("tail:\n%s", tail))
175174
}
176-
log.L().Debug(fmt.Sprintf("body: %s", body))
177175

178176
var totalLen int
179177
slices := [][]byte{head, body, tail}
@@ -185,6 +183,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
185183
for _, s := range slices {
186184
i += copy(dist[i:], s)
187185
}
186+
log.L().Debug(fmt.Sprintf("raw:\n%s", dist))
188187
dist, err = goFormat.Source(dist)
189188
if err != nil {
190189
return nil, nil, err

pkg/gci/gci_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestRun(t *testing.T) {
3636
}
3737
for _, testFile := range testFiles {
3838
fileBaseName := strings.TrimSuffix(testFile, ".in.go")
39-
t.Run(fileBaseName, func(t *testing.T) {
39+
t.Run("pkg/gci/"+testFile, func(t *testing.T) {
4040
t.Parallel()
4141

4242
gciCfg, err := config.InitializeGciConfigFromYAML(fileBaseName + ".cfg.yaml")

pkg/gci/internal/testdata/drop-prefix-comments.cfg.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/gci/internal/testdata/drop-prefix-comments.in.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

pkg/gci/internal/testdata/drop-prefix-comments.out.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

pkg/gci/internal/testdata/multiple-imports.cfg.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
common.cfg.yaml

pkg/gci/internal/testdata/multiple-imports.in.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ import (
99

1010
"github.com/daixiang0/test"
1111
)
12+
13+
import "math"
14+
15+
16+
func main() {
17+
}

pkg/gci/internal/testdata/multiple-imports.out.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"math"
67
"os"
78

89
"github.com/daixiang0/test"
910
)
11+
12+
func main() {
13+
}

pkg/parse/parse.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,53 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
8181
return nil, 0, 0, NoImportError{}
8282
}
8383

84-
var headEnd, tailStart int
84+
var (
85+
// headEnd means the start of import block
86+
headEnd int
87+
// tailStart means the end + 1 of import block
88+
tailStart int
89+
// lastImportStart means the start of last import block
90+
lastImportStart int
91+
data ImportList
92+
)
8593

86-
var data ImportList
87-
for i, imp := range f.Imports {
94+
for i, d := range f.Decls {
95+
switch d.(type) {
96+
case *ast.GenDecl:
97+
dd := d.(*ast.GenDecl)
98+
if dd.Tok == token.IMPORT {
99+
// there are two cases, both end with linebreak:
100+
// 1.
101+
// import (
102+
// "xxxx"
103+
// )
104+
// 2.
105+
// import "xxx"
106+
if headEnd == 0 {
107+
headEnd = int(d.Pos()) - 1
108+
}
109+
tailStart = int(d.End())
110+
lastImportStart = i
111+
}
112+
}
113+
}
114+
115+
if len(f.Decls) > lastImportStart+1 {
116+
tailStart = int(f.Decls[lastImportStart+1].Pos() - 1)
117+
}
118+
119+
for _, imp := range f.Imports {
88120
if imp.Path.Value == C {
121+
if imp.Comment != nil {
122+
headEnd = int(imp.Comment.End())
123+
} else {
124+
headEnd = int(imp.Path.End())
125+
}
89126
continue
90127
}
91128

92129
start, end, name := getImports(imp)
93130

94-
if headEnd == 0 {
95-
headEnd = start
96-
}
97-
if i == len(f.Imports)-1 {
98-
tailStart = end
99-
}
100-
101131
data = append(data, &GciImports{
102132
Start: start,
103133
End: end,

0 commit comments

Comments
 (0)