This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
docValue.go
117 lines (93 loc) · 2.26 KB
/
docValue.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
package internal
import (
"fmt"
"log"
"strings"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/format"
"github.com/dagger/cuelsp/loader"
)
type DocValue struct {
description string
structure string
}
func NewDocValue(node ast.Node, v *loader.Value) *DocValue {
doc := &DocValue{}
for _, d := range v.Doc() {
if d.Text() != "" {
doc.description += fmt.Sprintf("%s\n", d.Text())
}
}
field, ok := node.(*ast.Field)
if ok {
doc.structure = customerFormatNode(field, 0)
}
return doc
}
func customerFormatNode(node ast.Node, depth int) string {
var doc string
formatNode := func(n ast.Node) string {
display, err := format.Node(n, format.Simplify())
if err == nil {
return string(display)
}
return "unknown"
}
switch n := node.(type) {
case *ast.Field:
switch v := n.Value.(type) {
case *ast.Ident:
if depth == 0 {
return fmt.Sprintf("%s: %s", n.Label, v)
}
return formatNode(n)
case *ast.UnaryExpr, *ast.BinaryExpr:
return formatNode(n)
case *ast.StructLit:
return fmt.Sprintf("%s: %s", n.Label, formatNode(v))
case *ast.BasicLit:
return fmt.Sprintf("%s: %s", n.Label, formatNode(v))
default:
doc += fmt.Sprintf("%s: {\n%s}", n.Label, customerFormatNode(v, depth+1))
}
case *ast.StructLit:
for _, d := range n.Elts {
doc += customerFormatNode(d, depth+1)
}
}
return doc
}
func (d *DocValue) String() string {
var doc string
if d.description != "" {
doc = fmt.Sprintf("#### Description\n%s", d.description)
}
if d.structure != "" {
doc += fmt.Sprintf("#### Type\n%s", d.structure)
}
return doc
}
func (d *DocValue) MarkdownString() string {
var doc string
if d.description != "" {
doc = fmt.Sprintf("#### Description\n%s", d.description)
}
if d.structure != "" {
doc += "#### Type\n"
structure := d.structure
// Insert carrier return if it's a definition
structure = strings.Replace(structure, ": {", ": {\n\n", 1)
// Insert tab on each fields in a definition
if strings.Index(structure, ": {") < strings.Index(structure, "\n") {
lines := strings.Split(structure, "\n")
for i := 1; i < len(lines)-1; i++ {
lines[i] = fmt.Sprintf("\t%s", lines[i])
}
structure = strings.Join(lines, "\n")
} else {
log.Println("not struct")
}
doc += structure
}
return doc
}