1
0
mirror of https://github.com/golang/go synced 2024-11-18 07:04:52 -07:00

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
This commit is contained in:
Brad Garcia 2014-01-06 13:26:25 -05:00
parent a28efa5d8c
commit 27d851fc2c
4 changed files with 65 additions and 35 deletions

View File

@ -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:

View File

@ -4,6 +4,19 @@
license that can be found in the LICENSE file.
-->
{{$query_url := urlquery .Query}}
{{if not .Idents}}
{{with .Pak}}
<h2 id="Packages">Package {{html $.Query}}</h2>
<p>
<table class="layout">
{{range .}}
{{$pkg_html := pkgLink .Pak.Path | html}}
<tr><td><a href="/{{$pkg_html}}">{{$pkg_html}}</a></td></tr>
{{end}}
</table>
</p>
{{end}}
{{end}}
{{with .Hit}}
{{with .Decls}}
<h2 id="Global">Package-level declarations</h2>

View File

@ -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}}
<h2 id="Packages">Package {{html $.Query}}</h2>
<p>
<table class="layout">
{{range .}}
{{$pkg_html := pkgLink .Pak.Path | html}}
<tr><td><a href="/{{$pkg_html}}">{{$pkg_html}}</a></td></tr>
{{end}}
</table>
</p>
{{end}}
{{range $key, $val := .Idents}}
{{if $val}}
<h2 id="Global">{{$key.Name}}</h2>
{{range $val}}
{{$pkg_html := pkgLink .Path | html}}
{{$doc_html := docLink .Path .Name| html}}
<a href="/{{$pkg_html}}">{{html .Package}}</a>.<a href="{{$doc_html}}">{{.Name}}</a>
{{if eq "Packages" $key.Name}}
<a href="/{{$pkg_html}}">{{html .Path}}</a>
{{else}}
{{$doc_html := docLink .Path .Name| html}}
<a href="/{{$pkg_html}}">{{html .Package}}</a>.<a href="{{$doc_html}}">{{.Name}}</a>
{{end}}
{{if .Doc}}
<p>{{comment_html .Doc}}</p>
<p>{{comment_html .Doc}}</p>
{{else}}
<p><em>No documentation available</em></p>
<p><em>No documentation available</em></p>
{{end}}
{{end}}
{{end}}

View File

@ -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}}
<h2 id="Packages">Package {{html $.Query}}</h2>
<p>
<table class="layout">
{{range .}}
{{$pkg_html := pkgLink .Pak.Path | html}}
<tr><td><a href="/{{$pkg_html}}">{{$pkg_html}}</a></td></tr>
{{end}}
</table>
</p>
{{end}}
{{end}}
{{with .Hit}}
{{with .Decls}}
<h2 id="Global">Package-level declarations</h2>
@ -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}}
<h2 id="Packages">Package {{html $.Query}}</h2>
<p>
<table class="layout">
{{range .}}
{{$pkg_html := pkgLink .Pak.Path | html}}
<tr><td><a href="/{{$pkg_html}}">{{$pkg_html}}</a></td></tr>
{{end}}
</table>
</p>
{{end}}
{{range $key, $val := .Idents}}
{{if $val}}
<h2 id="Global">{{$key.Name}}</h2>
{{range $val}}
{{$pkg_html := pkgLink .Path | html}}
{{$doc_html := docLink .Path .Name| html}}
<a href="/{{$pkg_html}}">{{html .Package}}</a>.<a href="{{$doc_html}}">{{.Name}}</a>
{{if eq "Packages" $key.Name}}
<a href="/{{$pkg_html}}">{{html .Path}}</a>
{{else}}
{{$doc_html := docLink .Path .Name| html}}
<a href="/{{$pkg_html}}">{{html .Package}}</a>.<a href="{{$doc_html}}">{{.Name}}</a>
{{end}}
{{if .Doc}}
<p>{{comment_html .Doc}}</p>
<p>{{comment_html .Doc}}</p>
{{else}}
<p><em>No documentation available</em></p>
<p><em>No documentation available</em></p>
{{end}}
{{end}}
{{end}}