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

  1. func Examples(files ...*ast.File) []*Example

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

  1. func IsPredeclared(s string) bool

IsPredeclared reports whether s is a predeclared identifier.

func Synopsis

  1. 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.

func ToHTML

  1. func ToHTML(w io.Writer, text string, words map[string]string)

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

  5. func ToText(w io.Writer, text string, indent, preIndent string, width int)
  6.  
  7. 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.

  8. type Example

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

  22. type Filter

  23. type Filter func(string) bool
  24.  
  25.  
  26. type Func

  27. type Func struct {
  28.     Doc  string
  29.     Name string
  30.     Decl *ast.FuncDecl
  31.     // methods
  32.     // (for functions, these fields have the respective zero value)
  33.     Recv  string // actual   receiver "T" or "*T"
  34.     Orig  string // original receiver "T" or "*T"
  35.     Level int    // embedding level; 0 means not embedded
  36. }
  37.  
  38. Func is the documentation for a func declaration.

  39. type Mode

  40. type Mode int
  41.  
  42. Mode values control the operation of New.

  43. const (
  44.     // extract documentation for all package-level declarations,
  45.     // not just exported ones
  46.     AllDecls Mode = 1 << iota
  47.     // show all embedded methods, not just the ones of
  48.     // invisible (unexported) anonymous fields
  49.     AllMethods
  50. )
  51.  
  52.  
  53. type Note

  54. type Note struct {
  55.     Pos, End token.Pos // position range of the comment containing the marker
  56.     UID      string    // uid found with the marker
  57.     Body     string    // note body text
  58. }
  59.  
  60. 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.

  61. type Package

  62. type Package struct {
  63.     Doc        string
  64.     Name       string
  65.     ImportPath string
  66.     Imports    []string
  67.     Filenames  []string
  68.     Notes      map[string][]*Note
  69.     // Deprecated: For backward compatibility Bugs is still populated,
  70.     // but all new code should use Notes instead.
  71.     Bugs []string
  72.     // declarations
  73.     Consts []*Value
  74.     Types  []*Type
  75.     Vars   []*Value
  76.     Funcs  []*Func
  77. }
  78.  
  79. Package is the documentation for an entire package.

  80. func New

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

  84. func (*Package) Filter

  85. func (p *Package) Filter(f Filter)
  86.  
  87. Filter eliminates documentation for names that dont pass through the filter f.
    TODO(gri): Recognize Type.Method as a name.

  88. type Type

  89. type Type struct {
  90.     Doc  string
  91.     Name string
  92.     Decl *ast.GenDecl
  93.     // associated declarations
  94.     Consts  []*Value // sorted list of constants of (mostly) this type
  95.     Vars    []*Value // sorted list of variables of (mostly) this type
  96.     Funcs   []*Func  // sorted list of functions returning this type
  97.     Methods []*Func  // sorted list of methods (including embedded ones) of this type
  98. }
  99.  
  100. Type is the documentation for a type declaration.

  101. type Value

  102. type Value struct {
  103.     Doc   string
  104.     Names []string // var or const names in declaration order
  105.     Decl  *ast.GenDecl
  106.     // contains filtered or unexported fields
  107. }
  108.  
  109. Value is the documentation for a (possibly grouped) var or const declaration.