Skip to content

Commit

Permalink
SetComments: commentOnce or note
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 21, 2021
1 parent a35a2a4 commit fdd7879
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 8 additions & 5 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type CodeBuilder struct {
pkg *Package
varDecl *ValueDecl
closureParamInsts
commentOnce bool
}

func (p *CodeBuilder) init(pkg *Package) {
Expand Down Expand Up @@ -198,9 +199,11 @@ func (p *CodeBuilder) commitStmt(idx int) {
}

func (p *CodeBuilder) emitStmt(stmt ast.Stmt) {
if comments := p.comments; comments != nil {
p.comments = nil
stmt = &printer.CommentedStmt{Comments: comments, Stmt: stmt}
if p.comments != nil {
stmt = &printer.CommentedStmt{Comments: p.comments, Stmt: stmt}
if p.commentOnce {
p.comments = nil
}
}
if p.current.label != nil {
p.current.label.Stmt = stmt
Expand All @@ -224,13 +227,13 @@ func (p *CodeBuilder) Comments() *ast.CommentGroup {
}

// SetComments sets current comments.
func (p *CodeBuilder) SetComments(comments *ast.CommentGroup) *CodeBuilder {
func (p *CodeBuilder) SetComments(comments *ast.CommentGroup, once bool) *CodeBuilder {
if debugComments && comments != nil {
for i, c := range comments.List {
log.Println("SetComments", i, c.Text)
}
}
p.comments = comments
p.comments, p.commentOnce = comments, once
return p
}

Expand Down
6 changes: 3 additions & 3 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ func TestIncDec(t *testing.T) {
pkg := newMainPackage()
tyInt := types.Typ[types.Uint]
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
SetComments(comment("\n// define variable a")).
SetComments(comment("\n// define variable a"), false).
NewVar(tyInt, "a").
SetComments(comment("\n// inc a")).
SetComments(comment("\n// inc a"), true).
VarRef(ctxRef(pkg, "a")).IncDec(token.INC).EndStmt().
End()
if pkg.CB().Comments() != nil {
t.Fatal("please clear Comments")
t.Fatal("comments is not nil")
}
domTest(t, pkg, `package main
Expand Down

0 comments on commit fdd7879

Please sign in to comment.