Skip to content

Commit

Permalink
update:define FuncId by RenderOption
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Apr 20, 2024
1 parent 2070250 commit 3bacb51
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
31 changes: 15 additions & 16 deletions internal/godoc/dochtml/dochtml.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
"golang.org/x/pkgsite/internal/gopdoc"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -60,6 +59,7 @@ type RenderOptions struct {
FileLinkFunc func(file string) (url string)
SourceLinkFunc func(ast.Node) string
SinceVersionFunc func(name string) string
FuncIdFunc func(fn *doc.Func, fullName string) (id string)
// ModInfo optionally specifies information about the module the package
// belongs to in order to render module-related documentation.
ModInfo *ModuleInfo
Expand Down Expand Up @@ -89,15 +89,15 @@ type Parts struct {
//
// If any of the rendered documentation part HTML sizes exceeds the specified limit,
// an error with ErrTooLarge in its chain will be returned.
func Render(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions, info *gopdoc.GopInfo) (_ *Parts, err error) {
func Render(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions) (_ *Parts, err error) {
defer derrors.Wrap(&err, "dochtml.RenderParts")

if opt.Limit == 0 {
const megabyte = 1000 * 1000
opt.Limit = 10 * megabyte
}

funcs, data, links := renderInfo(ctx, fset, p, opt, info)
funcs, data, links := renderInfo(ctx, fset, p, opt)
p = data.Package
if docIsEmpty(p) {
return &Parts{}, nil
Expand Down Expand Up @@ -145,12 +145,13 @@ type item struct {
FuncId string // for functions and methods
}

func packageToItems(p *doc.Package, exmap map[string][]*example, info *gopdoc.GopInfo) (consts, vars, funcs, types []*item) {
func packageToItems(p *doc.Package, exmap map[string][]*example, opt RenderOptions) (consts, vars, funcs, types []*item) {

consts = valuesToItems(p.Consts)
vars = valuesToItems(p.Vars)
funcs = funcsToItems(p.Funcs, "Documentation-functionHeader", "", exmap, info)
funcs = funcsToItems(p.Funcs, "Documentation-functionHeader", "", exmap, opt)
for _, t := range p.Types {
types = append(types, typeToItem(t, exmap, info))
types = append(types, typeToItem(t, exmap, opt))
}
return consts, vars, funcs, types
}
Expand All @@ -171,7 +172,7 @@ func valueToItem(v *doc.Value) *item {
}
}

func funcsToItems(fs []*doc.Func, hclass, typeName string, exmap map[string][]*example, info *gopdoc.GopInfo) []*item {
func funcsToItems(fs []*doc.Func, hclass, typeName string, exmap map[string][]*example, opt RenderOptions) []*item {
var r []*item
for _, f := range fs {
fullName := f.Name
Expand All @@ -185,10 +186,8 @@ func funcsToItems(fs []*doc.Func, hclass, typeName string, exmap map[string][]*e
headerStart += " (" + f.Recv + ")"
}
funcId := fullName
if info != nil {
if overloadOrder, ok := info.OverloadFuncsOrder[f]; ok && overloadOrder > 0 {
funcId = fmt.Sprintf("%s__%d", fullName, overloadOrder)
}
if opt.FuncIdFunc != nil {
funcId = opt.FuncIdFunc(f, fullName)
}
i := &item{
Doc: f.Doc,
Expand All @@ -207,7 +206,7 @@ func funcsToItems(fs []*doc.Func, hclass, typeName string, exmap map[string][]*e
return r
}

func typeToItem(t *doc.Type, exmap map[string][]*example, info *gopdoc.GopInfo) *item {
func typeToItem(t *doc.Type, exmap map[string][]*example, opt RenderOptions) *item {
return &item{
Name: t.Name,
FullName: t.Name,
Expand All @@ -220,8 +219,8 @@ func typeToItem(t *doc.Type, exmap map[string][]*example, info *gopdoc.GopInfo)
Examples: exmap[t.Name],
Consts: valuesToItems(t.Consts),
Vars: valuesToItems(t.Vars),
Funcs: funcsToItems(t.Funcs, "Documentation-typeFuncHeader", "", exmap, info),
Methods: funcsToItems(t.Methods, "Documentation-typeMethodHeader", t.Name, exmap, info),
Funcs: funcsToItems(t.Funcs, "Documentation-typeFuncHeader", "", exmap, opt),
Methods: funcsToItems(t.Methods, "Documentation-typeMethodHeader", t.Name, exmap, opt),
}
}

Expand All @@ -235,7 +234,7 @@ func docIsEmpty(p *doc.Package) bool {
}

// renderInfo returns the functions and data needed to render the doc.
func renderInfo(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions, info *gopdoc.GopInfo) (map[string]any, TemplateData, func() []render.Link) {
func renderInfo(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions) (map[string]any, TemplateData, func() []render.Link) {
// Make a copy to avoid modifying caller's *doc.Package.
p2 := *p
p = &p2
Expand Down Expand Up @@ -303,7 +302,7 @@ func renderInfo(ctx context.Context, fset *token.FileSet, p *doc.Package, opt Re
Examples: examples,
NoteHeaders: buildNoteHeaders(p.Notes),
}
data.Consts, data.Vars, data.Funcs, data.Types = packageToItems(p, examples.Map, info)
data.Consts, data.Vars, data.Funcs, data.Types = packageToItems(p, examples.Map, opt)
return funcs, data, r.Links
}

Expand Down
9 changes: 5 additions & 4 deletions internal/godoc/dochtml/dochtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestRender(t *testing.T) {
for _, pkg := range []string{"everydecl", "comments"} {
t.Run(pkg, func(t *testing.T) {
fset, d := mustLoadPackage(pkg)
parts, err := Render(ctx, fset, d, testRenderOptions, nil)
parts, err := Render(ctx, fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestRender(t *testing.T) {
func TestRenderDeprecated(t *testing.T) {
t.Helper()
fset, d := mustLoadPackage("deprecated")
parts, err := Render(context.Background(), fset, d, testRenderOptions, nil)
parts, err := Render(context.Background(), fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
Expand All @@ -96,7 +96,8 @@ func TestRenderOverload(t *testing.T) {
LoadTemplates(templateFS)
fset, d := mustLoadPackage("overload")
doc, gopinfo := gopdoc.Transform(d)
parts, err := Render(context.Background(), fset, doc, testRenderOptions, gopinfo)
testRenderOptions.FuncIdFunc = gopinfo.FuncId
parts, err := Render(context.Background(), fset, doc, testRenderOptions)
if err != nil {
t.Fatal(err)
}
Expand All @@ -123,7 +124,7 @@ func TestExampleRender(t *testing.T) {
ctx := context.Background()
fset, d := mustLoadPackage("example_test")

parts, err := Render(ctx, fset, d, testRenderOptions, nil)
parts, err := Render(ctx, fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/godoc/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ func (p *Package) Render(ctx context.Context, innerPath string,
}

opts := p.renderOptions(innerPath, sourceInfo, modInfo, nameToVersion, bc)
parts, err := dochtml.Render(ctx, p.Fset, d, opts, info)
opts.FuncIdFunc = info.FuncId
parts, err := dochtml.Render(ctx, p.Fset, d, opts)
if errors.Is(err, ErrTooLarge) {
return &dochtml.Parts{Body: template.MustParseAndExecuteToHTML(DocTooLargeReplacement)}, nil
}
Expand Down
15 changes: 13 additions & 2 deletions internal/gopdoc/z_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package gopdoc

import (
"fmt"
"go/ast"
"go/doc"
"go/token"
Expand Down Expand Up @@ -48,7 +49,17 @@ type transformCtx struct {
}

type GopInfo struct {
OverloadFuncsOrder map[*doc.Func]int
overloadFuncsOrder map[*doc.Func]int
}

func (g *GopInfo) FuncId(fn *doc.Func, fullName string) string {
if g == nil || g.overloadFuncsOrder == nil {
return fullName

Check warning on line 57 in internal/gopdoc/z_transform.go

View check run for this annotation

Codecov / codecov/patch

internal/gopdoc/z_transform.go#L55-L57

Added lines #L55 - L57 were not covered by tests
}
if overloadOrder, ok := g.overloadFuncsOrder[fn]; ok && overloadOrder > 0 {
return fmt.Sprintf("%s__%d", fullName, overloadOrder)

Check warning on line 60 in internal/gopdoc/z_transform.go

View check run for this annotation

Codecov / codecov/patch

internal/gopdoc/z_transform.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}
return fullName

Check warning on line 62 in internal/gopdoc/z_transform.go

View check run for this annotation

Codecov / codecov/patch

internal/gopdoc/z_transform.go#L62

Added line #L62 was not covered by tests
}

func (p *transformCtx) finish(in *doc.Package) {
Expand Down Expand Up @@ -259,7 +270,7 @@ func Transform(in *doc.Package) (*doc.Package, *GopInfo) {
transformTypes(ctx, in.Types)
ctx.finish(in)
return in, &GopInfo{
OverloadFuncsOrder: ctx.orders,
overloadFuncsOrder: ctx.orders,
}
} /* else if in.ImportPath == "builtin" {
transformBuiltin(in)
Expand Down

0 comments on commit 3bacb51

Please sign in to comment.