/
decl.go
129 lines (109 loc) · 2.75 KB
/
decl.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
package analyzer
import (
"go/ast"
"strconv"
"strings"
)
func formatFieldAndType(t ast.Expr, id *ast.Ident) string {
typeStr := expToString(t)
return id.String() + " " + typeStr
}
func formatFieldsList(params *ast.FieldList, joinChar string) (string, int) {
if params == nil {
return "", 0
}
paramsLen := len(params.List)
fieldTypePair := make([]string, 0, paramsLen)
for _, p := range params.List {
if len(p.Names) == 0 {
// In case when no named return
fieldTypePair = append(fieldTypePair, expToString(p.Type))
}
// Named return params
for _, n := range p.Names {
paramStr := formatFieldAndType(p.Type, n)
fieldTypePair = append(fieldTypePair, paramStr)
}
}
return strings.Join(fieldTypePair, joinChar), paramsLen
}
func valSpecToItem(isConst bool, v *ast.ValueSpec, withPrivate bool) []*CompletionItem {
items := make([]*CompletionItem, 0, len(v.Names))
for _, n := range v.Names {
if !n.IsExported() {
if !withPrivate {
continue
}
}
ci := &CompletionItem{
Label: n.String(),
Documentation: v.Doc.Text(),
Detail: n.String(),
InsertText: n.String(),
}
if isConst {
ci.Kind = Constant
} else {
ci.Kind = Variable
}
items = append(items, ci)
}
return items
}
func funcToString(fn *ast.FuncType) string {
params, _ := formatFieldsList(fn.Params, ", ")
str := "func(" + params + ")"
returns, retCount := formatFieldsList(fn.Results, ", ")
switch retCount {
case 0:
break
case 1:
str += " " + returns
default:
str += " (" + returns + ")"
}
return str
}
// formatFuncInsertText returns `insertText` snippet template for monaco-editor
// from function signature.
//
// Example:
// func sum(a, b int) int
// Will output:
// "sum(${1:a}, ${2:b})"
func formatFuncInsertText(fn *ast.FuncDecl) string {
sb := strings.Builder{}
sb.WriteString(fn.Name.String())
sb.WriteByte('(')
if fn.Type.Params != nil && fn.Type.Params.List != nil {
pos := 1 // in monaco, first input argument should start with 0
params := make([]string, 0, fn.Type.Params.NumFields())
for _, param := range fn.Type.Params.List {
if len(param.Names) == 0 {
// $pos
params = append(params, "$"+strconv.Itoa(pos))
pos++
continue
}
for _, p := range param.Names {
// ${pos:paramName}
params = append(params, "${"+strconv.Itoa(pos)+":"+p.Name+"}")
pos++
}
}
sb.WriteString(strings.Join(params, ", "))
}
sb.WriteRune(')')
return sb.String()
}
func funcToItem(fn *ast.FuncDecl) *CompletionItem {
ci := &CompletionItem{
Label: fn.Name.String(),
Kind: Function,
Detail: funcToString(fn.Type),
Documentation: formatDoc(fn.Doc.Text()),
InsertText: formatFuncInsertText(fn),
InsertTextRules: InsertAsSnippet,
}
return ci
}