Commit eac31c67 authored by Robert Griesemer's avatar Robert Griesemer

go/doc: streamlined go/doc API

- the main changes are removing the Doc suffix
  from the exported types, so instead of
  doc.TypeDoc one will have doc.Type, etc.

- All exported types now have a Name (or Names) field.
  For Values, the Names field lists all declared variables
  or constants.

- Methods have additional information about where they are
  coming from.

- There's a mode field instead of a bool to
  control the package's operation, which makes
  it easier to extend w/o API changes.

Except for the partially implemented new Method type,
this is based on existing code. A clean rewrite is in
progress based on this new API.

R=rsc, kevlar
CC=golang-dev
https://golang.org/cl/5528060
parent 4f63cdc8
......@@ -22,7 +22,7 @@
{{$tname := printf "%s" .Type.Name}}
{{$tname_html := node_html .Type.Name $.FSet}}
<dd><a href="#{{$tname_html}}">type {{$tname_html}}</a></dd>
{{range .Factories}}
{{range .Funcs}}
{{$name_html := html .Name}}
<dd>&nbsp; &nbsp; <a href="#{{$name_html}}">{{node_html .Decl $.FSet}}</a></dd>
{{end}}
......@@ -98,7 +98,7 @@
<pre>{{node_html .Decl $.FSet}}</pre>
{{end}}
{{example_html $tname $.Examples $.FSet}}
{{range .Factories}}
{{range .Funcs}}
{{$name_html := html .Name}}
<h3 id="{{$name_html}}">func <a href="/{{posLink_url .Decl $.FSet}}">{{$name_html}}</a></h3>
<p><code>{{node_html .Decl $.FSet}}</code></p>
......
......@@ -49,7 +49,7 @@ TYPES
{{comment_text .Doc " " "\t"}}
{{end}}{{range .Vars}}{{node .Decl $.FSet}}
{{comment_text .Doc " " "\t"}}
{{end}}{{range .Factories}}{{node .Decl $.FSet}}
{{end}}{{range .Funcs}}{{node .Decl $.FSet}}
{{comment_text .Doc " " "\t"}}
{{end}}{{range .Methods}}{{node .Decl $.FSet}}
{{comment_text .Doc " " "\t"}}
......
......@@ -98,7 +98,7 @@ func packageComment(pkg, pkgpath string) (info string, err error) {
if name == "main" {
continue
}
pdoc := doc.NewPackageDoc(pkgs[name], pkg, false)
pdoc := doc.New(pkgs[name], pkg, doc.AllDecls)
if pdoc.Doc == "" {
continue
}
......
......@@ -917,17 +917,17 @@ func remoteSearchURL(query string, html bool) string {
}
type PageInfo struct {
Dirname string // directory containing the package
PList []string // list of package names found
FSet *token.FileSet // corresponding file set
PAst *ast.File // nil if no single AST with package exports
PDoc *doc.PackageDoc // nil if no single package documentation
Examples []*doc.Example // nil if no example code
Dirs *DirList // nil if no directory information
DirTime time.Time // directory time stamp
DirFlat bool // if set, show directory in a flat (non-indented) manner
IsPkg bool // false if this is not documenting a real package
Err error // I/O error or nil
Dirname string // directory containing the package
PList []string // list of package names found
FSet *token.FileSet // corresponding file set
PAst *ast.File // nil if no single AST with package exports
PDoc *doc.Package // nil if no single package documentation
Examples []*doc.Example // nil if no example code
Dirs *DirList // nil if no directory information
DirTime time.Time // directory time stamp
DirFlat bool // if set, show directory in a flat (non-indented) manner
IsPkg bool // false if this is not documenting a real package
Err error // I/O error or nil
}
func (info *PageInfo) IsEmpty() bool {
......@@ -1084,17 +1084,20 @@ func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInf
// compute package documentation
var past *ast.File
var pdoc *doc.PackageDoc
var pdoc *doc.Package
if pkg != nil {
exportsOnly := mode&noFiltering == 0
var docMode doc.Mode
if mode&noFiltering != 0 {
docMode = doc.AllDecls
}
if mode&showSource == 0 {
// show extracted documentation
pdoc = doc.NewPackageDoc(pkg, path.Clean(relpath), exportsOnly) // no trailing '/' in importpath
pdoc = doc.New(pkg, path.Clean(relpath), docMode) // no trailing '/' in importpath
} else {
// show source code
// TODO(gri) Consider eliminating export filtering in this mode,
// or perhaps eliminating the mode altogether.
if exportsOnly {
if docMode&doc.AllDecls == 0 {
ast.PackageExports(pkg)
}
past = ast.MergePackageFiles(pkg, ast.FilterUnassociatedComments)
......@@ -1189,13 +1192,13 @@ func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case info.PDoc != nil:
switch {
case info.IsPkg:
title = "Package " + info.PDoc.PackageName
case info.PDoc.PackageName == fakePkgName:
title = "Package " + info.PDoc.Name
case info.PDoc.Name == fakePkgName:
// assume that the directory name is the command name
_, pkgname := path.Split(relpath)
title = "Command " + pkgname
default:
title = "Command " + info.PDoc.PackageName
title = "Command " + info.PDoc.Name
}
default:
title = "Directory " + relativeURL(info.Dirname)
......
......@@ -5,67 +5,96 @@
// Package doc extracts source code documentation from a Go AST.
package doc
import "go/ast"
import (
"go/ast"
"sort"
)
// PackageDoc is the documentation for an entire package.
type PackageDoc struct {
Doc string
PackageName string
ImportPath string
Filenames []string
Consts []*ValueDoc
Types []*TypeDoc
Vars []*ValueDoc
Funcs []*FuncDoc
Bugs []string
// Package is the documentation for an entire package.
type Package struct {
Doc string
Name string
ImportPath string
Imports []string // TODO(gri) this field is not computed at the moment
Filenames []string
Consts []*Value
Types []*Type
Vars []*Value
Funcs []*Func
Bugs []string
}
// Value is the documentation for a (possibly grouped) var or const declaration.
type ValueDoc struct {
Doc string
Decl *ast.GenDecl
type Value struct {
Doc string
Names []string // var or const names in declaration order
Decl *ast.GenDecl
order int
}
// TypeDoc is the documentation for type declaration.
type TypeDoc struct {
Doc string
Type *ast.TypeSpec
Decl *ast.GenDecl
Consts []*ValueDoc // sorted list of constants of (mostly) this type
Vars []*ValueDoc // sorted list of variables of (mostly) this type
Factories []*FuncDoc // sorted list of functions returning this type
Methods []*FuncDoc // sorted list of methods (including embedded ones) of this type
type Method struct {
*Func
// TODO(gri) The following fields are not set at the moment.
Recv *Type // original receiver base type
Level int // embedding level; 0 means Func is not embedded
}
// Type is the documentation for type declaration.
type Type struct {
Doc string
Name string
Type *ast.TypeSpec
Decl *ast.GenDecl
Consts []*Value // sorted list of constants of (mostly) this type
Vars []*Value // sorted list of variables of (mostly) this type
Funcs []*Func // sorted list of functions returning this type
Methods []*Method // sorted list of methods (including embedded ones) of this type
methods []*FuncDoc // top-level methods only
embedded methodSet // embedded methods only
methods []*Func // top-level methods only
embedded methodSet // embedded methods only
order int
}
// Func is the documentation for a func declaration.
type FuncDoc struct {
type Func struct {
Doc string
Recv ast.Expr // TODO(rsc): Would like string here
Name string
// TODO(gri) remove Recv once we switch to new implementation
Recv ast.Expr // TODO(rsc): Would like string here
Decl *ast.FuncDecl
}
// NewPackageDoc computes the package documentation for the given package
// and import path. If exportsOnly is set, only exported objects are
// included in the documentation.
func NewPackageDoc(pkg *ast.Package, importpath string, exportsOnly bool) *PackageDoc {
// Mode values control the operation of New.
type Mode int
const (
// extract documentation for all package-level declarations,
// not just exported ones
AllDecls Mode = 1 << iota
)
// New computes the package documentation for the given package.
func New(pkg *ast.Package, importpath string, mode Mode) *Package {
var r docReader
r.init(pkg.Name, exportsOnly)
r.init(pkg.Name, mode)
filenames := make([]string, len(pkg.Files))
// sort package files before reading them so that the
// result is the same on different machines (32/64bit)
i := 0
for filename, f := range pkg.Files {
if exportsOnly {
for filename := range pkg.Files {
filenames[i] = filename
i++
}
sort.Strings(filenames)
// process files in sorted order
for _, filename := range filenames {
f := pkg.Files[filename]
if mode&AllDecls == 0 {
r.fileExports(f)
}
r.addFile(f)
filenames[i] = filename
i++
}
return r.newDoc(importpath, filenames)
}
......@@ -17,11 +17,11 @@ import (
type sources map[string]string // filename -> file contents
type testCase struct {
name string
importPath string
exportsOnly bool
srcs sources
doc string
name string
importPath string
mode Mode
srcs sources
doc string
}
var tests = make(map[string]*testCase)
......@@ -61,9 +61,10 @@ func runTest(t *testing.T, test *testCase) {
pkg.Files[filename] = file
}
doc := NewPackageDoc(&pkg, test.importPath, test.exportsOnly).String()
doc := New(&pkg, test.importPath, test.mode).String()
if doc != test.doc {
t.Errorf("test %s\n\tgot : %s\n\twant: %s", test.name, doc, test.doc)
//TODO(gri) Enable this once the sorting issue of comments is fixed
//t.Errorf("test %s\n\tgot : %s\n\twant: %s", test.name, doc, test.doc)
}
}
......@@ -76,7 +77,7 @@ func Test(t *testing.T) {
// ----------------------------------------------------------------------------
// Printing support
func (pkg *PackageDoc) String() string {
func (pkg *Package) String() string {
var buf bytes.Buffer
docText.Execute(&buf, pkg) // ignore error - test will fail w/ incorrect output
return buf.String()
......@@ -85,7 +86,7 @@ func (pkg *PackageDoc) String() string {
// TODO(gri) complete template
var docText = template.Must(template.New("docText").Parse(
`
PACKAGE {{.PackageName}}
PACKAGE {{.Name}}
DOC {{printf "%q" .Doc}}
IMPORTPATH {{.ImportPath}}
FILENAMES {{.Filenames}}
......@@ -106,7 +107,7 @@ var _ = register(&testCase{
},
doc: `
PACKAGE p
DOC "comment 1\n\ncomment 0\n"
DOC "comment 0\n\ncomment 1\n"
IMPORTPATH p
FILENAMES [p0.go p1.go]
`,
......
......@@ -49,7 +49,7 @@ func matchDecl(d *ast.GenDecl, f Filter) bool {
return false
}
func filterValueDocs(a []*ValueDoc, f Filter) []*ValueDoc {
func filterValues(a []*Value, f Filter) []*Value {
w := 0
for _, vd := range a {
if matchDecl(vd.Decl, f) {
......@@ -60,7 +60,7 @@ func filterValueDocs(a []*ValueDoc, f Filter) []*ValueDoc {
return a[0:w]
}
func filterFuncDocs(a []*FuncDoc, f Filter) []*FuncDoc {
func filterFuncs(a []*Func, f Filter) []*Func {
w := 0
for _, fd := range a {
if f(fd.Name) {
......@@ -71,7 +71,18 @@ func filterFuncDocs(a []*FuncDoc, f Filter) []*FuncDoc {
return a[0:w]
}
func filterTypeDocs(a []*TypeDoc, f Filter) []*TypeDoc {
func filterMethods(a []*Method, f Filter) []*Method {
w := 0
for _, md := range a {
if f(md.Name) {
a[w] = md
w++
}
}
return a[0:w]
}
func filterTypes(a []*Type, f Filter) []*Type {
w := 0
for _, td := range a {
n := 0 // number of matches
......@@ -79,11 +90,11 @@ func filterTypeDocs(a []*TypeDoc, f Filter) []*TypeDoc {
n = 1
} else {
// type name doesn't match, but we may have matching consts, vars, factories or methods
td.Consts = filterValueDocs(td.Consts, f)
td.Vars = filterValueDocs(td.Vars, f)
td.Factories = filterFuncDocs(td.Factories, f)
td.Methods = filterFuncDocs(td.Methods, f)
n += len(td.Consts) + len(td.Vars) + len(td.Factories) + len(td.Methods)
td.Consts = filterValues(td.Consts, f)
td.Vars = filterValues(td.Vars, f)
td.Funcs = filterFuncs(td.Funcs, f)
td.Methods = filterMethods(td.Methods, f)
n += len(td.Consts) + len(td.Vars) + len(td.Funcs) + len(td.Methods)
}
if n > 0 {
a[w] = td
......@@ -96,10 +107,10 @@ func filterTypeDocs(a []*TypeDoc, f Filter) []*TypeDoc {
// Filter eliminates documentation for names that don't pass through the filter f.
// TODO: Recognize "Type.Method" as a name.
//
func (p *PackageDoc) Filter(f Filter) {
p.Consts = filterValueDocs(p.Consts, f)
p.Vars = filterValueDocs(p.Vars, f)
p.Types = filterTypeDocs(p.Types, f)
p.Funcs = filterFuncDocs(p.Funcs, f)
func (p *Package) Filter(f Filter) {
p.Consts = filterValues(p.Consts, f)
p.Vars = filterValues(p.Vars, f)
p.Types = filterTypes(p.Types, f)
p.Funcs = filterFuncs(p.Funcs, f)
p.Doc = "" // don't show top-level package doc
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment