From e4933889654a3d0cd7f3ab2ae7511ce0199b4994 Mon Sep 17 00:00:00 2001 From: Hiroshi Ioka Date: Fri, 9 Jun 2017 10:48:25 +0900 Subject: [PATCH] x/tools/cmd/bundle: include comments inside functions Fixes golang/go#20548 Change-Id: Ieff2323b63308cbc052a2883237520620965cf86 Reviewed-on: https://go-review.googlesource.com/45117 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- cmd/bundle/.gitignore | 1 + cmd/bundle/main.go | 8 ++------ cmd/bundle/testdata/out.golden | 4 +++- cmd/bundle/testdata/src/initial/a.go | 4 +++- 4 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 cmd/bundle/.gitignore diff --git a/cmd/bundle/.gitignore b/cmd/bundle/.gitignore new file mode 100644 index 00000000..caaeb09d --- /dev/null +++ b/cmd/bundle/.gitignore @@ -0,0 +1 @@ +testdata/out.got diff --git a/cmd/bundle/main.go b/cmd/bundle/main.go index e4b261fa..02bd8357 100644 --- a/cmd/bundle/main.go +++ b/cmd/bundle/main.go @@ -88,6 +88,7 @@ import ( "go/build" "go/format" "go/parser" + "go/printer" "go/token" "go/types" "io/ioutil" @@ -348,18 +349,13 @@ func bundle(src, dst, dstpkg, prefix string) ([]byte, error) { // Pretty-print package-level declarations. // but no package or import declarations. - // - // TODO(adonovan): this may cause loss of comments - // preceding or associated with the package or import - // declarations or not associated with any declaration. - // Check. var buf bytes.Buffer for _, decl := range f.Decls { if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT { continue } buf.Reset() - format.Node(&buf, lprog.Fset, decl) + format.Node(&buf, lprog.Fset, &printer.CommentedNode{Node: decl, Comments: f.Comments}) // Remove each "@@@." in the output. // TODO(adonovan): not hygienic. out.Write(bytes.Replace(buf.Bytes(), []byte("@@@."), nil, -1)) diff --git a/cmd/bundle/testdata/out.golden b/cmd/bundle/testdata/out.golden index 9374b2e9..b76c5ae7 100644 --- a/cmd/bundle/testdata/out.golden +++ b/cmd/bundle/testdata/out.golden @@ -26,7 +26,9 @@ type prefixS struct { } // Function bar. -func prefixbar(s *prefixS) { fmt.Println(s.prefixt, s.u) } +func prefixbar(s *prefixS) { + fmt.Println(s.prefixt, s.u) // comment inside function +} type prefixt int diff --git a/cmd/bundle/testdata/src/initial/a.go b/cmd/bundle/testdata/src/initial/a.go index 99cd145a..5d0d1900 100644 --- a/cmd/bundle/testdata/src/initial/a.go +++ b/cmd/bundle/testdata/src/initial/a.go @@ -12,4 +12,6 @@ type S struct { } // Function bar. -func bar(s *S) { fmt.Println(s.t, s.u) } +func bar(s *S) { + fmt.Println(s.t, s.u) // comment inside function +}