From ce1291533bf047e29f9e188b3f697d3a5157d142 Mon Sep 17 00:00:00 2001 From: Masahiro Furudate Date: Sun, 7 May 2017 04:40:42 +0900 Subject: [PATCH] godoc: remove the function declaration link The link of the function declaration part is unnecessary because it points to the same place. Removed the link of the function declaration part. "IdentMode" has been removed since it is no longer used. Fixes golang/go#20269 Change-Id: I6399899947e46dc84c5432c1d645f6d96b7db4f6 Reviewed-on: https://go-review.googlesource.com/42892 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- godoc/godoc_test.go | 22 ++++++++++++++++++++++ godoc/linkify.go | 37 +++++++++++++++---------------------- 2 files changed, 37 insertions(+), 22 deletions(-) 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 }