mirror of
https://github.com/golang/go
synced 2024-11-22 05:14:40 -07:00
go/doc: collect imports
R=r CC=golang-dev https://golang.org/cl/5556051
This commit is contained in:
parent
f47807a57f
commit
c109705c6f
@ -15,7 +15,7 @@ type Package struct {
|
|||||||
Doc string
|
Doc string
|
||||||
Name string
|
Name string
|
||||||
ImportPath string
|
ImportPath string
|
||||||
Imports []string // TODO(gri) this field is not computed at the moment
|
Imports []string
|
||||||
Filenames []string
|
Filenames []string
|
||||||
Consts []*Value
|
Consts []*Value
|
||||||
Types []*Type
|
Types []*Type
|
||||||
|
@ -124,6 +124,9 @@ func (doc *docReader) filterType(tinfo *typeInfo, typ ast.Expr) bool {
|
|||||||
|
|
||||||
func (doc *docReader) filterSpec(spec ast.Spec) bool {
|
func (doc *docReader) filterSpec(spec ast.Spec) bool {
|
||||||
switch s := spec.(type) {
|
switch s := spec.(type) {
|
||||||
|
case *ast.ImportSpec:
|
||||||
|
// always keep imports so we can collect them
|
||||||
|
return true
|
||||||
case *ast.ValueSpec:
|
case *ast.ValueSpec:
|
||||||
s.Names = filterIdentList(s.Names)
|
s.Names = filterIdentList(s.Names)
|
||||||
if len(s.Names) > 0 {
|
if len(s.Names) > 0 {
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"go/token"
|
"go/token"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@ -55,6 +56,7 @@ type docReader struct {
|
|||||||
doc *ast.CommentGroup // package documentation, if any
|
doc *ast.CommentGroup // package documentation, if any
|
||||||
pkgName string
|
pkgName string
|
||||||
mode Mode
|
mode Mode
|
||||||
|
imports map[string]int
|
||||||
values []*ast.GenDecl // consts and vars
|
values []*ast.GenDecl // consts and vars
|
||||||
types map[string]*typeInfo
|
types map[string]*typeInfo
|
||||||
embedded map[string]*typeInfo // embedded types, possibly not exported
|
embedded map[string]*typeInfo // embedded types, possibly not exported
|
||||||
@ -65,6 +67,7 @@ type docReader struct {
|
|||||||
func (doc *docReader) init(pkgName string, mode Mode) {
|
func (doc *docReader) init(pkgName string, mode Mode) {
|
||||||
doc.pkgName = pkgName
|
doc.pkgName = pkgName
|
||||||
doc.mode = mode
|
doc.mode = mode
|
||||||
|
doc.imports = make(map[string]int)
|
||||||
doc.types = make(map[string]*typeInfo)
|
doc.types = make(map[string]*typeInfo)
|
||||||
doc.embedded = make(map[string]*typeInfo)
|
doc.embedded = make(map[string]*typeInfo)
|
||||||
doc.funcs = make(map[string]*ast.FuncDecl)
|
doc.funcs = make(map[string]*ast.FuncDecl)
|
||||||
@ -244,6 +247,13 @@ func (doc *docReader) addDecl(decl ast.Decl) {
|
|||||||
case *ast.GenDecl:
|
case *ast.GenDecl:
|
||||||
if len(d.Specs) > 0 {
|
if len(d.Specs) > 0 {
|
||||||
switch d.Tok {
|
switch d.Tok {
|
||||||
|
case token.IMPORT:
|
||||||
|
// imports are handled individually
|
||||||
|
for _, spec := range d.Specs {
|
||||||
|
if import_, err := strconv.Unquote(spec.(*ast.ImportSpec).Path.Value); err == nil {
|
||||||
|
doc.imports[import_] = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
case token.CONST, token.VAR:
|
case token.CONST, token.VAR:
|
||||||
// constants and variables are always handled as a group
|
// constants and variables are always handled as a group
|
||||||
doc.addValue(d)
|
doc.addValue(d)
|
||||||
@ -346,6 +356,17 @@ func (doc *docReader) addFile(src *ast.File) {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Conversion to external representation
|
// Conversion to external representation
|
||||||
|
|
||||||
|
func (doc *docReader) makeImports() []string {
|
||||||
|
list := make([]string, len(doc.imports))
|
||||||
|
i := 0
|
||||||
|
for import_ := range doc.imports {
|
||||||
|
list[i] = import_
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
sort.Strings(list)
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
type sortValue []*Value
|
type sortValue []*Value
|
||||||
|
|
||||||
func (p sortValue) Len() int { return len(p) }
|
func (p sortValue) Len() int { return len(p) }
|
||||||
@ -661,6 +682,7 @@ func (doc *docReader) newDoc(importpath string, filenames []string) *Package {
|
|||||||
// doc.funcs and thus must be called before any other
|
// doc.funcs and thus must be called before any other
|
||||||
// function consuming those lists
|
// function consuming those lists
|
||||||
p.Types = doc.makeTypes(doc.types)
|
p.Types = doc.makeTypes(doc.types)
|
||||||
|
p.Imports = doc.makeImports()
|
||||||
p.Consts = makeValues(doc.values, token.CONST)
|
p.Consts = makeValues(doc.values, token.CONST)
|
||||||
p.Vars = makeValues(doc.values, token.VAR)
|
p.Vars = makeValues(doc.values, token.VAR)
|
||||||
p.Funcs = makeFuncs(doc.funcs)
|
p.Funcs = makeFuncs(doc.funcs)
|
||||||
|
3
src/pkg/go/doc/testdata/b.out
vendored
3
src/pkg/go/doc/testdata/b.out
vendored
@ -4,6 +4,9 @@ PACKAGE b
|
|||||||
IMPORTPATH
|
IMPORTPATH
|
||||||
testdata/b
|
testdata/b
|
||||||
|
|
||||||
|
IMPORTS
|
||||||
|
a
|
||||||
|
|
||||||
FILENAMES
|
FILENAMES
|
||||||
testdata/b.go
|
testdata/b.go
|
||||||
|
|
||||||
|
6
src/pkg/go/doc/testdata/template.txt
vendored
6
src/pkg/go/doc/testdata/template.txt
vendored
@ -4,10 +4,10 @@ PACKAGE {{.Name}}
|
|||||||
IMPORTPATH
|
IMPORTPATH
|
||||||
{{.ImportPath}}
|
{{.ImportPath}}
|
||||||
|
|
||||||
{{with .Imports}}
|
{{with .Imports}}IMPORTS
|
||||||
IMPORTS
|
|
||||||
{{range .}} {{.}}
|
{{range .}} {{.}}
|
||||||
{{end}}{{end}}{{/*
|
{{end}}
|
||||||
|
{{end}}{{/*
|
||||||
|
|
||||||
*/}}FILENAMES
|
*/}}FILENAMES
|
||||||
{{range .Filenames}} {{.}}
|
{{range .Filenames}} {{.}}
|
||||||
|
12
src/pkg/go/doc/testdata/testing.out
vendored
12
src/pkg/go/doc/testdata/testing.out
vendored
@ -4,6 +4,18 @@ PACKAGE testing
|
|||||||
IMPORTPATH
|
IMPORTPATH
|
||||||
testdata/testing
|
testdata/testing
|
||||||
|
|
||||||
|
IMPORTS
|
||||||
|
bytes
|
||||||
|
flag
|
||||||
|
fmt
|
||||||
|
io
|
||||||
|
os
|
||||||
|
runtime
|
||||||
|
runtime/pprof
|
||||||
|
strconv
|
||||||
|
strings
|
||||||
|
time
|
||||||
|
|
||||||
FILENAMES
|
FILENAMES
|
||||||
testdata/benchmark.go
|
testdata/benchmark.go
|
||||||
testdata/example.go
|
testdata/example.go
|
||||||
|
Loading…
Reference in New Issue
Block a user