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