Skip to content

Commit 32c56bc

Browse files
committed
server: improve type report
1 parent a49d921 commit 32c56bc

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

pkg/analyzer/decl.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func formatFieldAndType(t ast.Expr, id *ast.Ident) string {
1010
return id.String() + " " + typeStr
1111
}
1212

13-
func formatFuncParams(params *ast.FieldList) (string, int) {
13+
func formatFieldsList(params *ast.FieldList, joinChar string) (string, int) {
1414
if params == nil {
1515
return "", 0
1616
}
@@ -30,7 +30,7 @@ func formatFuncParams(params *ast.FieldList) (string, int) {
3030
}
3131
}
3232

33-
return strings.Join(fieldTypePair, ", "), paramsLen
33+
return strings.Join(fieldTypePair, joinChar), paramsLen
3434
}
3535

3636
func valSpecToItem(isConst bool, v *ast.ValueSpec, withPrivate bool) []*CompletionItem {
@@ -61,26 +61,30 @@ func valSpecToItem(isConst bool, v *ast.ValueSpec, withPrivate bool) []*Completi
6161
return items
6262
}
6363

64-
func funcToItem(fn *ast.FuncDecl) *CompletionItem {
65-
ci := &CompletionItem{
66-
Label: fn.Name.String(),
67-
Kind: Function,
68-
Documentation: fn.Doc.Text(),
69-
}
70-
71-
params, _ := formatFuncParams(fn.Type.Params)
72-
ci.Detail = "func(" + params + ")"
73-
ci.InsertText = ci.Label + "(" + params + ")"
74-
75-
returns, retCount := formatFuncParams(fn.Type.Results)
64+
func funcToString(fn *ast.FuncType) string {
65+
params, _ := formatFieldsList(fn.Params, ", ")
66+
str := "func(" + params + ")"
67+
returns, retCount := formatFieldsList(fn.Results, ", ")
7668
switch retCount {
7769
case 0:
7870
break
7971
case 1:
80-
ci.Detail += " " + returns
72+
str += " " + returns
8173
default:
82-
ci.Detail += " (" + returns + ")"
74+
str += " (" + returns + ")"
75+
}
76+
77+
return str
78+
}
79+
80+
func funcToItem(fn *ast.FuncDecl) *CompletionItem {
81+
ci := &CompletionItem{
82+
Label: fn.Name.String(),
83+
Kind: Function,
84+
Documentation: fn.Doc.Text(),
8385
}
8486

87+
ci.Detail = funcToString(fn.Type)
88+
ci.InsertText = ci.Label + "()"
8589
return ci
8690
}

pkg/analyzer/expr.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ func expToString(exp ast.Expr) string {
2323
return v.Sel.String()
2424
case *ast.StarExpr:
2525
return "*" + expToString(v.X)
26+
case *ast.Ellipsis:
27+
return "..." + expToString(v.Elt)
28+
case *ast.MapType:
29+
keyT := expToString(v.Key)
30+
valT := expToString(v.Value)
31+
return "map[" + keyT + "]" + valT
32+
case *ast.ChanType:
33+
chanT := expToString(v.Value)
34+
return "chan " + chanT
35+
case *ast.InterfaceType:
36+
typ := "interface{"
37+
fields, fieldCount := formatFieldsList(v.Methods, "\n")
38+
if fieldCount > 0 {
39+
typ += "\n" + fields + "\n"
40+
}
41+
return typ + "}"
42+
case *ast.FuncType:
43+
return funcToString(v)
2644
default:
2745
log.Warnf("expToString: unknown expression - [%[1]T %[1]v]", exp)
2846
return "interface{}"

0 commit comments

Comments
 (0)