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:
parent
a28efa5d8c
commit
27d851fc2c
|
@ -132,7 +132,7 @@ const removeDuplicates = true
|
||||||
|
|
||||||
// A KindRun is a run of SpotInfos of the same kind in a given file.
|
// 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
|
// 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
|
type KindRun []SpotInfo
|
||||||
|
|
||||||
// KindRuns are sorted by line number or index. Since the isIndex bit
|
// 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" {
|
if pkgName == "main" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
dirname = pathpkg.Clean(dirname)
|
||||||
astPkg := ast.Package{
|
astPkg := ast.Package{
|
||||||
Name: pkgName,
|
Name: pkgName,
|
||||||
Files: map[string]*ast.File{
|
Files: map[string]*ast.File{
|
||||||
|
@ -705,18 +706,35 @@ func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
var m doc.Mode
|
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) {
|
addIdent := func(sk SpotKind, name string, docstr string) {
|
||||||
if x.idents[sk] == nil {
|
if x.idents[sk] == nil {
|
||||||
x.idents[sk] = make(map[string][]Ident)
|
x.idents[sk] = make(map[string][]Ident)
|
||||||
}
|
}
|
||||||
x.idents[sk][name] = append(x.idents[sk][name], Ident{
|
x.idents[sk][name] = append(x.idents[sk][name], Ident{
|
||||||
Path: pathpkg.Clean(dirname),
|
Path: dirname,
|
||||||
Package: pkgName,
|
Package: pkgName,
|
||||||
Name: name,
|
Name: name,
|
||||||
Doc: doc.Synopsis(docstr),
|
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 _, c := range docPkg.Consts {
|
||||||
for _, name := range c.Names {
|
for _, name := range c.Names {
|
||||||
addIdent(ConstDecl, name, c.Doc)
|
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
|
// Test files are already filtered out in visitFile if IndexGoCode and
|
||||||
// IndexFullText are false. Otherwise, check here.
|
// IndexFullText are false. Otherwise, check here.
|
||||||
isTestFile := (x.c.IndexGoCode || x.c.IndexFullText) &&
|
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 {
|
if !isTestFile {
|
||||||
x.indexDocs(dirname, filename, astFile)
|
x.indexDocs(dirname, filename, astFile)
|
||||||
}
|
}
|
||||||
|
@ -816,7 +834,7 @@ func (x *Indexer) visitFile(dirname string, fi os.FileInfo) {
|
||||||
case x.c.IndexDocs:
|
case x.c.IndexDocs:
|
||||||
if !goFile ||
|
if !goFile ||
|
||||||
strings.HasSuffix(fi.Name(), "_test.go") ||
|
strings.HasSuffix(fi.Name(), "_test.go") ||
|
||||||
strings.HasPrefix(dirname, "test/") {
|
strings.HasPrefix(dirname, "/test/") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -4,6 +4,19 @@
|
||||||
license that can be found in the LICENSE file.
|
license that can be found in the LICENSE file.
|
||||||
-->
|
-->
|
||||||
{{$query_url := urlquery .Query}}
|
{{$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 .Hit}}
|
||||||
{{with .Decls}}
|
{{with .Decls}}
|
||||||
<h2 id="Global">Package-level declarations</h2>
|
<h2 id="Global">Package-level declarations</h2>
|
||||||
|
|
|
@ -3,28 +3,21 @@
|
||||||
Use of this source code is governed by a BSD-style
|
Use of this source code is governed by a BSD-style
|
||||||
license that can be found in the LICENSE file.
|
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}}
|
{{range $key, $val := .Idents}}
|
||||||
{{if $val}}
|
{{if $val}}
|
||||||
<h2 id="Global">{{$key.Name}}</h2>
|
<h2 id="Global">{{$key.Name}}</h2>
|
||||||
{{range $val}}
|
{{range $val}}
|
||||||
{{$pkg_html := pkgLink .Path | html}}
|
{{$pkg_html := pkgLink .Path | html}}
|
||||||
{{$doc_html := docLink .Path .Name| html}}
|
{{if eq "Packages" $key.Name}}
|
||||||
<a href="/{{$pkg_html}}">{{html .Package}}</a>.<a href="{{$doc_html}}">{{.Name}}</a>
|
<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}}
|
{{if .Doc}}
|
||||||
<p>{{comment_html .Doc}}</p>
|
<p>{{comment_html .Doc}}</p>
|
||||||
{{else}}
|
{{else}}
|
||||||
<p><em>No documentation available</em></p>
|
<p><em>No documentation available</em></p>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -1447,6 +1447,19 @@ function PlaygroundOutput(el) {
|
||||||
license that can be found in the LICENSE file.
|
license that can be found in the LICENSE file.
|
||||||
-->
|
-->
|
||||||
{{$query_url := urlquery .Query}}
|
{{$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 .Hit}}
|
||||||
{{with .Decls}}
|
{{with .Decls}}
|
||||||
<h2 id="Global">Package-level declarations</h2>
|
<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
|
Use of this source code is governed by a BSD-style
|
||||||
license that can be found in the LICENSE file.
|
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}}
|
{{range $key, $val := .Idents}}
|
||||||
{{if $val}}
|
{{if $val}}
|
||||||
<h2 id="Global">{{$key.Name}}</h2>
|
<h2 id="Global">{{$key.Name}}</h2>
|
||||||
{{range $val}}
|
{{range $val}}
|
||||||
{{$pkg_html := pkgLink .Path | html}}
|
{{$pkg_html := pkgLink .Path | html}}
|
||||||
{{$doc_html := docLink .Path .Name| html}}
|
{{if eq "Packages" $key.Name}}
|
||||||
<a href="/{{$pkg_html}}">{{html .Package}}</a>.<a href="{{$doc_html}}">{{.Name}}</a>
|
<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}}
|
{{if .Doc}}
|
||||||
<p>{{comment_html .Doc}}</p>
|
<p>{{comment_html .Doc}}</p>
|
||||||
{{else}}
|
{{else}}
|
||||||
<p><em>No documentation available</em></p>
|
<p><em>No documentation available</em></p>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
Loading…
Reference in New Issue