Skip to content

Commit 5543977

Browse files
committed
Insert struct and function before main function for completion logic
1 parent 536a7d3 commit 5543977

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

session.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os"
1616
"os/exec"
1717
"path/filepath"
18+
"slices"
1819
"strings"
1920
"unicode"
2021

@@ -255,7 +256,12 @@ func (s *Session) evalStmt(in string) error {
255256
case *ast.DeclStmt:
256257
if decl, ok := stmt.Decl.(*ast.GenDecl); ok {
257258
if decl.Tok == token.TYPE {
258-
s.file.Decls = append(s.file.Decls, decl)
259+
for i, d := range s.file.Decls {
260+
if d, ok := d.(*ast.FuncDecl); ok && d.Name.String() == "main" {
261+
s.file.Decls = slices.Insert(s.file.Decls, i, stmt.Decl)
262+
break
263+
}
264+
}
259265
continue
260266
} else if stmt := buildPrintStmtOfDecl(decl); stmt != nil {
261267
stmts = append(stmts, stmt)
@@ -317,17 +323,23 @@ func (s *Session) evalFunc(in string) error {
317323
if len(f.Decls) != 1 {
318324
return errors.New("eval func error")
319325
}
320-
newDecl, ok := f.Decls[0].(*ast.FuncDecl)
321-
if !ok {
326+
decl, name := f.Decls[0], ""
327+
if d, ok := decl.(*ast.FuncDecl); !ok {
322328
return errors.New("eval func error")
329+
} else {
330+
name = d.Name.String()
323331
}
324332
for i, d := range s.file.Decls {
325-
if d, ok := d.(*ast.FuncDecl); ok && d.Name.String() == newDecl.Name.String() {
326-
s.file.Decls = append(s.file.Decls[:i], s.file.Decls[i+1:]...)
327-
break
333+
if d, ok := d.(*ast.FuncDecl); ok {
334+
if d.Name.String() == name {
335+
s.file.Decls[i] = decl
336+
break
337+
} else if d.Name.String() == "main" {
338+
s.file.Decls = slices.Insert(s.file.Decls, i, decl)
339+
break
340+
}
328341
}
329342
}
330-
s.file.Decls = append(s.file.Decls, newDecl)
331343
return nil
332344
}
333345

@@ -502,10 +514,7 @@ func (s *Session) invokeCommand(in string) (err error) {
502514
// storeCode stores current state of code so that it can be restored
503515
func (s *Session) storeCode() {
504516
s.lastStmts = s.mainBody.List
505-
if len(s.lastDecls) != len(s.file.Decls) {
506-
s.lastDecls = make([]ast.Decl, len(s.file.Decls))
507-
}
508-
copy(s.lastDecls, s.file.Decls)
517+
s.lastDecls = slices.Clone(s.file.Decls)
509518
}
510519

511520
// restoreCode restores the previous code

session_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@ func TestSessionEval_Func(t *testing.T) {
382382

383383
assert.Equal(t, "112\n2400\n204\n", stdout.String())
384384
assert.Equal(t, `cannot use s (variable of type string) as int value in return statement
385+
cannot use i (variable of type int) as string value in return statement
385386
invalid operation: f() + len(g()) (mismatched types string and int)
386387
invalid operation: f() * len(g()) (mismatched types string and int)
387-
cannot use i (variable of type int) as string value in return statement
388388
`, stderr.String())
389389
}
390390

0 commit comments

Comments
 (0)