| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/ast" |
| 10 |
"go/types" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"golang.org/x/tools/gopls/internal/protocol" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func (c *completer) fuzz(testingF types.Type, imp *importInfo, cb func(candidate)) bool { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
mset := types.NewMethodSet(testingF) |
| 30 |
|
| 31 |
|
| 32 |
inside := false |
| 33 |
Loop: |
| 34 |
for i, n := range c.path { |
| 35 |
switch v := n.(type) { |
| 36 |
case *ast.CallExpr: |
| 37 |
if len(v.Args) != 1 { |
| 38 |
continue Loop |
| 39 |
} |
| 40 |
if _, ok := v.Args[0].(*ast.FuncLit); !ok { |
| 41 |
continue |
| 42 |
} |
| 43 |
if s, ok := v.Fun.(*ast.SelectorExpr); !ok || s.Sel.Name != "Fuzz" { |
| 44 |
continue |
| 45 |
} |
| 46 |
if i > 2 { |
| 47 |
inside = true |
| 48 |
break Loop |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
if inside { |
| 53 |
for method := range mset.Methods() { |
| 54 |
o := method.Obj() |
| 55 |
if o.Name() == "Failed" || o.Name() == "Name" { |
| 56 |
cb(candidate{ |
| 57 |
obj: o, |
| 58 |
score: stdScore, |
| 59 |
imp: imp, |
| 60 |
addressable: true, |
| 61 |
}) |
| 62 |
} |
| 63 |
} |
| 64 |
return true |
| 65 |
} |
| 66 |
|
| 67 |
id, ok := c.path[0].(*ast.Ident) |
| 68 |
if ok && strings.HasPrefix("Fuzz", id.Name) { |
| 69 |
var add *ast.CallExpr |
| 70 |
f := func(n ast.Node) bool { |
| 71 |
if n == nil { |
| 72 |
return true |
| 73 |
} |
| 74 |
call, ok := n.(*ast.CallExpr) |
| 75 |
if !ok { |
| 76 |
return true |
| 77 |
} |
| 78 |
s, ok := call.Fun.(*ast.SelectorExpr) |
| 79 |
if !ok { |
| 80 |
return true |
| 81 |
} |
| 82 |
if s.Sel.Name != "Add" { |
| 83 |
return true |
| 84 |
} |
| 85 |
|
| 86 |
got := c.pkg.TypesInfo().Types[s.X] |
| 87 |
if got.Type.String() == "*testing.F" { |
| 88 |
add = call |
| 89 |
} |
| 90 |
return false |
| 91 |
} |
| 92 |
|
| 93 |
if len(c.path) < 2 { |
| 94 |
return false |
| 95 |
} |
| 96 |
n := c.path[len(c.path)-2] |
| 97 |
if _, ok := n.(*ast.FuncDecl); !ok { |
| 98 |
|
| 99 |
|
| 100 |
return false |
| 101 |
} |
| 102 |
ast.Inspect(n, f) |
| 103 |
if add == nil { |
| 104 |
|
| 105 |
|
| 106 |
return false |
| 107 |
} |
| 108 |
|
| 109 |
lbl := "Fuzz(func(t *testing.T" |
| 110 |
for i, a := range add.Args { |
| 111 |
info := c.pkg.TypesInfo().TypeOf(a) |
| 112 |
if info == nil { |
| 113 |
return false |
| 114 |
} |
| 115 |
lbl += fmt.Sprintf(", %c %s", 'a'+i, info) |
| 116 |
} |
| 117 |
lbl += ")" |
| 118 |
xx := CompletionItem{ |
| 119 |
Label: lbl, |
| 120 |
InsertText: lbl, |
| 121 |
Kind: protocol.FunctionCompletion, |
| 122 |
Depth: 0, |
| 123 |
Score: 10, |
| 124 |
Documentation: "argument types from f.Add", |
| 125 |
isSlice: false, |
| 126 |
} |
| 127 |
c.items = append(c.items, xx) |
| 128 |
for method := range mset.Methods() { |
| 129 |
o := method.Obj() |
| 130 |
if o.Name() != "Fuzz" { |
| 131 |
cb(candidate{ |
| 132 |
obj: o, |
| 133 |
score: stdScore, |
| 134 |
imp: imp, |
| 135 |
addressable: true, |
| 136 |
}) |
| 137 |
} |
| 138 |
} |
| 139 |
return true |
| 140 |
} |
| 141 |
|
| 142 |
return false |
| 143 |
} |