From cf513387c3839e0815016ec8d9b4cf0cd1802aae Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Tue, 2 Oct 2012 08:35:20 +1000 Subject: [PATCH] go/doc: strip example output comment from synthesized main function R=gri CC=gobot, golang-dev https://golang.org/cl/6524047 --- src/pkg/go/doc/example.go | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/pkg/go/doc/example.go b/src/pkg/go/doc/example.go index 8fcee33af0..581471ae24 100644 --- a/src/pkg/go/doc/example.go +++ b/src/pkg/go/doc/example.go @@ -61,7 +61,7 @@ func Examples(files ...*ast.File) []*Example { Code: f.Body, Play: playExample(file, f.Body), Comments: file.Comments, - Output: exampleOutput(f, file.Comments), + Output: exampleOutput(f.Body, file.Comments), }) } if !hasTests && numDecl > 1 && len(flist) == 1 { @@ -78,14 +78,14 @@ func Examples(files ...*ast.File) []*Example { var outputPrefix = regexp.MustCompile(`(?i)^[[:space:]]*output:`) -func exampleOutput(fun *ast.FuncDecl, comments []*ast.CommentGroup) string { +func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) string { // find the last comment in the function var last *ast.CommentGroup for _, cg := range comments { - if cg.Pos() < fun.Pos() { + if cg.Pos() < b.Pos() { continue } - if cg.End() > fun.End() { + if cg.End() > b.End() { break } last = cg @@ -163,8 +163,6 @@ func playExample(file *ast.File, body *ast.BlockStmt) *ast.File { } } - // TODO(adg): look for other unresolved identifiers and, if found, give up. - // Synthesize new imports. importDecl := &ast.GenDecl{ Tok: token.IMPORT, @@ -179,12 +177,7 @@ func playExample(file *ast.File, body *ast.BlockStmt) *ast.File { importDecl.Specs = append(importDecl.Specs, s) } - // Synthesize main function. - funcDecl := &ast.FuncDecl{ - Name: ast.NewIdent("main"), - Type: &ast.FuncType{}, - Body: body, - } + // TODO(adg): look for other unresolved identifiers and, if found, give up. // Filter out comments that are outside the function body. var comments []*ast.CommentGroup @@ -195,6 +188,27 @@ func playExample(file *ast.File, body *ast.BlockStmt) *ast.File { comments = append(comments, c) } + // Strip "Output:" commment and adjust body end position. + if len(comments) > 0 { + last := comments[len(comments)-1] + if outputPrefix.MatchString(last.Text()) { + comments = comments[:len(comments)-1] + // Copy body, as the original may be used elsewhere. + body = &ast.BlockStmt{ + Lbrace: body.Pos(), + List: body.List, + Rbrace: last.Pos(), + } + } + } + + // Synthesize main function. + funcDecl := &ast.FuncDecl{ + Name: ast.NewIdent("main"), + Type: &ast.FuncType{}, + Body: body, + } + // Synthesize file. f := &ast.File{ Name: ast.NewIdent("main"), -- 2.48.1