forked from princjef/gomarkdoc
/
type.go
145 lines (121 loc) · 3.7 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package lang
import (
"fmt"
"go/doc"
"strings"
)
// Type holds documentation information for a type declaration.
type Type struct {
cfg *Config
doc *doc.Type
examples []*doc.Example
}
// NewType creates a Type from the raw documentation representation of the type,
// the token.FileSet for the package's files and the full list of examples from
// the containing package.
func NewType(cfg *Config, doc *doc.Type, examples []*doc.Example) *Type {
return &Type{cfg, doc, examples}
}
// Level provides the default level that headers for the type should be
// rendered.
func (typ *Type) Level() int {
return typ.cfg.Level
}
// Name provides the name of the type
func (typ *Type) Name() string {
return typ.doc.Name
}
// Title provides a formatted name suitable for use in a header identifying the
// type.
func (typ *Type) Title() string {
return fmt.Sprintf("type %s", typ.doc.Name)
}
// Location returns a representation of the node's location in a file within a
// repository.
func (typ *Type) Location() Location {
return NewLocation(typ.cfg, typ.doc.Decl)
}
// Summary provides the one-sentence summary of the type's documentation
// comment.
func (typ *Type) Summary() string {
return extractSummary(typ.doc.Doc)
}
// Doc provides the structured contents of the documentation comment for the
// type.
func (typ *Type) Doc() *Doc {
return NewDoc(typ.cfg.Inc(1), typ.doc.Doc)
}
// Decl provides the raw text representation of the code for the type's
// declaration.
func (typ *Type) Decl() (string, error) {
return printNode(typ.doc.Decl, typ.cfg.FileSet)
}
// Examples lists the examples pertaining to the type from the set provided on
// initialization.
func (typ *Type) Examples() (examples []*Example) {
underscorePrefix := fmt.Sprintf("%s_", typ.doc.Name)
for _, example := range typ.examples {
var name string
switch {
case example.Name == typ.doc.Name:
name = ""
case strings.HasPrefix(example.Name, underscorePrefix) && !typ.isSubexample(example.Name):
name = example.Name[len(underscorePrefix):]
default:
// TODO: better filtering
continue
}
examples = append(examples, NewExample(typ.cfg.Inc(1), name, example))
}
return
}
func (typ *Type) isSubexample(exampleName string) bool {
for _, m := range typ.doc.Methods {
fullName := fmt.Sprintf("%s_%s", typ.doc.Name, m.Name)
underscorePrefix := fmt.Sprintf("%s_", fullName)
if exampleName == fullName || strings.HasPrefix(exampleName, underscorePrefix) {
return true
}
}
return false
}
// Funcs lists the funcs related to the type. This only includes functions which
// return an instance of the type or its pointer.
func (typ *Type) Funcs() []*Func {
funcs := make([]*Func, len(typ.doc.Funcs))
for i, fn := range typ.doc.Funcs {
funcs[i] = NewFunc(typ.cfg.Inc(1), fn, typ.examples)
}
return funcs
}
// Methods lists the funcs that use the type as a value or pointer receiver.
func (typ *Type) Methods() []*Func {
methods := make([]*Func, len(typ.doc.Methods))
for i, fn := range typ.doc.Methods {
methods[i] = NewFunc(typ.cfg.Inc(1), fn, typ.examples)
}
return methods
}
// Consts lists the const declaration blocks containing values of this type.
func (typ *Type) Consts() []*Value {
consts := make([]*Value, len(typ.doc.Consts))
for i, c := range typ.doc.Consts {
consts[i] = NewValue(typ.cfg.Inc(1), c)
}
return consts
}
// Vars lists the var declaration blocks containing values of this type.
func (typ *Type) Vars() []*Value {
vars := make([]*Value, len(typ.doc.Vars))
for i, v := range typ.doc.Vars {
vars[i] = NewValue(typ.cfg.Inc(1), v)
}
return vars
}
// Anchor produces anchor text for the type.
func (typ *Type) Anchor() string {
return Symbol{
Kind: TypeSymbolKind,
Name: typ.doc.Name,
}.Anchor()
}