version: 1.10

package doc

import "go/doc"

Overview

Package doc extracts source code documentation from a Go AST.

Index

Package files

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

Variables

  1. var IllegalPrefixes = []string{
  2. "copyright",
  3. "all rights",
  4. "author",
  5. }

func

Examples returns the examples found in the files, sorted by Name field. The
Order fields record the order in which the examples were encountered.

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:

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

func

IsPredeclared reports whether s is a predeclared identifier.

func

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.

func

ToHTML converts comment text to formatted HTML. The comment was prepared by
DocReader, so it is known not to have leading, trailing blank lines nor to have
trailing spaces at the end of lines. The comment markers have already been
removed.

Each span of unindented non-blank lines is converted into a single paragraph.
There is one exception to the rule: a span that consists of a single line, is
followed by another paragraph span, begins with a capital letter, and contains
no punctuation is formatted as a heading.

A span of indented lines is converted into a

  1. block, with the common indent
    prefix removed.
  2. URLs in the comment text are converted into links; if the URL also appears in
    the words map, the link is taken from the map (if the corresponding map value is
    the empty string, the URL is not converted into a link).

  3. Go identifiers that appear in the words map are italicized; if the corresponding
    map value is not the empty string, it is considered a URL and the word is
    converted into a link.

  4. func ToText(w io.Writer, text string, indent, preIndent string, width int)
  5.  
  6. ToText prepares comment text for presentation in textual output. It wraps
    paragraphs of text to width or fewer Unicode code points and then prefixes each
    line with the indent. In preformatted sections (such as program text), it
    prefixes each non-blank line with preIndent.

  7. type Example struct {
  8.     Name        string // name of the item being exemplified
  9.     Doc         string // example function doc string
  10.     Code        ast.Node
  11.     Play        *ast.File // a whole program version of the example
  12.     Comments    []*ast.CommentGroup
  13.     Output      string // expected output
  14.     Unordered   bool
  15.     EmptyOutput bool // expect empty output
  16.     Order       int  // original source code order
  17. }
  18.  
  19. An Example represents an example function found in a source files.

  20. type Filter func(string) bool
  21.  
  22.  
  23. type Func struct {
  24.     Decl *ast.FuncDecl
  25.     // methods
  26.     // (for functions, these fields have the respective zero value)
  27.     Recv  string // actual   receiver "T" or "*T"
  28.     Orig  string // original receiver "T" or "*T"
  29.     Level int    // embedding level; 0 means not embedded
  30. }
  31.  
  32. Func is the documentation for a func declaration.

  33. type Mode int
  34.  
  35. Mode values control the operation of New.

  36. const (
  37.     // extract documentation for all package-level declarations,
  38.     // not just exported ones
  39.     AllDecls Mode = 1 << iota
  40.     // show all embedded methods, not just the ones of
  41.     // invisible (unexported) anonymous fields
  42.     AllMethods
  43. )
  44.  
  45.  
  46. type Note struct {
  47.     Pos, End token.Pos // position range of the comment containing the marker
  48.     UID      string    // uid found with the marker
  49.     Body     string    // note body text
  50. }
  51.  
  52. 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.

  53. type Package struct {
  54.     Doc        string
  55.     Name       string
  56.     ImportPath string
  57.     Imports    []string
  58.     Filenames  []string
  59.     Notes      map[string][]*Note
  60.     // Deprecated: For backward compatibility Bugs is still populated,
  61.     // but all new code should use Notes instead.
  62.     Bugs []string
  63.     // declarations
  64.     Consts []*Value
  65.     Types  []*Type
  66.     Vars   []*Value
  67.     Funcs  []*Func
  68. }
  69.  
  70. Package is the documentation for an entire package.

  71. func New

  72. func New(pkg *ast.Package, importPath string, mode Mode) *Package
  73.  
  74. New computes the package documentation for the given package AST. New takes
    ownership of the AST pkg and may edit or overwrite it.

  75. func (*Package) Filter

  76. func (p *Package) Filter(f Filter)
  77.  
  78. Filter eliminates documentation for names that don’t pass through the filter f.
    TODO(gri): Recognize “Type.Method” as a name.

  79. type Type struct {
  80.     Decl *ast.GenDecl
  81.     // associated declarations
  82.     Consts  []*Value // sorted list of constants of (mostly) this type
  83.     Vars    []*Value // sorted list of variables of (mostly) this type
  84.     Funcs   []*Func  // sorted list of functions returning this type
  85.     Methods []*Func  // sorted list of methods (including embedded ones) of this type
  86. }
  87.  
  88. Type is the documentation for a type declaration.

  89. type Value struct {
  90.     Doc   string
  91.     Names []string // var or const names in declaration order
  92.     Decl  *ast.GenDecl
  93.     // contains filtered or unexported fields
  94. }
  95.  
  96. Value is the documentation for a (possibly grouped) var or const declaration.