From 91afca94e07aa1366afba9ece04c2def9f99d4c4 Mon Sep 17 00:00:00 2001 From: closs Date: Sat, 15 Jul 2017 16:07:30 -0600 Subject: [PATCH] ast: make ExampleCommentMap a runnable example Fixes #20450 Change-Id: I2256282a8880e99508e98fefedfb94a7cccacbcf Reviewed-on: https://go-review.googlesource.com/48969 Run-TryBot: Bryan Mills TryBot-Result: Gobot Gobot Reviewed-by: Bryan Mills --- src/go/ast/example_test.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/go/ast/example_test.go b/src/go/ast/example_test.go index d2e734f2cb..52a77981b8 100644 --- a/src/go/ast/example_test.go +++ b/src/go/ast/example_test.go @@ -44,7 +44,7 @@ var X = f(3.14)*2 + c return true }) - // output: + // Output: // src.go:2:9: p // src.go:3:7: c // src.go:3:11: 1.0 @@ -75,7 +75,7 @@ func main() { // Print the AST. ast.Print(fset, f) - // output: + // Output: // 0 *ast.File { // 1 . Package: 2:1 // 2 . Name: *ast.Ident { @@ -172,7 +172,12 @@ func main() { cmap := ast.NewCommentMap(fset, f, f.Comments) // Remove the first variable declaration from the list of declarations. - f.Decls = removeFirstVarDecl(f.Decls) + for i, decl := range f.Decls { + if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.VAR { + copy(f.Decls[i:], f.Decls[i+1:]) + f.Decls = f.Decls[:len(f.Decls)-1] + } + } // Use the comment map to filter comments that don't belong anymore // (the comments associated with the variable declaration), and create @@ -186,7 +191,7 @@ func main() { } fmt.Printf("%s", buf.Bytes()) - // output: + // Output: // // This is the package comment. // package main // @@ -198,13 +203,3 @@ func main() { // fmt.Println(hello) // line comment 3 // } } - -func removeFirstVarDecl(list []ast.Decl) []ast.Decl { - for i, decl := range list { - if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.VAR { - copy(list[i:], list[i+1:]) - return list[:len(list)-1] - } - } - panic("variable declaration not found") -} -- 2.48.1