From 27d851fc2cb3c51fcd07d3790f6fc2bdd7f4a9dc Mon Sep 17 00:00:00 2001
From: Brad Garcia
Date: Mon, 6 Jan 2014 13:26:25 -0500
Subject: [PATCH] godoc: Add package identifiers to documentation indexing.
This allows us to show package synopsis in search results. Fix skipping of
test files (prefix changed from "test/" to "/test/").
R=bradfitz, bradfitz
CC=golang-codereviews
https://golang.org/cl/44790044
---
godoc/index.go | 28 +++++++++++++++++++++++-----
godoc/static/searchcode.html | 13 +++++++++++++
godoc/static/searchdoc.html | 23 ++++++++---------------
godoc/static/static.go | 36 +++++++++++++++++++++---------------
4 files changed, 65 insertions(+), 35 deletions(-)
diff --git a/godoc/index.go b/godoc/index.go
index 640aa81138..d5332d2384 100644
--- a/godoc/index.go
+++ b/godoc/index.go
@@ -132,7 +132,7 @@ const removeDuplicates = true
// A KindRun is a run of SpotInfos of the same kind in a given file.
// The kind (3 bits) is stored in each SpotInfo element; to find the
-// kind of a KindRun, look at any of it's elements.
+// kind of a KindRun, look at any of its elements.
type KindRun []SpotInfo
// KindRuns are sorted by line number or index. Since the isIndex bit
@@ -698,6 +698,7 @@ func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File)
if pkgName == "main" {
return
}
+ dirname = pathpkg.Clean(dirname)
astPkg := ast.Package{
Name: pkgName,
Files: map[string]*ast.File{
@@ -705,18 +706,35 @@ func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File)
},
}
var m doc.Mode
- docPkg := doc.New(&astPkg, pathpkg.Clean(dirname), m)
+ docPkg := doc.New(&astPkg, dirname, m)
addIdent := func(sk SpotKind, name string, docstr string) {
if x.idents[sk] == nil {
x.idents[sk] = make(map[string][]Ident)
}
x.idents[sk][name] = append(x.idents[sk][name], Ident{
- Path: pathpkg.Clean(dirname),
+ Path: dirname,
Package: pkgName,
Name: name,
Doc: doc.Synopsis(docstr),
})
}
+ foundPkg := false
+ if x.idents[PackageClause] != nil {
+ pkgs := x.idents[PackageClause][docPkg.Name]
+ for i, p := range pkgs {
+ if p.Path == dirname {
+ foundPkg = true
+ if docPkg.Doc != "" {
+ p.Doc = doc.Synopsis(docPkg.Doc)
+ pkgs[i] = p
+ }
+ break
+ }
+ }
+ }
+ if !foundPkg {
+ addIdent(PackageClause, docPkg.Name, docPkg.Doc)
+ }
for _, c := range docPkg.Consts {
for _, name := range c.Names {
addIdent(ConstDecl, name, c.Doc)
@@ -769,7 +787,7 @@ func (x *Indexer) indexGoFile(dirname string, filename string, file *token.File,
// Test files are already filtered out in visitFile if IndexGoCode and
// IndexFullText are false. Otherwise, check here.
isTestFile := (x.c.IndexGoCode || x.c.IndexFullText) &&
- (strings.HasSuffix(filename, "_test.go") || strings.HasPrefix(dirname, "test/"))
+ (strings.HasSuffix(filename, "_test.go") || strings.HasPrefix(dirname, "/test/"))
if !isTestFile {
x.indexDocs(dirname, filename, astFile)
}
@@ -816,7 +834,7 @@ func (x *Indexer) visitFile(dirname string, fi os.FileInfo) {
case x.c.IndexDocs:
if !goFile ||
strings.HasSuffix(fi.Name(), "_test.go") ||
- strings.HasPrefix(dirname, "test/") {
+ strings.HasPrefix(dirname, "/test/") {
return
}
default:
diff --git a/godoc/static/searchcode.html b/godoc/static/searchcode.html
index 22cd0c7356..a032e642c6 100644
--- a/godoc/static/searchcode.html
+++ b/godoc/static/searchcode.html
@@ -4,6 +4,19 @@
license that can be found in the LICENSE file.
-->
{{$query_url := urlquery .Query}}
+{{if not .Idents}}
+ {{with .Pak}}
+ Package {{html $.Query}}
+
+
+ {{range .}}
+ {{$pkg_html := pkgLink .Pak.Path | html}}
+ | {{$pkg_html}} |
+ {{end}}
+
+
+ {{end}}
+{{end}}
{{with .Hit}}
{{with .Decls}}
Package-level declarations
diff --git a/godoc/static/searchdoc.html b/godoc/static/searchdoc.html
index 222719098b..99e1f035d2 100644
--- a/godoc/static/searchdoc.html
+++ b/godoc/static/searchdoc.html
@@ -3,28 +3,21 @@
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
-{{with .Pak}}
- Package {{html $.Query}}
-
-
- {{range .}}
- {{$pkg_html := pkgLink .Pak.Path | html}}
- | {{$pkg_html}} |
- {{end}}
-
-
-{{end}}
{{range $key, $val := .Idents}}
{{if $val}}
{{$key.Name}}
{{range $val}}
{{$pkg_html := pkgLink .Path | html}}
- {{$doc_html := docLink .Path .Name| html}}
- {{html .Package}}.{{.Name}}
+ {{if eq "Packages" $key.Name}}
+ {{html .Path}}
+ {{else}}
+ {{$doc_html := docLink .Path .Name| html}}
+ {{html .Package}}.{{.Name}}
+ {{end}}
{{if .Doc}}
- {{comment_html .Doc}}
+ {{comment_html .Doc}}
{{else}}
- No documentation available
+ No documentation available
{{end}}
{{end}}
{{end}}
diff --git a/godoc/static/static.go b/godoc/static/static.go
index df75f100a0..ad409d3f1a 100644
--- a/godoc/static/static.go
+++ b/godoc/static/static.go
@@ -1447,6 +1447,19 @@ function PlaygroundOutput(el) {
license that can be found in the LICENSE file.
-->
{{$query_url := urlquery .Query}}
+{{if not .Idents}}
+ {{with .Pak}}
+ Package {{html $.Query}}
+
+
+ {{range .}}
+ {{$pkg_html := pkgLink .Pak.Path | html}}
+ | {{$pkg_html}} |
+ {{end}}
+
+
+ {{end}}
+{{end}}
{{with .Hit}}
{{with .Decls}}
Package-level declarations
@@ -1498,28 +1511,21 @@ function PlaygroundOutput(el) {
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
-{{with .Pak}}
- Package {{html $.Query}}
-
-
- {{range .}}
- {{$pkg_html := pkgLink .Pak.Path | html}}
- | {{$pkg_html}} |
- {{end}}
-
-
-{{end}}
{{range $key, $val := .Idents}}
{{if $val}}
{{$key.Name}}
{{range $val}}
{{$pkg_html := pkgLink .Path | html}}
- {{$doc_html := docLink .Path .Name| html}}
- {{html .Package}}.{{.Name}}
+ {{if eq "Packages" $key.Name}}
+ {{html .Path}}
+ {{else}}
+ {{$doc_html := docLink .Path .Name| html}}
+ {{html .Package}}.{{.Name}}
+ {{end}}
{{if .Doc}}
- {{comment_html .Doc}}
+ {{comment_html .Doc}}
{{else}}
- No documentation available
+ No documentation available
{{end}}
{{end}}
{{end}}