Skip to content

Commit

Permalink
internal/godoc/dochtml: add type parameters on func synopsis
Browse files Browse the repository at this point in the history
Adding type parameters on functions synopsis.

Fixes golang/go#60954

Change-Id: If72c1b4d401b6cd32c335b1ed136063e7c965743
GitHub-Last-Rev: 375797e
GitHub-Pull-Request: #79
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/535555
Reviewed-by: Suzy Mueller <[email protected]>
kokoro-CI: kokoro <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
ThiagoSDQ authored and jba committed Nov 8, 2023
1 parent 8d5d35d commit d8c9c76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You can check it out at [https://pkg.go.dev](https://pkg.go.dev).
`pkgsite` program extracts and generates documentation for Go projects.

Example usage:

```
$ go install golang.org/x/pkgsite/cmd/pkgsite@latest
$ cd myproject
Expand Down
2 changes: 1 addition & 1 deletion internal/fetch/fetchdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2845,7 +2845,7 @@ var moduleGenerics = &testModule{
{
SymbolMeta: internal.SymbolMeta{
Name: "Min",
Synopsis: "func Min(a, b T) T",
Synopsis: "func Min[T constraints.Ordered](a, b T) T",
Section: "Functions",
Kind: "Function",
},
Expand Down
19 changes: 16 additions & 3 deletions internal/godoc/dochtml/internal/render/synopsis.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,17 @@ func OneLineNodeDepth(fset *token.FileSet, node ast.Node, depth int) string {
}
}

tparam := formatTypeParams(fset, 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 {
Expand Down Expand Up @@ -214,3 +216,14 @@ func joinStrings(ss []string) string {
}
return strings.Join(ss, ", ")
}

func formatTypeParams(fset *token.FileSet, list *ast.FieldList, depth int) string {
if list.NumFields() == 0 {
return ""
}
var tparams []string
for _, field := range list.List {
tparams = append(tparams, OneLineField(fset, field, depth))
}
return "[" + joinStrings(tparams) + "]"
}

0 comments on commit d8c9c76

Please sign in to comment.