-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.go
More file actions
347 lines (286 loc) · 7.89 KB
/
render.go
File metadata and controls
347 lines (286 loc) · 7.89 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package godoc
import (
"fmt"
"go/ast"
"go/doc"
"go/token"
"go/types"
"strings"
"slices"
)
// typeSpecForDocType finds the *[ast.TypeSpec] corresponding to the given
// *[doc.Type].
func typeSpecForDocType(t *doc.Type) *ast.TypeSpec {
if t == nil || t.Decl == nil {
return nil
}
for _, spec := range t.Decl.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
if ts.Name != nil && ts.Name.Name == t.Name {
return ts
}
}
return nil
}
// pkgRequiresTypesInfo checks if the given *[doc.Package] contains any types
// that require *[types.Info] for accurate documentation.
func pkgRequiresTypesInfo(p *doc.Package) bool {
if p == nil {
return false
}
return slices.ContainsFunc(p.Types, typeRequiresTypesInfo)
}
// typeRequiresTypesInfo checks if the given *[doc.Type] requires *[types.Info]
// for accurate documentation.
func typeRequiresTypesInfo(t *doc.Type) bool {
if t == nil {
return false
}
typeSpec := typeSpecForDocType(t)
if typeSpec == nil {
return false
}
iface, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok || iface.Methods == nil {
return false
}
for _, field := range iface.Methods.List {
if len(field.Names) == 0 {
return true
}
}
return false
}
// typeDecl returns the kind and declaration string for the given *[doc.Type].
func typeDecl(t *doc.Type, fset *token.FileSet, astInfo *packageAST) (string, string) {
typeSpec := typeSpecForDocType(t)
if typeSpec == nil {
return "", ""
}
switch node := typeSpec.Type.(type) {
case *ast.InterfaceType:
return "interface", renderInterfaceDecl(t.Name, node, fset, astInfo)
case *ast.StructType:
return "struct", renderStructDecl(t.Name, node, fset, astInfo)
default:
expr := exprString(typeSpec.Type, fset)
if typeSpec.Assign.IsValid() {
return "alias", fmt.Sprintf("type %s = %s", t.Name, expr)
}
return "other", fmt.Sprintf("type %s %s", t.Name, expr)
}
}
// renderInterfaceDecl renders the declaration of an interface type as a string.
func renderInterfaceDecl(name string, iface *ast.InterfaceType, fset *token.FileSet, astInfo *packageAST) string {
lines := []string{fmt.Sprintf("type %s interface {", name)}
if iface != nil && iface.Methods != nil {
for _, field := range iface.Methods.List {
comment := ""
if field.Doc != nil {
comment = field.Doc.Text()
} else if field.Comment != nil {
comment = field.Comment.Text()
} else if c := commentTextForNode(field, astInfo); c != "" {
comment = c
}
lines = append(lines, formatCommentLines(comment)...)
var entry string
if len(field.Names) == 0 {
entry = exprString(field.Type, fset)
} else {
sig := exprString(field.Type, fset)
sig = strings.TrimPrefix(sig, "func")
entry = field.Names[0].Name + sig
}
if entry != "" {
lines = append(lines, " "+entry)
}
}
}
lines = append(lines, "}")
return strings.Join(lines, "\n")
}
// renderStructDecl renders the declaration of a struct type as a string.
func renderStructDecl(name string, st *ast.StructType, fset *token.FileSet, astInfo *packageAST) string {
lines := []string{fmt.Sprintf("type %s struct {", name)}
if st != nil && st.Fields != nil {
for _, field := range st.Fields.List {
comment := ""
if field.Doc != nil {
comment = field.Doc.Text()
} else if field.Comment != nil {
comment = field.Comment.Text()
} else if c := commentTextForNode(field, astInfo); c != "" {
comment = c
}
lines = append(lines, formatCommentLines(comment)...)
var entry string
if len(field.Names) == 0 {
entry = exprString(field.Type, fset)
} else {
names := make([]string, 0, len(field.Names))
for _, ident := range field.Names {
names = append(names, ident.Name)
}
typeStr := exprString(field.Type, fset)
entry = strings.Join(names, ", ")
if typeStr != "" {
entry = entry + " " + typeStr
}
}
if field.Tag != nil {
entry = entry + " " + field.Tag.Value
}
if entry != "" {
lines = append(lines, " "+entry)
}
}
}
lines = append(lines, "}")
return strings.Join(lines, "\n")
}
// formatCommentLines formats raw comment text into a slice of strings, each
// prefixed with "//" and properly indented.
func formatCommentLines(raw string) []string {
text := raw
if text == "" {
return nil
}
text = strings.TrimSuffix(text, "\n")
segments := strings.Split(text, "\n")
lines := make([]string, 0, len(segments))
for _, segment := range segments {
trimmed := strings.TrimRight(segment, " \t")
if trimmed == "" {
lines = append(lines, " //")
continue
}
lines = append(lines, " // "+trimmed)
}
return lines
}
// interfaceMethodDocs extracts method documentation for an interface type.
func interfaceMethodDocs(t *doc.Type, typesInfo *types.Info) []MethodDoc {
if t == nil || typesInfo == nil || t.Decl == nil {
return nil
}
typeSpec := typeSpecForDocType(t)
if typeSpec == nil {
return nil
}
ifaceAST, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok {
return nil
}
obj, _ := typesInfo.Defs[typeSpec.Name].(*types.TypeName)
if obj == nil {
return nil
}
ifaceType, _ := obj.Type().Underlying().(*types.Interface)
if ifaceType == nil {
return nil
}
ifaceType = ifaceType.Complete()
docMap := make(map[string]string)
if ifaceAST.Methods != nil {
for _, field := range ifaceAST.Methods.List {
if len(field.Names) == 0 {
continue
}
name := field.Names[0].Name
docText := ""
if field.Doc != nil {
docText = field.Doc.Text()
} else if field.Comment != nil {
docText = field.Comment.Text()
}
docMap[name] = docText
}
}
methods := make([]MethodDoc, 0, ifaceType.NumMethods())
for i := 0; i < ifaceType.NumMethods(); i++ {
method := ifaceType.Method(i)
sig, _ := method.Type().(*types.Signature)
methods = append(methods, MethodDoc{
Recv: t.Name,
RecvType: t.Name,
Name: method.Name(),
Args: argsFromSignature(sig, nil),
Returns: resultsFromSignature(sig, nil),
Doc: docMap[method.Name()],
})
}
return methods
}
// structFieldDocs extracts field documentation for a struct type.
func structFieldDocs(t *doc.Type, fset *token.FileSet, typesInfo *types.Info, astInfo *packageAST) []FieldDoc {
if t == nil || t.Decl == nil {
return nil
}
typeSpec := typeSpecForDocType(t)
if typeSpec == nil {
return nil
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok || structType.Fields == nil {
return nil
}
fields := make([]FieldDoc, 0, len(structType.Fields.List))
for _, field := range structType.Fields.List {
typeStr := fieldTypeString(field, typesInfo, fset)
docText := ""
if field.Doc != nil {
docText = field.Doc.Text()
} else if field.Comment != nil {
docText = field.Comment.Text()
} else if c := commentTextForNode(field, astInfo); c != "" {
docText = c
}
tag := ""
if field.Tag != nil {
tag = strings.Trim(field.Tag.Value, "`")
}
if len(field.Names) == 0 {
name := embeddedFieldName(field, fset, typeStr)
fields = append(fields, FieldDoc{
Name: name,
Type: typeStr,
Doc: docText,
Tag: tag,
Embedded: true,
})
continue
}
for _, ident := range field.Names {
fields = append(fields, FieldDoc{
Name: ident.Name,
Type: typeStr,
Doc: docText,
Tag: tag,
})
}
}
return fields
}
// fieldTypeString returns the string representation of a struct field's type.
func fieldTypeString(field *ast.Field, typesInfo *types.Info, fset *token.FileSet) string {
if field == nil || field.Type == nil {
return ""
}
if typesInfo != nil {
if tv, ok := typesInfo.Types[field.Type]; ok && tv.Type != nil {
return tv.Type.String()
}
}
return exprString(field.Type, fset)
}
// embeddedFieldName returns the name of an embedded struct field.
func embeddedFieldName(field *ast.Field, fset *token.FileSet, typeStr string) string {
if typeStr != "" {
return strings.TrimPrefix(typeStr, "*")
}
return strings.TrimPrefix(exprString(field.Type, fset), "*")
}