diff options
author | Robert Findley <rfindley@google.com> | 2021-10-29 17:41:41 -0400 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2021-11-04 14:54:46 +0000 |
commit | 00d6d2037ee1bf4b6959219120b79b7c01244b02 (patch) | |
tree | df2d5504710ef46ce7b3ddc597cf3db3ccb16e89 /src/cmd/doc | |
parent | 901bf291bc90819cb6dad76064475cf9ecbc9651 (diff) | |
download | go-00d6d2037ee1bf4b6959219120b79b7c01244b02.tar.gz go-00d6d2037ee1bf4b6959219120b79b7c01244b02.zip |
cmd/doc, go/doc: add basic support for generic code
Update cmd/doc and go/doc for the generics, by adding handling for type
parameters and the new embedded interface elements.
Specifically:
- Format type parameters when summarizing type and function nodes.
- Find the origin type name for instantiation expressions, so that
methods are associated with generic type declarations.
- Generalize the handling of embedding 'error' in interfaces to
arbitrary predeclared types.
- Keep embedded type literals.
- Update filtering to descend into embedded type literals.
Also add "any" to the list of predeclared types.
Updates #49210
Change-Id: I6ea82869f19c3cdbc3c842f01581c8fc7e1c2ee7
Reviewed-on: https://go-review.googlesource.com/c/go/+/359778
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/doc')
-rw-r--r-- | src/cmd/doc/pkg.go | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/cmd/doc/pkg.go b/src/cmd/doc/pkg.go index 822c9e16f8..2257c5c0eb 100644 --- a/src/cmd/doc/pkg.go +++ b/src/cmd/doc/pkg.go @@ -323,7 +323,8 @@ func (pkg *Package) oneLineNodeDepth(node ast.Node, depth int) string { if n.Assign.IsValid() { sep = " = " } - return fmt.Sprintf("type %s%s%s", n.Name.Name, sep, pkg.oneLineNodeDepth(n.Type, depth)) + tparams := pkg.formatTypeParams(n.TypeParams, depth) + return fmt.Sprintf("type %s%s%s%s", n.Name.Name, tparams, sep, pkg.oneLineNodeDepth(n.Type, depth)) case *ast.FuncType: var params []string @@ -342,15 +343,16 @@ func (pkg *Package) oneLineNodeDepth(node ast.Node, depth int) string { } } + tparam := pkg.formatTypeParams(n.TypeParams, depth) param := joinStrings(params) if len(results) == 0 { - return fmt.Sprintf("func(%s)", param) + return fmt.Sprintf("func%s(%s)", tparam, param) } result := joinStrings(results) if !needParens { - return fmt.Sprintf("func(%s) %s", param, result) + return fmt.Sprintf("func%s(%s) %s", tparam, param, result) } - return fmt.Sprintf("func(%s) (%s)", param, result) + return fmt.Sprintf("func%s(%s) (%s)", tparam, param, result) case *ast.StructType: if n.Fields == nil || len(n.Fields.List) == 0 { @@ -419,6 +421,17 @@ func (pkg *Package) oneLineNodeDepth(node ast.Node, depth int) string { } } +func (pkg *Package) formatTypeParams(list *ast.FieldList, depth int) string { + if list.NumFields() == 0 { + return "" + } + var tparams []string + for _, field := range list.List { + tparams = append(tparams, pkg.oneLineField(field, depth)) + } + return "[" + joinStrings(tparams) + "]" +} + // oneLineField returns a one-line summary of the field. func (pkg *Package) oneLineField(field *ast.Field, depth int) string { var names []string |