From 936715c71c91e99f3d5f5acfa7c073183339281c Mon Sep 17 00:00:00 2001 From: Brad Garcia Date: Fri, 27 Jun 2014 10:25:57 -0400 Subject: [PATCH] godoc: Strip "/src/pkg/" prefix from identifier keys, so that it's keyed identically to import count. Intern the documentation strings. LGTM=sameer R=sameer, bradfitz CC=golang-codereviews https://golang.org/cl/107250043 --- godoc/godoc.go | 11 ++++++----- godoc/godoc_test.go | 6 ++++-- godoc/index.go | 13 +++++++------ godoc/index_test.go | 16 ++++++++-------- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/godoc/godoc.go b/godoc/godoc.go index 5b9f95f7b6..dd574dce81 100644 --- a/godoc/godoc.go +++ b/godoc/godoc.go @@ -303,11 +303,12 @@ func (info *PageInfo) IsEmpty() bool { } func pkgLinkFunc(path string) string { - relpath := path[1:] // because of the irregular mapping under goroot // we need to correct certain relative paths - relpath = strings.TrimPrefix(relpath, "src/pkg/") - return "pkg/" + relpath // remove trailing '/' for relative URL + path = strings.TrimPrefix(path, "/") + path = strings.TrimPrefix(path, "src/") + path = strings.TrimPrefix(path, "pkg/") + return "pkg/" + path } func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n interface{}) string { @@ -368,6 +369,7 @@ func srcPosLinkFunc(s string, line, low, high int) string { func srcLinkFunc(s string) string { s = pathpkg.Clean("/" + s) + // TODO(bgarcia): Once the /src/pkg -> /src transition occurs, update this function. if !strings.HasPrefix(s, "/src/pkg/") { s = "/src/pkg" + s } @@ -388,8 +390,7 @@ func queryLinkFunc(s, query string, line int) string { } func docLinkFunc(s string, ident string) string { - s = strings.TrimPrefix(s, "/src") - return pathpkg.Clean("/"+s) + "/#" + ident + return pathpkg.Clean("/pkg/"+s) + "/#" + ident } func (p *Presentation) example_textFunc(info *PageInfo, funcName, indent string) string { diff --git a/godoc/godoc_test.go b/godoc/godoc_test.go index 76ce946a36..37e1d713d6 100644 --- a/godoc/godoc_test.go +++ b/godoc/godoc_test.go @@ -14,7 +14,9 @@ func TestPkgLinkFunc(t *testing.T) { want string }{ {"/src/pkg/fmt", "pkg/fmt"}, + {"src/pkg/fmt", "pkg/fmt"}, {"/fmt", "pkg/fmt"}, + {"fmt", "pkg/fmt"}, } { if got := pkgLinkFunc(tc.path); got != tc.want { t.Errorf("pkgLinkFunc(%v) = %v; want %v", tc.path, got, tc.want) @@ -84,8 +86,8 @@ func TestDocLinkFunc(t *testing.T) { ident string want string }{ - {"/src/pkg/fmt", "Sprintf", "/pkg/fmt/#Sprintf"}, - {"/src/pkg/fmt", "EOF", "/pkg/fmt/#EOF"}, + {"fmt", "Sprintf", "/pkg/fmt/#Sprintf"}, + {"fmt", "EOF", "/pkg/fmt/#EOF"}, } { if got := docLinkFunc(tc.src, tc.ident); got != tc.want { t.Errorf("docLinkFunc(%v, %v) = %v; want %v", tc.src, tc.ident, got, tc.want) diff --git a/godoc/index.go b/godoc/index.go index 417c386bd1..f14f5c1123 100644 --- a/godoc/index.go +++ b/godoc/index.go @@ -694,11 +694,11 @@ func isWhitelisted(filename string) bool { } func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File) { - pkgName := astFile.Name.Name + pkgName := x.intern(astFile.Name.Name) if pkgName == "main" { return } - dirname = pathpkg.Clean(dirname) + pkgPath := x.intern(strings.TrimPrefix(strings.TrimPrefix(dirname, "/src/"), "pkg/")) astPkg := ast.Package{ Name: pkgName, Files: map[string]*ast.File{ @@ -711,8 +711,9 @@ func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File) if x.idents[sk] == nil { x.idents[sk] = make(map[string][]Ident) } + name = x.intern(name) x.idents[sk][name] = append(x.idents[sk][name], Ident{ - Path: dirname, + Path: pkgPath, Package: pkgName, Name: name, Doc: doc.Synopsis(docstr), @@ -722,7 +723,7 @@ func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File) if x.idents[PackageClause] != nil { pkgs := x.idents[PackageClause][docPkg.Name] for i, p := range pkgs { - if p.Path == dirname { + if p.Path == pkgPath { foundPkg = true if docPkg.Doc != "" { p.Doc = doc.Synopsis(docPkg.Doc) @@ -760,7 +761,7 @@ func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File) // Change the name of methods to be ".". // They will still be indexed as . idents := x.idents[MethodDecl][f.Name] - idents[len(idents)-1].Name = t.Name + "." + f.Name + idents[len(idents)-1].Name = x.intern(t.Name + "." + f.Name) } } for _, v := range docPkg.Vars { @@ -797,7 +798,7 @@ func (x *Indexer) indexGoFile(dirname string, filename string, file *token.File, if _, ok := x.packagePath[ppKey]; !ok { x.packagePath[ppKey] = make(map[string]bool) } - pkgPath := x.intern(strings.TrimPrefix(dirname, "/src/pkg/")) + pkgPath := x.intern(strings.TrimPrefix(strings.TrimPrefix(dirname, "/src/"), "pkg/")) x.packagePath[ppKey][pkgPath] = true // Merge in exported symbols found walking this file into diff --git a/godoc/index_test.go b/godoc/index_test.go index 9a71a47ed5..9341784904 100644 --- a/godoc/index_test.go +++ b/godoc/index_test.go @@ -231,23 +231,23 @@ func checkIdents(t *testing.T, c *Corpus, ix *Index) { want = map[SpotKind]map[string][]Ident{ PackageClause: map[string][]Ident{ "bar": []Ident{ - {"/src/pkg/bar", "bar", "bar", "Package bar is another example to test races."}, - {"/src/pkg/other/bar", "bar", "bar", "Package bar is another bar package."}, + {"bar", "bar", "bar", "Package bar is another example to test races."}, + {"other/bar", "bar", "bar", "Package bar is another bar package."}, }, - "foo": []Ident{{"/src/pkg/foo", "foo", "foo", "Package foo is an example."}}, + "foo": []Ident{{"foo", "foo", "foo", "Package foo is an example."}}, }, ConstDecl: map[string][]Ident{ - "Pi": []Ident{{"/src/pkg/foo", "foo", "Pi", ""}}, + "Pi": []Ident{{"foo", "foo", "Pi", ""}}, }, VarDecl: map[string][]Ident{ - "Foos": []Ident{{"/src/pkg/foo", "foo", "Foos", ""}}, + "Foos": []Ident{{"foo", "foo", "Foos", ""}}, }, TypeDecl: map[string][]Ident{ - "Foo": []Ident{{"/src/pkg/foo", "foo", "Foo", "Foo is stuff."}}, + "Foo": []Ident{{"foo", "foo", "Foo", "Foo is stuff."}}, }, FuncDecl: map[string][]Ident{ - "New": []Ident{{"/src/pkg/foo", "foo", "New", ""}}, - "X": []Ident{{"/src/pkg/other/bar", "bar", "X", ""}}, + "New": []Ident{{"foo", "foo", "New", ""}}, + "X": []Ident{{"other/bar", "bar", "X", ""}}, }, } }