Debian Codegraph @

gopls/internal/golang/completion/fuzz.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
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 // golang/go#51089
17 // *testing.F deserves special treatment as member use is constrained:
18 // The arguments to f.Fuzz are determined by the arguments to a previous f.Add
19 // Inside f.Fuzz only f.Failed and f.Name are allowed.
20 // PJW: are there other packages where we can deduce usage constraints?
21
22 // if we find fuzz completions, then return true, as those are the only completions to offer
23 func (c *completer) fuzz(testingF types.Type, imp *importInfo, cb func(candidate)) bool {
24 // 1. inside f.Fuzz? (only f.Failed and f.Name)
25 // 2. possible completing f.Fuzz?
26 // [Ident,SelectorExpr,Callexpr,ExprStmt,BlockiStmt,FuncDecl(Fuzz...)]
27 // 3. before f.Fuzz, same (for 2., offer choice when looking at an F)
28
29 mset := types.NewMethodSet(testingF)
30
31 // does the path contain FuncLit as arg to f.Fuzz CallExpr?
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 { // avoid t.Fuzz itself in tests
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 // if it could be t.Fuzz, look for the preceding t.Add
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 // Sel.X should be of type *testing.F
86 got := c.pkg.TypesInfo().Types[s.X]
87 if got.Type.String() == "*testing.F" {
88 add = call
89 }
90 return false // because we're done...
91 }
92 // look at the enclosing FuzzFoo functions
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 // the path should start with ast.File, ast.FuncDecl, ...
99 // but it didn't, so give up
100 return false
101 }
102 ast.Inspect(n, f)
103 if add == nil {
104 // looks like f.Fuzz without a preceding f.Add.
105 // let the regular completion handle it.
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 // How could this happen, but better safe than panic.
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, // pretty confident the user should see this
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 // done
140 }
141 // let the standard processing take care of it instead
142 return false
143 }