pkgsite: golang.org/x/pkgsite/internal/fetch/internal/doc Index | Examples | Files

package doc

import "golang.org/x/pkgsite/internal/fetch/internal/doc"

Package doc extracts source code documentation from a Go AST.

This is a temporary copy of go/doc from Go 1.14 standard library for testing of and validation of CL 204830 (golang.org/cl/204830). Go 1.14 has been released, so go/doc can be used in its place as soon as Go 1.14 is used for deployment.

This package shouldn't be modified unless it's a part of a plan to make an upstream change in go/doc package in the standard library.

Index

Examples

Package Files

doc.go example.go exports.go filter.go reader.go synopsis.go

Variables

var IllegalPrefixes = []string{
    "copyright",
    "all rights",
    "author",
}

func IsPredeclared Uses

func IsPredeclared(s string) bool

IsPredeclared reports whether s is a predeclared identifier.

func Synopsis Uses

func Synopsis(s string) string

Synopsis returns a cleaned version of the first sentence in s. That sentence ends after the first period followed by space and not preceded by exactly one uppercase letter. The result string has no \n, \r, or \t characters and uses only single spaces between words. If s starts with any of the IllegalPrefixes, the result is the empty string.

type Example Uses

type Example struct {
    Name        string // name of the item being exemplified (including optional suffix)
    Suffix      string // example suffix, without leading '_' (only populated by NewFromFiles)
    Doc         string // example function doc string
    Code        ast.Node
    Play        *ast.File // a whole program version of the example
    Comments    []*ast.CommentGroup
    Output      string // expected output
    Unordered   bool
    EmptyOutput bool // expect empty output
    Order       int  // original source code order
}

An Example represents an example function found in a test source file.

func Examples Uses

func Examples(testFiles ...*ast.File) []*Example

Examples returns the examples found in testFiles, sorted by Name field. The Order fields record the order in which the examples were encountered. The Suffix field is not populated when Examples is called directly, it is only populated by NewFromFiles for examples it finds in _test.go files.

Playable Examples must be in a package whose name ends in "_test". An Example is "playable" (the Play field is non-nil) in either of these circumstances:

- The example function is self-contained: the function references only
  identifiers from other packages (or predeclared identifiers, such as
  "int") and the test file does not include a dot import.
- The entire test file is the example: the file contains exactly one
  example function, zero test or benchmark functions, and at least one
  top-level function, type, variable, or constant declaration other
  than the example function.

type Filter Uses

type Filter func(string) bool

type Func Uses

type Func struct {
    Doc  string
    Name string
    Decl *ast.FuncDecl

    // methods
    // (for functions, these fields have the respective zero value)
    Recv  string // actual   receiver "T" or "*T"
    Orig  string // original receiver "T" or "*T"
    Level int    // embedding level; 0 means not embedded

    // Examples is a sorted list of examples associated with this
    // function or method. Examples are extracted from _test.go files
    // provided to NewFromFiles.
    Examples []*Example
}

Func is the documentation for a func declaration.

type Mode Uses

type Mode int

Mode values control the operation of New and NewFromFiles.

const (
    // AllDecls says to extract documentation for all package-level
    // declarations, not just exported ones.
    AllDecls Mode = 1 << iota

    // AllMethods says to show all embedded methods, not just the ones of
    // invisible (unexported) anonymous fields.
    AllMethods

    // PreserveAST says to leave the AST unmodified. Originally, pieces of
    // the AST such as function bodies were nil-ed out to save memory in
    // godoc, but not all programs want that behavior.
    PreserveAST
)

type Note Uses

type Note struct {
    Pos, End token.Pos // position range of the comment containing the marker
    UID      string    // uid found with the marker
    Body     string    // note body text
}

A Note represents a marked comment starting with "MARKER(uid): note body". Any note with a marker of 2 or more upper case [A-Z] letters and a uid of at least one character is recognized. The ":" following the uid is optional. Notes are collected in the Package.Notes map indexed by the notes marker.

type Package Uses

type Package struct {
    Doc        string
    Name       string
    ImportPath string
    Imports    []string
    Filenames  []string
    Notes      map[string][]*Note

    // Deprecated: For backward compatibility Bugs is still populated,
    // but all new code should use Notes instead.
    Bugs []string

    // declarations
    Consts []*Value
    Types  []*Type
    Vars   []*Value
    Funcs  []*Func

    // Examples is a sorted list of examples associated with
    // the package. Examples are extracted from _test.go files
    // provided to NewFromFiles.
    Examples []*Example
}

Package is the documentation for an entire package.

func New Uses

func New(pkg *ast.Package, importPath string, mode Mode) *Package

New computes the package documentation for the given package AST. New takes ownership of the AST pkg and may edit or overwrite it. To have the Examples fields populated, use NewFromFiles and include the package's _test.go files.

func NewFromFiles Uses

func NewFromFiles(fset *token.FileSet, files []*ast.File, importPath string, opts ...interface{}) (*Package, error)

NewFromFiles computes documentation for a package.

The package is specified by a list of *ast.Files and corresponding file set, which must not be nil. NewFromFiles does not skip files based on build constraints, so it is the caller's responsibility to provide only the files that are matched by the build context. The import path of the package is specified by importPath.

Examples found in _test.go files are associated with the corresponding type, function, method, or the package, based on their name. If the example has a suffix in its name, it is set in the Example.Suffix field. Examples with malformed names are skipped.

Optionally, a single extra argument of type Mode can be provided to control low-level aspects of the documentation extraction behavior.

NewFromFiles takes ownership of the AST files and may edit them, unless the PreserveAST Mode bit is on.

This example illustrates how to use NewFromFiles to compute package documentation with examples.

Code:

// src and test are two source files that make up
// a package whose documentation will be computed.
const src = `
// This is the package comment.
package p

import "fmt"

// This comment is associated with the Greet function.
func Greet(who string) {
	fmt.Printf("Hello, %s!\n", who)
}
`
const test = `
package p_test

// This comment is associated with the ExampleGreet_world example.
func ExampleGreet_world() {
	Greet("world")
}
`

// Create the AST by parsing src and test.
fset := token.NewFileSet()
files := []*ast.File{
    mustParse(fset, "src.go", src),
    mustParse(fset, "src_test.go", test),
}

// Compute package documentation with examples.
p, err := doc.NewFromFiles(fset, files, "example.com/p")
if err != nil {
    panic(err)
}

fmt.Printf("package %s - %s", p.Name, p.Doc)
fmt.Printf("func %s - %s", p.Funcs[0].Name, p.Funcs[0].Doc)
fmt.Printf(" ⤷ example with suffix %q - %s", p.Funcs[0].Examples[0].Suffix, p.Funcs[0].Examples[0].Doc)

Output:

package p - This is the package comment.
func Greet - This comment is associated with the Greet function.
 ⤷ example with suffix "world" - This comment is associated with the ExampleGreet_world example.

func (*Package) Filter Uses

func (p *Package) Filter(f Filter)

Filter eliminates documentation for names that don't pass through the filter f. TODO(gri): Recognize "Type.Method" as a name.

type Type Uses

type Type struct {
    Doc  string
    Name string
    Decl *ast.GenDecl

    // associated declarations
    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 []*Func  // sorted list of methods (including embedded ones) of this type

    // Examples is a sorted list of examples associated with
    // this type. Examples are extracted from _test.go files
    // provided to NewFromFiles.
    Examples []*Example
}

Type is the documentation for a type declaration.

type Value Uses

type Value struct {
    Doc   string
    Names []string // var or const names in declaration order
    Decl  *ast.GenDecl
    // contains filtered or unexported fields
}

Value is the documentation for a (possibly grouped) var or const declaration.

Package doc imports 11 packages (graph) and is imported by 5 packages. Updated 2020-08-05. Refresh now. Tools for package owners.