Debian Codegraph @

gopls/internal/test/marker/testdata/completion/foobarbaz.txt

raw
1 This test ports some arbitrary tests from the old marker framework, that were
2 *mostly* about completion.
3
4 -- flags --
5 -ignore_extra_diags
6
7 -- settings.json --
8 {
9 "completeUnimported": false,
10 "deepCompletion": false,
11 "experimentalPostfixCompletions": false
12 }
13
14 -- go.mod --
15 module foobar.test
16
17 go 1.18
18
19 -- foo/foo.go --
20 package foo //@loc(PackageFoo, "foo"),item(PackageFooItem, "foo", "\"foobar.test/foo\"", "package")
21
22 type StructFoo struct { //@loc(StructFooLoc, "StructFoo"), item(StructFoo, "StructFoo", "struct{...}", "struct")
23 Value int //@item(Value, "Value", "int", "field")
24 }
25
26 // Pre-set this marker, as we don't have a "source" for it in this package.
27 /* Error() */ //@item(Error, "Error", "func() string", "method")
28
29 func Foo() { //@item(Foo, "Foo", "func()", "func")
30 var err error
31 err.Error() //@complete(re"()E", Error)
32 }
33
34 func _() {
35 var sFoo StructFoo //@complete(re"()t", StructFoo)
36 if x := sFoo; x.Value == 1 { //@complete(re"()V", Value), typedef("sFoo", StructFooLoc)
37 return
38 }
39 }
40
41 func _() {
42 shadowed := 123
43 {
44 shadowed := "hi" //@item(shadowed, "shadowed", "string", "var")
45 sha //@complete(re"()a", shadowed), diag("sha", re"(undefined|undeclared)")
46 _ = shadowed
47 }
48 }
49
50 type IntFoo int //@loc(IntFooLoc, "IntFoo"), item(IntFoo, "IntFoo", "int", "type")
51
52 -- bar/bar.go --
53 package bar
54
55 import (
56 "foobar.test/foo" //@item(foo, "foo", "\"foobar.test/foo\"", "package")
57 )
58
59 func helper(i foo.IntFoo) {} //@item(helper, "helper", "func(i foo.IntFoo)", "func")
60
61 func _() {
62 help //@complete(re"()l", helper)
63 _ = foo.StructFoo{} //@complete(re"()S", IntFoo, StructFoo)
64 }
65
66 // Bar is a function.
67 func Bar() { //@item(Bar, "Bar", "func()", "func", "Bar is a function.")
68 foo.Foo() //@complete(re"()F", Foo, IntFoo, StructFoo)
69 var _ foo.IntFoo //@complete(re"()I", IntFoo, StructFoo)
70 foo.() //@complete(re"()\\(", Foo, IntFoo, StructFoo), diag(")", re"expected type")
71 }
72
73 // These items weren't present in the old marker tests (due to settings), but
74 // we may as well include them.
75 //@item(intConversion, "int()"), item(fooFoo, "foo.Foo")
76 //@item(fooIntFoo, "foo.IntFoo"), item(fooStructFoo, "foo.StructFoo")
77
78 func _() {
79 var Valentine int //@item(Valentine, "Valentine", "int", "var")
80
81 _ = foo.StructFoo{ //@diag("foo", re"unkeyed fields")
82 Valu //@complete(re"() \\/\\/", Value)
83 }
84 _ = foo.StructFoo{ //@diag("foo", re"unkeyed fields")
85 Va //@complete(re"()a", Value, Valentine)
86
87 }
88 _ = foo.StructFoo{
89 Value: 5, //@complete(re"()a", Value)
90 }
91 _ = foo.StructFoo{
92 //@complete(re"()//", Value, Valentine, intConversion, foo, helper, Bar)
93 }
94 _ = foo.StructFoo{
95 Value: Valen //@complete(re"()le", Valentine)
96 }
97 _ = foo.StructFoo{
98 Value: //@complete(re"() \\/\\/", Valentine, intConversion, foo, helper, Bar)
99 }
100 _ = foo.StructFoo{
101 Value: //@complete(re"() ", Valentine, intConversion, foo, helper, Bar)
102 }
103 }
104
105 -- baz/baz.go --
106 package baz
107
108 import (
109 "foobar.test/bar"
110
111 f "foobar.test/foo"
112 )
113
114 var FooStruct f.StructFoo
115
116 func Baz() {
117 defer bar.Bar() //@complete(re"()B", Bar)
118 // TODO: Test completion here.
119 defer bar.B //@diag(re"bar.B()", re"must be function call")
120 var x f.IntFoo //@complete(re"()n", IntFoo), typedef("x", IntFooLoc)
121 bar.Bar() //@complete(re"()B", Bar)
122 }
123
124 func _() {
125 bob := f.StructFoo{Value: 5}
126 if x := bob. //@complete(re"() \\/\\/", Value)
127 switch true == false {
128 case true:
129 if x := bob. //@complete(re"() \\/\\/", Value)
130 case false:
131 }
132 if x := bob.Va //@complete(re"()a", Value)
133 switch true == true {
134 default:
135 }
136 }
137
138 -- arraytype/arraytype.go --
139 package arraytype
140
141 import (
142 "foobar.test/foo"
143 )
144
145 func _() {
146 var (
147 val string //@item(atVal, "val", "string", "var")
148 )
149
150 [] //@complete(re"() \\/\\/", atVal, PackageFooItem)
151
152 []val //@complete(re"() \\/\\/")
153
154 []foo.StructFoo //@complete(re"() \\/\\/", StructFoo)
155
156 []foo.StructFoo(nil) //@complete(re"()\\(", StructFoo)
157
158 []*foo.StructFoo //@complete(re"() \\/\\/", StructFoo)
159
160 [...]foo.StructFoo //@complete(re"() \\/\\/", StructFoo)
161
162 [2][][4]foo.StructFoo //@complete(re"() \\/\\/", StructFoo)
163
164 []struct { f []foo.StructFoo } //@complete(re"() }", StructFoo)
165 }
166
167 func _() {
168 type myInt int //@item(atMyInt, "myInt", "int", "type")
169
170 var mark []myInt //@item(atMark, "mark", "[]myInt", "var")
171
172 var s []myInt //@item(atS, "s", "[]myInt", "var")
173 s = []m //@complete(re"() \\/\\/", atMyInt)
174
175 var a [1]myInt
176 a = [1]m //@complete(re"() \\/\\/", atMyInt)
177
178 var ds [][]myInt
179 ds = [][]m //@complete(re"() \\/\\/", atMyInt)
180 }
181
182 func _() {
183 var b [0]byte //@item(atByte, "b", "[0]byte", "var")
184 var _ []byte = b //@snippet(re"() \\/\\/", atByte, "b[:]")
185 }
186
187 -- badstmt/badstmt.go --
188 package badstmt
189
190 import (
191 "foobar.test/foo"
192 )
193
194 // (The syntax error causes suppression of diagnostics for type errors.
195 // See issue #59888.)
196
197 func _(x int) {
198 defer foo.F //@complete(re"() \\/\\/", Foo, IntFoo, StructFoo)
199 defer foo.F //@complete(re"() \\/\\/", Foo, IntFoo, StructFoo)
200 }
201
202 func _() {
203 switch true {
204 case true:
205 go foo.F //@complete(re"() \\/\\/", Foo, IntFoo, StructFoo)
206 }
207 }
208
209 func _() {
210 defer func() {
211 foo.F //@complete(re"() \\/\\/", Foo, IntFoo, StructFoo), snippet(re"() \\/\\/", Foo, "Foo()")
212
213 foo. //@rank(re"() \\/\\/", Foo)
214 }
215 }
216
217 -- badstmt/badstmt_2.go --
218 package badstmt
219
220 import (
221 "foobar.test/foo"
222 )
223
224 func _() {
225 defer func() { foo. } //@rank(re"() }", Foo)
226 }
227
228 -- badstmt/badstmt_3.go --
229 package badstmt
230
231 import (
232 "foobar.test/foo"
233 )
234
235 func _() {
236 go foo. //@rank(re"() \\/\\/", Foo, IntFoo), snippet(re"() \\/\\/", Foo, "Foo()")
237 }
238
239 -- badstmt/badstmt_4.go --
240 package badstmt
241
242 import (
243 "foobar.test/foo"
244 )
245
246 func _() {
247 go func() {
248 defer foo. //@rank(re"() \\/\\/", Foo, IntFoo)
249 }
250 }
251
252 -- selector/selector.go --
253 package selector
254
255 import (
256 "foobar.test/bar"
257 )
258
259 type S struct {
260 B, A, C int //@item(Bf, "B", "int", "field"),item(Af, "A", "int", "field"),item(Cf, "C", "int", "field")
261 }
262
263 func _() {
264 _ = S{}.; //@complete(re"();", Af, Bf, Cf)
265 }
266
267 type bob struct { a int } //@item(a, "a", "int", "field")
268 type george struct { b int }
269 type jack struct { c int } //@item(c, "c", "int", "field")
270 type jill struct { d int }
271
272 func (b *bob) george() *george {} //@item(george, "george", "func() *george", "method")
273 func (g *george) jack() *jack {}
274 func (j *jack) jill() *jill {} //@item(jill, "jill", "func() *jill", "method")
275
276 func _() {
277 b := &bob{}
278 y := b.george().
279 jack();
280 y.; //@complete(re"();", c, jill)
281 }
282
283 func _() {
284 bar. //@complete(re"() \\/", Bar)
285 x := 5
286
287 var b *bob
288 b. //@complete(re"() \\/", a, george)
289 y, z := 5, 6
290
291 b. //@complete(re"() \\/", a, george)
292 y, z, a, b, c := 5, 6
293 }
294
295 func _() {
296 bar. //@complete(re"() \\/", Bar)
297 bar.Bar()
298
299 bar. //@complete(re"() \\/", Bar)
300 go f()
301 }
302
303 func _() {
304 var b *bob
305 if y != b. //@complete(re"() \\/", a, george)
306 z := 5
307
308 if z + y + 1 + b. //@complete(re"() \\/", a, george)
309 r, s, t := 4, 5
310
311 if y != b. //@complete(re"() \\/", a, george)
312 z = 5
313
314 if z + y + 1 + b. //@complete(re"() \\/", a, george)
315 r = 4
316 }
317
318 -- literal_snippets/literal_snippets.go --
319 package literal_snippets
320
321 import (
322 "bytes"
323 "context"
324 "go/ast"
325 "net/http"
326 "sort"
327
328 "golang.org/lsptests/foo"
329 )
330
331 func _() {
332 []int{} //@item(litIntSlice, "[]int{}", "", "var")
333 &[]int{} //@item(litIntSliceAddr, "&[]int{}", "", "var")
334 make([]int, 0) //@item(makeIntSlice, "make([]int, 0)", "", "func")
335
336 var _ *[]int = in //@snippet(re"() \\/\\/", litIntSliceAddr, "&[]int{$0\\}")
337 var _ **[]int = in //@complete(re"() \\/\\/")
338
339 var slice []int
340 slice = i //@snippet(re"() \\/\\/", litIntSlice, "[]int{$0\\}")
341 slice = m //@snippet(re"() \\/\\/", makeIntSlice, "make([]int, ${1:})")
342 }
343
344 func _() {
345 type namedInt []int
346
347 namedInt{} //@item(litNamedSlice, "namedInt{}", "", "var")
348 make(namedInt, 0) //@item(makeNamedSlice, "make(namedInt, 0)", "", "func")
349
350 var namedSlice namedInt
351 namedSlice = n //@snippet(re"() \\/\\/", litNamedSlice, "namedInt{$0\\}")
352 namedSlice = m //@snippet(re"() \\/\\/", makeNamedSlice, "make(namedInt, ${1:})")
353 }
354
355 func _() {
356 make(chan int) //@item(makeChan, "make(chan int)", "", "func")
357
358 var ch chan int
359 ch = m //@snippet(re"() \\/\\/", makeChan, "make(chan int)")
360 }
361
362 func _() {
363 map[string]struct{}{} //@item(litMap, "map[string]struct{}{}", "", "var")
364 make(map[string]struct{}) //@item(makeMap, "make(map[string]struct{})", "", "func")
365
366 var m map[string]struct{}
367 m = m //@snippet(re"() \\/\\/", litMap, "map[string]struct{\\}{$0\\}")
368 m = m //@snippet(re"() \\/\\/", makeMap, "make(map[string]struct{\\})")
369
370 struct{}{} //@item(litEmptyStruct, "struct{}{}", "", "var")
371
372 m["hi"] = s //@snippet(re"() \\/\\/", litEmptyStruct, "struct{\\}{\\}")
373 }
374
375 func _() {
376 type myStruct struct{ i int } //@item(myStructType, "myStruct", "struct{...}", "struct")
377
378 myStruct{} //@item(litStruct, "myStruct{}", "", "var")
379 &myStruct{} //@item(litStructPtr, "&myStruct{}", "", "var")
380
381 var ms myStruct
382 ms = m //@snippet(re"() \\/\\/", litStruct, "myStruct{$0\\}")
383
384 var msPtr *myStruct
385 msPtr = m //@snippet(re"() \\/\\/", litStructPtr, "&myStruct{$0\\}")
386
387 msPtr = &m //@snippet(re"() \\/\\/", litStruct, "myStruct{$0\\}")
388
389 type myStructCopy struct { i int } //@item(myStructCopyType, "myStructCopy", "struct{...}", "struct")
390
391 // Don't offer literal completion for convertible structs.
392 ms = myStruct //@complete(re"() \\/\\/", litStruct, myStructType, myStructCopyType)
393 }
394
395 type myImpl struct{}
396
397 func (myImpl) foo() {}
398
399 func (*myImpl) bar() {}
400
401 type myBasicImpl string
402
403 func (myBasicImpl) foo() {}
404
405 func _() {
406 type myIntf interface {
407 foo()
408 }
409
410 myImpl{} //@item(litImpl, "myImpl{}", "", "var")
411
412 var mi myIntf
413 mi = m //@snippet(re"() \\/\\/", litImpl, "myImpl{\\}")
414
415 myBasicImpl() //@item(litBasicImpl, "myBasicImpl()", "string", "var")
416
417 mi = m //@snippet(re"() \\/\\/", litBasicImpl, "myBasicImpl($0)")
418
419 // only satisfied by pointer to myImpl
420 type myPtrIntf interface {
421 bar()
422 }
423
424 &myImpl{} //@item(litImplPtr, "&myImpl{}", "", "var")
425
426 var mpi myPtrIntf
427 mpi = m //@snippet(re"() \\/\\/", litImplPtr, "&myImpl{\\}")
428 }
429
430 func _() {
431 var s struct{ i []int } //@item(litSliceField, "i", "[]int", "field")
432 var foo []int
433 // no literal completions after selector
434 foo = s.i //@complete(re"() \\/\\/", litSliceField)
435 }
436
437 func _() {
438 type myStruct struct{ i int } //@item(litMyStructType, "myStruct", "struct{...}", "struct")
439 myStruct{} //@item(litMyStruct, "myStruct{}", "", "var")
440
441 foo := func(s string, args ...myStruct) {}
442 // Don't give literal slice candidate for variadic arg.
443 // Do give literal candidates for variadic element.
444 foo("", myStruct) //@complete(re"()\\)", litMyStruct, litMyStructType)
445 }
446
447 func _() {
448 Buffer{} //@item(litBuffer, "Buffer{}", "", "var")
449
450 var b *bytes.Buffer
451 b = bytes.Bu //@snippet(re"() \\/\\/", litBuffer, "Buffer{\\}")
452 }
453
454 func _() {
455 _ = "func(...) {}" //@item(litFunc, "func(...) {}", "", "var")
456
457 // no literal "func" completions
458 http.Handle("", fun) //@complete(re"()\\)")
459
460 var namedReturn func(s string) (b bool)
461 namedReturn = f //@snippet(re"() \\/\\/", litFunc, "func(s string) (b bool) {$0\\}")
462
463 var multiReturn func() (bool, int)
464 multiReturn = f //@snippet(re"() \\/\\/", litFunc, "func() (bool, int) {$0\\}")
465
466 var multiNamedReturn func() (b bool, i int)
467 multiNamedReturn = f //@snippet(re"() \\/\\/", litFunc, "func() (b bool, i int) {$0\\}")
468
469 var duplicateParams func(myImpl, int, myImpl)
470 duplicateParams = f //@snippet(re"() \\/\\/", litFunc, "func(mi1 myImpl, i int, mi2 myImpl) {$0\\}")
471
472 type aliasImpl = myImpl
473 var aliasParams func(aliasImpl) aliasImpl
474 aliasParams = f //@snippet(re"() \\/\\/", litFunc, "func(ai aliasImpl) aliasImpl {$0\\}")
475
476 const two = 2
477 var builtinTypes func([]int, [two]bool, map[string]string, struct{ i int }, interface{ foo() }, <-chan int)
478 builtinTypes = f //@snippet(re"() \\/\\/", litFunc, "func(i1 []int, b [2]bool, m map[string]string, s struct{i int\\}, i2 interface{foo()\\}, c <-chan int) {$0\\}")
479
480 var _ func(ast.Node) = f //@snippet(re"() \\/\\/", litFunc, "func(n ast.Node) {$0\\}")
481 var _ func(error) = f //@snippet(re"() \\/\\/", litFunc, "func(err error) {$0\\}")
482 var _ func(context.Context) = f //@snippet(re"() \\/\\/", litFunc, "func(ctx context.Context) {$0\\}")
483
484 type context struct {}
485 var _ func(context) = f //@snippet(re"() \\/\\/", litFunc, "func(ctx context) {$0\\}")
486 }
487
488 func _() {
489 float64() //@item(litFloat64, "float64()", "float64", "var")
490
491 // don't complete to "&float64()"
492 var _ *float64 = float64 //@complete(re"() \\/\\/")
493
494 var f float64
495 f = fl //@complete(re"() \\/\\/", litFloat64),snippet(re"() \\/\\/", litFloat64, "float64($0)")
496
497 type myInt int
498 myInt() //@item(litMyInt, "myInt()", "", "var")
499
500 var mi myInt
501 mi = my //@snippet(re"() \\/\\/", litMyInt, "myInt($0)")
502 }
503
504 func _() {
505 type ptrStruct struct {
506 p *ptrStruct
507 }
508
509 ptrStruct{} //@item(litPtrStruct, "ptrStruct{}", "", "var")
510
511 ptrStruct{
512 p: &ptrSt, //@rank(re"(),", litPtrStruct)
513 }
514
515 &ptrStruct{} //@item(litPtrStructPtr, "&ptrStruct{}", "", "var")
516
517 &ptrStruct{
518 p: ptrSt, //@rank(re"(),", litPtrStructPtr)
519 }
520 }
521
522 func _() {
523 f := func(...[]int) {}
524 f() //@snippet(re"()\\)", litIntSlice, "[]int{$0\\}")
525 }
526
527
528 func _() {
529 // don't complete to "untyped int()"
530 []int{}[untyped] //@complete(re"()\\] \\/\\/")
531 }
532
533 type Tree[T any] struct{}
534
535 func (tree Tree[T]) Do(f func(s T)) {}
536
537 func _() {
538 var t Tree[string]
539 t.Do(fun) //@complete(re"()\\)", litFunc), snippet(re"()\\)", litFunc, "func(s string) {$0\\}")
540 }