Skip to content

Commit fa2d646

Browse files
committed
internal/godoc/internal/doc: support examples on generic methods
Remove bracket contents from generic method example identifiers to allow them to be paired with examples. Port of https://go.dev/cl/401761. Fixes golang/go#52363. Change-Id: I4404c833d1aa98308407cdc6199fbe6261f256c9 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/401621 Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]>
1 parent 0c18560 commit fa2d646

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

internal/godoc/internal/doc/example.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func classifyExamples(p *Package, examples []*Example) {
492492
if !token.IsExported(m.Name) {
493493
continue
494494
}
495-
ids[strings.TrimPrefix(m.Recv, "*")+"_"+m.Name] = &m.Examples
495+
ids[strings.TrimPrefix(nameWithoutInst(m.Recv), "*")+"_"+m.Name] = &m.Examples
496496
}
497497
}
498498

@@ -527,6 +527,24 @@ func classifyExamples(p *Package, examples []*Example) {
527527
}
528528
}
529529

530+
// nameWithoutInst returns name if name has no brackets. If name contains
531+
// brackets, then it returns name with all the contents between (and including)
532+
// the outermost left and right bracket removed.
533+
//
534+
// Adapted from debug/gosym/symtab.go:Sym.nameWithoutInst.
535+
func nameWithoutInst(name string) string {
536+
start := strings.Index(name, "[")
537+
if start < 0 {
538+
return name
539+
}
540+
end := strings.LastIndex(name, "]")
541+
if end < 0 {
542+
// Malformed name, should contain closing bracket too.
543+
return name
544+
}
545+
return name[0:start] + name[end+1:]
546+
}
547+
530548
// splitExampleName attempts to split example name s at index i,
531549
// and reports if that produces a valid split. The suffix may be
532550
// absent. Otherwise, it must start with a lower-case letter and

internal/godoc/internal/doc/example_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"bytes"
99
"fmt"
1010
"go/ast"
11-
"go/doc"
1211
"go/format"
1312
"go/parser"
1413
"go/token"
1514
"reflect"
1615
"strings"
1716
"testing"
17+
18+
"golang.org/x/pkgsite/internal/godoc/internal/doc"
1819
)
1920

2021
const exampleTestFile = `
@@ -616,6 +617,10 @@ type (
616617
)
617618
618619
func (Conflict) Conflict() {}
620+
621+
type G[T any] int
622+
623+
func (G[T]) M() {}
619624
`
620625
const test = `
621626
package p_test
@@ -675,6 +680,9 @@ func ExampleConflict_Conflict() {} // ambiguous with either Conflict or C
675680
func ExampleConflict_conflict() {} // ambiguous with either Conflict or Conflict_conflict type
676681
func ExampleConflict_Conflict_suffix() {} // ambiguous with either Conflict or Conflict_Conflict type
677682
func ExampleConflict_conflict_suffix() {} // ambiguous with either Conflict or Conflict_conflict type
683+
684+
func ExampleG_M() {}
685+
func ExampleG_M_suffix() {}
678686
`
679687

680688
// Parse literal source code as a *doc.Package.
@@ -724,12 +732,18 @@ func ExampleConflict_conflict_suffix() {} // ambiguous with either Conflict or C
724732
// These are implementation dependent due to the ambiguous parsing.
725733
"Conflict_Conflict": {"", "suffix"},
726734
"Conflict_conflict": {"", "suffix"},
735+
736+
"G.M": {"", "suffix"},
727737
}
728738

729739
for id := range got {
730740
if !reflect.DeepEqual(got[id], want[id]) {
731741
t.Errorf("classification mismatch for %q:\ngot %q\nwant %q", id, got[id], want[id])
732742
}
743+
delete(want, id)
744+
}
745+
if len(want) > 0 {
746+
t.Errorf("did not find:\n%q", want)
733747
}
734748
}
735749

0 commit comments

Comments
 (0)