cmd/doc: show documentation for interface methods when requested explicitly

For historical reasons, the go/doc package does not include
the methods within an interface as part of the documented
methods for that type. Thus,

	go doc ast.Node.Pos

gives an incorrect and confusing error message:

	doc: no method Node.Pos in package go/ast

This CL does some dirty work to dig down to the methods
so interface methods now present their documentation:

% go doc ast.node.pos
func Pos() token.Pos  // position of first character belonging to the node
%

It must largely sidestep the doc package to do this, which
is a shame. Perhaps things will improve there one day.

The change does not handle embeddings, and in principle the
same approach could be done for struct fields, but that is also
not here yet. But this CL fixes the thing that was bugging me.

Change-Id: Ic10a91936da96f54ee0b2f4a4fe4a8c9b93a5b4a
Reviewed-on: https://go-review.googlesource.com/31852
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Rob Pike 2016-10-24 12:38:06 -07:00
parent 050f378085
commit 2ee82edfc2
2 changed files with 57 additions and 7 deletions

View file

@ -304,7 +304,7 @@ var tests = []test{
// Interface.
{
"type",
"interface type",
[]string{p, `ExportedInterface`},
[]string{
`Comment about exported interface`, // Include comment.
@ -324,7 +324,7 @@ var tests = []test{
},
// Interface -u with unexported methods.
{
"type with unexported methods and -u",
"interface type with unexported methods and -u",
[]string{"-u", p, `ExportedInterface`},
[]string{
`Comment about exported interface`, // Include comment.
@ -340,6 +340,19 @@ var tests = []test{
},
},
// Interface method.
{
"interface method",
[]string{p, `ExportedInterface.ExportedMethod`},
[]string{
`Comment before exported method.*\n.*ExportedMethod\(\)` +
`.*Comment on line with exported method`,
},
[]string{
`Comment about exported interface.`,
},
},
// Method.
{
"method",