mirror of
https://github.com/golang/go
synced 2024-11-20 10:14:43 -07:00
godoc: show comments in various filtered views
Fixes #3454. R=rsc CC=golang-dev https://golang.org/cl/6305069
This commit is contained in:
parent
f5f23e075e
commit
8140cd9149
@ -866,6 +866,19 @@ func inList(name string, list []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// packageExports is a local implementation of ast.PackageExports
|
||||||
|
// which correctly updates each package file's comment list.
|
||||||
|
// (The ast.PackageExports signature is frozen, hence the local
|
||||||
|
// implementation).
|
||||||
|
//
|
||||||
|
func packageExports(fset *token.FileSet, pkg *ast.Package) {
|
||||||
|
for _, src := range pkg.Files {
|
||||||
|
cmap := ast.NewCommentMap(fset, src)
|
||||||
|
ast.FileExports(src)
|
||||||
|
src.Comments = cmap.Filter(src).Comments()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// getPageInfo returns the PageInfo for a package directory abspath. If the
|
// getPageInfo returns the PageInfo for a package directory abspath. If the
|
||||||
// parameter genAST is set, an AST containing only the package exports is
|
// parameter genAST is set, an AST containing only the package exports is
|
||||||
// computed (PageInfo.PAst), otherwise package documentation (PageInfo.Doc)
|
// computed (PageInfo.PAst), otherwise package documentation (PageInfo.Doc)
|
||||||
@ -1012,9 +1025,9 @@ func (h *docServer) getPageInfo(abspath, relpath, pkgname string, mode PageInfoM
|
|||||||
// TODO(gri) Consider eliminating export filtering in this mode,
|
// TODO(gri) Consider eliminating export filtering in this mode,
|
||||||
// or perhaps eliminating the mode altogether.
|
// or perhaps eliminating the mode altogether.
|
||||||
if mode&noFiltering == 0 {
|
if mode&noFiltering == 0 {
|
||||||
ast.PackageExports(pkg)
|
packageExports(fset, pkg)
|
||||||
}
|
}
|
||||||
past = ast.MergePackageFiles(pkg, ast.FilterUnassociatedComments)
|
past = ast.MergePackageFiles(pkg, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +36,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/build"
|
"go/build"
|
||||||
|
"go/printer"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -424,20 +425,24 @@ func main() {
|
|||||||
filter := func(s string) bool { return rx.MatchString(s) }
|
filter := func(s string) bool { return rx.MatchString(s) }
|
||||||
switch {
|
switch {
|
||||||
case info.PAst != nil:
|
case info.PAst != nil:
|
||||||
|
cmap := ast.NewCommentMap(info.FSet, info.PAst)
|
||||||
ast.FilterFile(info.PAst, filter)
|
ast.FilterFile(info.PAst, filter)
|
||||||
// Special case: Don't use templates for printing
|
// Special case: Don't use templates for printing
|
||||||
// so we only get the filtered declarations without
|
// so we only get the filtered declarations without
|
||||||
// package clause or extra whitespace.
|
// package clause or extra whitespace.
|
||||||
for i, d := range info.PAst.Decls {
|
for i, d := range info.PAst.Decls {
|
||||||
|
// determine the comments associated with d only
|
||||||
|
comments := cmap.Filter(d).Comments()
|
||||||
|
cn := &printer.CommentedNode{Node: d, Comments: comments}
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
if *html {
|
if *html {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
writeNode(&buf, info.FSet, d)
|
writeNode(&buf, info.FSet, cn)
|
||||||
FormatText(os.Stdout, buf.Bytes(), -1, true, "", nil)
|
FormatText(os.Stdout, buf.Bytes(), -1, true, "", nil)
|
||||||
} else {
|
} else {
|
||||||
writeNode(os.Stdout, info.FSet, d)
|
writeNode(os.Stdout, info.FSet, cn)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user