Skip to content

Commit a57858c

Browse files
committed
x/format: GopClass update imports
1 parent 117a0eb commit a57858c

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

x/format/gopclass_test.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ func testClass(t *testing.T, name string, pkg string, class string, entry string
3737
func TestClassSpx(t *testing.T) {
3838
testClass(t, "spx class", "github.com/goplus/spx", "Calf", "Main", `package main
3939
40+
import (
41+
"github.com/goplus/spx"
42+
"fmt"
43+
"log"
44+
)
45+
4046
type Calf struct {
4147
spx.Sprite
4248
*Game
@@ -46,6 +52,9 @@ type Calf struct {
4652
func (this *Calf) Update() {
4753
this.index++
4854
}
55+
func (this *Calf) Dump() {
56+
log.Println(this.info)
57+
}
4958
func (this *Calf) Main() {
5059
this.OnStart(func() {
5160
this.Say("Hello Go+")
@@ -54,7 +63,11 @@ func (this *Calf) Main() {
5463
func (this *Calf) Classfname() string {
5564
return "Calf"
5665
}
57-
`, `var (
66+
`, `import (
67+
"log"
68+
)
69+
70+
var (
5871
index int
5972
info string
6073
)
@@ -63,8 +76,13 @@ func Update() {
6376
index++
6477
}
6578
79+
func Dump() {
80+
log.println info
81+
}
82+
6683
onStart func() {
6784
say "Hello Go+"
6885
}
6986
`)
87+
7088
}

x/format/gopstyle.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
249249
}
250250
var fnEntry *ast.FuncDecl
251251
var decls []ast.Decl
252+
var imports []ast.Decl
252253
var varSpecs []ast.Spec
253254
for _, decl := range file.Decls {
254255
switch v := decl.(type) {
@@ -265,7 +266,11 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
265266
}
266267
}
267268
case *ast.GenDecl:
268-
if v.Tok == token.TYPE {
269+
switch v.Tok {
270+
case token.IMPORT:
271+
imports = append(imports, v)
272+
continue
273+
case token.TYPE:
269274
if spec, ok := v.Specs[0].(*ast.TypeSpec); ok && spec.Name.Name == class {
270275
if st, ok := spec.Type.(*ast.StructType); ok {
271276
for _, fs := range st.Fields.List {
@@ -281,13 +286,15 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
281286
}
282287
decls = append(decls, decl)
283288
}
289+
290+
file.Decls = imports
284291
if len(varSpecs) != 0 {
285-
decls = append([]ast.Decl{&ast.GenDecl{Tok: token.VAR, Specs: varSpecs}}, decls...)
292+
file.Decls = append(file.Decls, &ast.GenDecl{Tok: token.VAR, Specs: varSpecs})
286293
}
294+
file.Decls = append(file.Decls, decls...)
287295
if fnEntry != nil {
288-
decls = append(decls, fnEntry)
296+
file.Decls = append(file.Decls, fnEntry)
289297
}
290-
file.Decls = decls
291298

292299
for _, decl := range file.Decls {
293300
switch v := decl.(type) {
@@ -321,7 +328,7 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
321328
formatFuncDecl(ctx, fn)
322329
}
323330
for _, imp := range ctx.imports {
324-
if imp.pkgPath == "fmt" && !imp.isUsed {
331+
if !imp.isUsed {
325332
if len(imp.decl.Specs) == 1 {
326333
file.Decls = deleteDecl(file.Decls, imp.decl)
327334
} else {

0 commit comments

Comments
 (0)