From fdd7879facd7452685be6764cf85cd1bc5c89d70 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 21 Jul 2021 19:30:43 +0800 Subject: [PATCH] SetComments: commentOnce or note --- codebuild.go | 13 ++++++++----- package_test.go | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/codebuild.go b/codebuild.go index cfb4fab9..50b612e9 100644 --- a/codebuild.go +++ b/codebuild.go @@ -101,6 +101,7 @@ type CodeBuilder struct { pkg *Package varDecl *ValueDecl closureParamInsts + commentOnce bool } func (p *CodeBuilder) init(pkg *Package) { @@ -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 @@ -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 } diff --git a/package_test.go b/package_test.go index 325296ee..c63d7de3 100644 --- a/package_test.go +++ b/package_test.go @@ -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