Skip to content

Commit 08c9308

Browse files
committed
Reland "godoc: skip build tag annotations when displaying examples"
After moving the filepath.Walk example to a standalone example file in CL 122237 (so it could use a standalone function), godoc includes the build tag annotation ("// +build !windows,!plan9" in this case) in the runnable example. The example runs correctly, but the annotation might be confusing for new users. I think godoc should skip these annotations when displaying examples. Fixes golang/go#26490. Skipped the testcase for now, since it triggers a false positive: golang/go#26627 Change-Id: I1da4b3b7e1e5a85a76773e25d9355b3f92479c19
1 parent bfb5194 commit 08c9308

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

godoc/godoc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string
666666
play := ""
667667
if eg.Play != nil && p.ShowPlayground {
668668
var buf bytes.Buffer
669+
eg.Play.Comments = filterOutBuildAnnotations(eg.Play.Comments)
669670
if err := format.Node(&buf, info.FSet, eg.Play); err != nil {
670671
log.Print(err)
671672
} else {
@@ -694,6 +695,23 @@ func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string
694695
return buf.String()
695696
}
696697

698+
func filterOutBuildAnnotations(cg []*ast.CommentGroup) []*ast.CommentGroup {
699+
if len(cg) == 0 {
700+
return cg
701+
}
702+
703+
for i := range cg {
704+
if !strings.HasPrefix(cg[i].Text(), "+build ") {
705+
// Found the first non-build tag, return from here until the end
706+
// of the slice.
707+
return cg[i:]
708+
}
709+
}
710+
711+
// There weren't any non-build tags, return an empty slice.
712+
return []*ast.CommentGroup{}
713+
}
714+
697715
// example_nameFunc takes an example function name and returns its display
698716
// name. For example, "Foo_Bar_quux" becomes "Foo.Bar (Quux)".
699717
func (p *Presentation) example_nameFunc(s string) string {

0 commit comments

Comments
 (0)