-
Notifications
You must be signed in to change notification settings - Fork 26
/
funcs.go
150 lines (131 loc) · 2.47 KB
/
funcs.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
146
147
148
149
150
package doc
import (
"bytes"
"fmt"
"go/ast"
"go/doc"
"go/printer"
"go/token"
"strings"
"gnd.la/internal/astutil"
"gnd.la/template"
)
const (
constPrefix = "const-"
varPrefix = "var-"
)
func ConstId(name string) string {
return constPrefix + name
}
func VarId(name string) string {
return varPrefix + name
}
func FuncId(name string) string {
return "func-" + name
}
func TypeId(name string) string {
return "type-" + name
}
func MethodId(typ string, name string) string {
return "type-" + typ + "-method-" + name
}
func trim(s string, t string) string {
return strings.Trim(s, t)
}
func funcId(fn *doc.Func) string {
if fn.Recv != "" {
recv := fn.Recv
if recv[0] == '*' {
recv = recv[1:]
}
return MethodId(recv, fn.Name)
}
return FuncId(fn.Name)
}
func FuncReceiver(fn *ast.FuncDecl) string {
if fn.Recv != nil {
recv := astutil.Ident(fn.Recv.List[0].Type)
if recv[0] == '*' {
recv = recv[1:]
}
return recv
}
return ""
}
func funcName(fn *ast.FuncDecl) string {
if recv := FuncReceiver(fn); recv != "" {
return "(" + recv + ") " + fn.Name.Name
}
return fn.Name.Name
}
func typeId(typ *doc.Type) string {
return TypeId(typ.Name)
}
func documentedColor(val float64) string {
if val < 25 {
return "red"
}
if val < 50 {
return "yello"
}
if val < 75 {
return "lightblue"
}
return "green"
}
func issuesColor(val int) string {
if val > 20 {
return "red"
}
if val > 10 {
return "yellow"
}
return "green"
}
func complexityColor(val interface{}) (string, error) {
var v float64
switch va := val.(type) {
case int:
v = float64(va)
case float64:
v = va
default:
return "", fmt.Errorf("invalid complexity type %T", val)
}
if v < 5 {
return "green", nil
}
if v < 10 {
return "yellow", nil
}
return "red", nil
}
func bootstrapColor(c string) string {
switch c {
case "green":
return "success"
case "lightblue":
return "info"
case "yellow":
return "warning"
case "red":
return "danger"
}
return ""
}
func funcListName(fn *ast.FuncDecl) string {
var buf bytes.Buffer
printer.Fprint(&buf, token.NewFileSet(), fn)
return strings.TrimPrefix(buf.String(), "func ")
}
func init() {
template.AddFuncs([]*template.Func{
{Name: "func_id", Fn: funcId},
{Name: "func_name", Fn: funcName},
{Name: "type_id", Fn: typeId},
{Name: "issues_color", Fn: issuesColor},
{Name: "complexity_color", Fn: complexityColor},
{Name: "bootstrap_color", Fn: bootstrapColor},
{Name: "func_list_name", Fn: funcListName},
})
}