diff --git a/godoc/godoc_test.go b/godoc/godoc_test.go index afc31374..ef5790ed 100644 --- a/godoc/godoc_test.go +++ b/godoc/godoc_test.go @@ -202,6 +202,28 @@ var S T = T{string) (resp *Response, err error)` + if got != want { + t.Errorf("got: %s\n\nwant: %s\n", got, want) + } + + // Method. + got = linkifySource(t, []byte(` +package http + +func (h Header) Get(key string) string`)) + want = `func (h Header) Get(key string) string` + if got != want { + t.Errorf("got: %s\n\nwant: %s\n", got, want) + } +} + func linkifySource(t *testing.T, src []byte) string { p := &Presentation{ DeclLinks: true, diff --git a/godoc/linkify.go b/godoc/linkify.go index 02720599..dbe0e419 100644 --- a/godoc/linkify.go +++ b/godoc/linkify.go @@ -53,7 +53,7 @@ func LinkifyText(w io.Writer, text []byte, n ast.Node) { prev = "a" case info.path == "" && info.name != "": // local identifier - if info.mode == identVal { + if info.isVal { fmt.Fprintf(w, ``, info.name) prev = "span" } else if ast.IsExported(info.name) { @@ -74,19 +74,10 @@ func LinkifyText(w io.Writer, text []byte, n ast.Node) { // The zero value of a link represents "no link". // type link struct { - mode identMode path, name string // package path, identifier name + isVal bool // identifier is defined in a const or var declaration } -// The identMode describes how an identifier is "used" at its source location. -type identMode int - -const ( - identUse identMode = iota // identifier is used (must be zero value for identMode) - identDef // identifier is defined - identVal // identifier is defined in a const or var declaration -) - // linksFor returns the list of links for the identifiers used // by node in the same order as they appear in the source. // @@ -101,18 +92,20 @@ func linksFor(node ast.Node) (links []link) { switch n := node.(type) { case *ast.Field: for _, n := range n.Names { - linkMap[n] = link{mode: identDef} + linkMap[n] = link{} } case *ast.ImportSpec: if name := n.Name; name != nil { - linkMap[name] = link{mode: identDef} + linkMap[name] = link{} } case *ast.ValueSpec: for _, n := range n.Names { - linkMap[n] = link{mode: identVal, name: n.Name} + linkMap[n] = link{name: n.Name, isVal: true} } + case *ast.FuncDecl: + linkMap[n.Name] = link{} case *ast.TypeSpec: - linkMap[n.Name] = link{mode: identDef} + linkMap[n.Name] = link{} case *ast.AssignStmt: // Short variable declarations only show up if we apply // this code to all source code (as opposed to exported @@ -125,7 +118,7 @@ func linksFor(node ast.Node) (links []link) { // Each lhs expression should be an // ident, but we are conservative and check. if n, _ := x.(*ast.Ident); n != nil { - linkMap[n] = link{mode: identVal} + linkMap[n] = link{isVal: true} } } } @@ -141,8 +134,8 @@ func linksFor(node ast.Node) (links []link) { if path, err := strconv.Unquote(spec.Path.Value); err == nil { // Register two links, one for the package // and one for the qualified identifier. - linkMap[x] = link{mode: identUse, path: path} - linkMap[n.Sel] = link{mode: identUse, path: path, name: n.Sel.Name} + linkMap[x] = link{path: path} + linkMap[n.Sel] = link{path: path, name: n.Sel.Name} } } } @@ -164,8 +157,8 @@ func linksFor(node ast.Node) (links []link) { if path, err := strconv.Unquote(spec.Path.Value); err == nil { // Register two links, one for the package // and one for the qualified identifier. - linkMap[x] = link{mode: identUse, path: path} - linkMap[typ.Sel] = link{mode: identUse, path: path, name: typ.Sel.Name} + linkMap[x] = link{path: path} + linkMap[typ.Sel] = link{path: path, name: typ.Sel.Name} fieldPath = path prefix = typ.Sel.Name + "." } @@ -180,7 +173,7 @@ func linksFor(node ast.Node) (links []link) { // if this is a struct literal or a map literal without type // information. We assume struct literal. name := prefix + k.Name - linkMap[k] = link{mode: identUse, path: fieldPath, name: name} + linkMap[k] = link{path: fieldPath, name: name} } } } @@ -188,7 +181,7 @@ func linksFor(node ast.Node) (links []link) { if l, ok := linkMap[n]; ok { links = append(links, l) } else { - l := link{mode: identUse, name: n.Name} + l := link{name: n.Name} if n.Obj == nil && predeclared[n.Name] { l.path = builtinPkgPath }