| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 12 |
) |
| 13 |
|
| 14 |
func TestPostfixSnippetCompletion(t *testing.T) { |
| 15 |
const mod = ` |
| 16 |
-- go.mod -- |
| 17 |
module mod.com |
| 18 |
|
| 19 |
go 1.12 |
| 20 |
` |
| 21 |
|
| 22 |
cases := []struct { |
| 23 |
name string |
| 24 |
before, after string |
| 25 |
allowMultipleItem bool |
| 26 |
}{ |
| 27 |
{ |
| 28 |
name: "sort", |
| 29 |
before: ` |
| 30 |
package foo |
| 31 |
|
| 32 |
func _() { |
| 33 |
var foo []int |
| 34 |
foo.sort |
| 35 |
} |
| 36 |
`, |
| 37 |
after: ` |
| 38 |
package foo |
| 39 |
|
| 40 |
import "sort" |
| 41 |
|
| 42 |
func _() { |
| 43 |
var foo []int |
| 44 |
sort.Slice(foo, func(i, j int) bool { |
| 45 |
$0 |
| 46 |
}) |
| 47 |
} |
| 48 |
`, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "sort_renamed_sort_package", |
| 52 |
before: ` |
| 53 |
package foo |
| 54 |
|
| 55 |
import blahsort "sort" |
| 56 |
|
| 57 |
var j int |
| 58 |
|
| 59 |
func _() { |
| 60 |
var foo []int |
| 61 |
foo.sort |
| 62 |
} |
| 63 |
`, |
| 64 |
after: ` |
| 65 |
package foo |
| 66 |
|
| 67 |
import blahsort "sort" |
| 68 |
|
| 69 |
var j int |
| 70 |
|
| 71 |
func _() { |
| 72 |
var foo []int |
| 73 |
blahsort.Slice(foo, func(i, j2 int) bool { |
| 74 |
$0 |
| 75 |
}) |
| 76 |
} |
| 77 |
`, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "last", |
| 81 |
before: ` |
| 82 |
package foo |
| 83 |
|
| 84 |
func _() { |
| 85 |
var s struct { i []int } |
| 86 |
s.i.last |
| 87 |
} |
| 88 |
`, |
| 89 |
after: ` |
| 90 |
package foo |
| 91 |
|
| 92 |
func _() { |
| 93 |
var s struct { i []int } |
| 94 |
s.i[len(s.i)-1] |
| 95 |
} |
| 96 |
`, |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "reverse", |
| 100 |
before: ` |
| 101 |
package foo |
| 102 |
|
| 103 |
func _() { |
| 104 |
var foo []int |
| 105 |
foo.reverse |
| 106 |
} |
| 107 |
`, |
| 108 |
after: ` |
| 109 |
package foo |
| 110 |
|
| 111 |
import "slices" |
| 112 |
|
| 113 |
func _() { |
| 114 |
var foo []int |
| 115 |
slices.Reverse(foo) |
| 116 |
} |
| 117 |
`, |
| 118 |
}, |
| 119 |
{ |
| 120 |
name: "slice_range", |
| 121 |
before: ` |
| 122 |
package foo |
| 123 |
|
| 124 |
func _() { |
| 125 |
type myThing struct{} |
| 126 |
var foo []myThing |
| 127 |
foo.range |
| 128 |
} |
| 129 |
`, |
| 130 |
after: ` |
| 131 |
package foo |
| 132 |
|
| 133 |
func _() { |
| 134 |
type myThing struct{} |
| 135 |
var foo []myThing |
| 136 |
for ${1:}, ${2:} := range foo { |
| 137 |
$0 |
| 138 |
} |
| 139 |
} |
| 140 |
`, |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "append_stmt", |
| 144 |
before: ` |
| 145 |
package foo |
| 146 |
|
| 147 |
func _() { |
| 148 |
var foo []int |
| 149 |
foo.append |
| 150 |
} |
| 151 |
`, |
| 152 |
after: ` |
| 153 |
package foo |
| 154 |
|
| 155 |
func _() { |
| 156 |
var foo []int |
| 157 |
foo = append(foo, $0) |
| 158 |
} |
| 159 |
`, |
| 160 |
}, |
| 161 |
{ |
| 162 |
name: "append_expr", |
| 163 |
before: ` |
| 164 |
package foo |
| 165 |
|
| 166 |
func _() { |
| 167 |
var foo []int |
| 168 |
var _ []int = foo.append |
| 169 |
} |
| 170 |
`, |
| 171 |
after: ` |
| 172 |
package foo |
| 173 |
|
| 174 |
func _() { |
| 175 |
var foo []int |
| 176 |
var _ []int = append(foo, $0) |
| 177 |
} |
| 178 |
`, |
| 179 |
}, |
| 180 |
{ |
| 181 |
name: "slice_copy", |
| 182 |
before: ` |
| 183 |
package foo |
| 184 |
|
| 185 |
func _() { |
| 186 |
var foo []int |
| 187 |
foo.copy |
| 188 |
} |
| 189 |
`, |
| 190 |
after: ` |
| 191 |
package foo |
| 192 |
|
| 193 |
func _() { |
| 194 |
var foo []int |
| 195 |
fooCopy := make([]int, len(foo)) |
| 196 |
copy(fooCopy, foo) |
| 197 |
|
| 198 |
} |
| 199 |
`, |
| 200 |
}, |
| 201 |
{ |
| 202 |
name: "map_range", |
| 203 |
before: ` |
| 204 |
package foo |
| 205 |
|
| 206 |
func _() { |
| 207 |
var foo map[string]int |
| 208 |
foo.range |
| 209 |
} |
| 210 |
`, |
| 211 |
after: ` |
| 212 |
package foo |
| 213 |
|
| 214 |
func _() { |
| 215 |
var foo map[string]int |
| 216 |
for ${1:}, ${2:} := range foo { |
| 217 |
$0 |
| 218 |
} |
| 219 |
} |
| 220 |
`, |
| 221 |
}, |
| 222 |
{ |
| 223 |
name: "map_clear", |
| 224 |
before: ` |
| 225 |
package foo |
| 226 |
|
| 227 |
func _() { |
| 228 |
var foo map[string]int |
| 229 |
foo.clear |
| 230 |
} |
| 231 |
`, |
| 232 |
after: ` |
| 233 |
package foo |
| 234 |
|
| 235 |
func _() { |
| 236 |
var foo map[string]int |
| 237 |
for k := range foo { |
| 238 |
delete(foo, k) |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
`, |
| 243 |
}, |
| 244 |
{ |
| 245 |
name: "map_keys", |
| 246 |
before: ` |
| 247 |
package foo |
| 248 |
|
| 249 |
func _() { |
| 250 |
var foo map[string]int |
| 251 |
foo.keys |
| 252 |
} |
| 253 |
`, |
| 254 |
after: ` |
| 255 |
package foo |
| 256 |
|
| 257 |
func _() { |
| 258 |
var foo map[string]int |
| 259 |
keys := make([]string, 0, len(foo)) |
| 260 |
for k := range foo { |
| 261 |
keys = append(keys, k) |
| 262 |
} |
| 263 |
|
| 264 |
} |
| 265 |
`, |
| 266 |
}, |
| 267 |
{ |
| 268 |
name: "channel_range", |
| 269 |
before: ` |
| 270 |
package foo |
| 271 |
|
| 272 |
func _() { |
| 273 |
foo := make(chan int) |
| 274 |
foo.range |
| 275 |
} |
| 276 |
`, |
| 277 |
after: ` |
| 278 |
package foo |
| 279 |
|
| 280 |
func _() { |
| 281 |
foo := make(chan int) |
| 282 |
for ${1:} := range foo { |
| 283 |
$0 |
| 284 |
} |
| 285 |
} |
| 286 |
`, |
| 287 |
}, |
| 288 |
{ |
| 289 |
name: "var", |
| 290 |
before: ` |
| 291 |
package foo |
| 292 |
|
| 293 |
func foo() (int, error) { return 0, nil } |
| 294 |
|
| 295 |
func _() { |
| 296 |
foo().var |
| 297 |
} |
| 298 |
`, |
| 299 |
after: ` |
| 300 |
package foo |
| 301 |
|
| 302 |
func foo() (int, error) { return 0, nil } |
| 303 |
|
| 304 |
func _() { |
| 305 |
${1:}, ${2:} := foo() |
| 306 |
} |
| 307 |
`, |
| 308 |
allowMultipleItem: true, |
| 309 |
}, |
| 310 |
{ |
| 311 |
name: "var_single_value", |
| 312 |
before: ` |
| 313 |
package foo |
| 314 |
|
| 315 |
func foo() error { return nil } |
| 316 |
|
| 317 |
func _() { |
| 318 |
foo().var |
| 319 |
} |
| 320 |
`, |
| 321 |
allowMultipleItem: true, |
| 322 |
after: ` |
| 323 |
package foo |
| 324 |
|
| 325 |
func foo() error { return nil } |
| 326 |
|
| 327 |
func _() { |
| 328 |
${1:} := foo() |
| 329 |
} |
| 330 |
`, |
| 331 |
}, |
| 332 |
{ |
| 333 |
name: "var_same_type", |
| 334 |
before: ` |
| 335 |
package foo |
| 336 |
|
| 337 |
func foo() (int, int) { return 0, 0 } |
| 338 |
|
| 339 |
func _() { |
| 340 |
foo().var |
| 341 |
} |
| 342 |
`, |
| 343 |
after: ` |
| 344 |
package foo |
| 345 |
|
| 346 |
func foo() (int, int) { return 0, 0 } |
| 347 |
|
| 348 |
func _() { |
| 349 |
${1:}, ${2:} := foo() |
| 350 |
} |
| 351 |
`, |
| 352 |
}, |
| 353 |
{ |
| 354 |
name: "print_scalar", |
| 355 |
before: ` |
| 356 |
package foo |
| 357 |
|
| 358 |
func _() { |
| 359 |
var foo int |
| 360 |
foo.print |
| 361 |
} |
| 362 |
`, |
| 363 |
after: ` |
| 364 |
package foo |
| 365 |
|
| 366 |
import "fmt" |
| 367 |
|
| 368 |
func _() { |
| 369 |
var foo int |
| 370 |
fmt.Printf("foo: %v\n", foo) |
| 371 |
} |
| 372 |
`, |
| 373 |
}, |
| 374 |
{ |
| 375 |
name: "print_multi", |
| 376 |
before: ` |
| 377 |
package foo |
| 378 |
|
| 379 |
func foo() (int, error) { return 0, nil } |
| 380 |
|
| 381 |
func _() { |
| 382 |
foo().print |
| 383 |
} |
| 384 |
`, |
| 385 |
after: ` |
| 386 |
package foo |
| 387 |
|
| 388 |
import "fmt" |
| 389 |
|
| 390 |
func foo() (int, error) { return 0, nil } |
| 391 |
|
| 392 |
func _() { |
| 393 |
fmt.Println(foo()) |
| 394 |
} |
| 395 |
`, |
| 396 |
}, |
| 397 |
{ |
| 398 |
name: "string split", |
| 399 |
before: ` |
| 400 |
package foo |
| 401 |
|
| 402 |
func foo() []string { |
| 403 |
x := "test" |
| 404 |
return x.split |
| 405 |
}`, |
| 406 |
after: ` |
| 407 |
package foo |
| 408 |
|
| 409 |
import "strings" |
| 410 |
|
| 411 |
func foo() []string { |
| 412 |
x := "test" |
| 413 |
return strings.Split(x, "$0") |
| 414 |
}`, |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "string slice join", |
| 418 |
before: ` |
| 419 |
package foo |
| 420 |
|
| 421 |
func foo() string { |
| 422 |
x := []string{"a", "test"} |
| 423 |
return x.join |
| 424 |
}`, |
| 425 |
after: ` |
| 426 |
package foo |
| 427 |
|
| 428 |
import "strings" |
| 429 |
|
| 430 |
func foo() string { |
| 431 |
x := []string{"a", "test"} |
| 432 |
return strings.Join(x, "$0") |
| 433 |
}`, |
| 434 |
}, |
| 435 |
{ |
| 436 |
name: "if not nil interface", |
| 437 |
before: ` |
| 438 |
package foo |
| 439 |
|
| 440 |
func _() { |
| 441 |
var foo error |
| 442 |
foo.ifnotnil |
| 443 |
} |
| 444 |
`, |
| 445 |
after: ` |
| 446 |
package foo |
| 447 |
|
| 448 |
func _() { |
| 449 |
var foo error |
| 450 |
if foo != nil { |
| 451 |
$0 |
| 452 |
} |
| 453 |
} |
| 454 |
`, |
| 455 |
}, |
| 456 |
{ |
| 457 |
name: "if not nil pointer", |
| 458 |
before: ` |
| 459 |
package foo |
| 460 |
|
| 461 |
func _() { |
| 462 |
var foo *int |
| 463 |
foo.ifnotnil |
| 464 |
} |
| 465 |
`, |
| 466 |
after: ` |
| 467 |
package foo |
| 468 |
|
| 469 |
func _() { |
| 470 |
var foo *int |
| 471 |
if foo != nil { |
| 472 |
$0 |
| 473 |
} |
| 474 |
} |
| 475 |
`, |
| 476 |
}, |
| 477 |
{ |
| 478 |
name: "if not nil slice", |
| 479 |
before: ` |
| 480 |
package foo |
| 481 |
|
| 482 |
func _() { |
| 483 |
var foo []int |
| 484 |
foo.ifnotnil |
| 485 |
} |
| 486 |
`, |
| 487 |
after: ` |
| 488 |
package foo |
| 489 |
|
| 490 |
func _() { |
| 491 |
var foo []int |
| 492 |
if foo != nil { |
| 493 |
$0 |
| 494 |
} |
| 495 |
} |
| 496 |
`, |
| 497 |
}, |
| 498 |
{ |
| 499 |
name: "if not nil map", |
| 500 |
before: ` |
| 501 |
package foo |
| 502 |
|
| 503 |
func _() { |
| 504 |
var foo map[string]any |
| 505 |
foo.ifnotnil |
| 506 |
} |
| 507 |
`, |
| 508 |
after: ` |
| 509 |
package foo |
| 510 |
|
| 511 |
func _() { |
| 512 |
var foo map[string]any |
| 513 |
if foo != nil { |
| 514 |
$0 |
| 515 |
} |
| 516 |
} |
| 517 |
`, |
| 518 |
}, |
| 519 |
{ |
| 520 |
name: "if not nil channel", |
| 521 |
before: ` |
| 522 |
package foo |
| 523 |
|
| 524 |
func _() { |
| 525 |
var foo chan int |
| 526 |
foo.ifnotnil |
| 527 |
} |
| 528 |
`, |
| 529 |
after: ` |
| 530 |
package foo |
| 531 |
|
| 532 |
func _() { |
| 533 |
var foo chan int |
| 534 |
if foo != nil { |
| 535 |
$0 |
| 536 |
} |
| 537 |
} |
| 538 |
`, |
| 539 |
}, |
| 540 |
{ |
| 541 |
name: "if not nil function", |
| 542 |
before: ` |
| 543 |
package foo |
| 544 |
|
| 545 |
func _() { |
| 546 |
var foo func() |
| 547 |
foo.ifnotnil |
| 548 |
} |
| 549 |
`, |
| 550 |
after: ` |
| 551 |
package foo |
| 552 |
|
| 553 |
func _() { |
| 554 |
var foo func() |
| 555 |
if foo != nil { |
| 556 |
$0 |
| 557 |
} |
| 558 |
} |
| 559 |
`, |
| 560 |
}, |
| 561 |
{ |
| 562 |
name: "slice_len", |
| 563 |
before: ` |
| 564 |
package foo |
| 565 |
|
| 566 |
func _() { |
| 567 |
var foo []int |
| 568 |
foo.len |
| 569 |
} |
| 570 |
`, |
| 571 |
after: ` |
| 572 |
package foo |
| 573 |
|
| 574 |
func _() { |
| 575 |
var foo []int |
| 576 |
len(foo) |
| 577 |
} |
| 578 |
`, |
| 579 |
}, |
| 580 |
{ |
| 581 |
name: "map_len", |
| 582 |
before: ` |
| 583 |
package foo |
| 584 |
|
| 585 |
func _() { |
| 586 |
var foo map[string]int |
| 587 |
foo.len |
| 588 |
} |
| 589 |
`, |
| 590 |
after: ` |
| 591 |
package foo |
| 592 |
|
| 593 |
func _() { |
| 594 |
var foo map[string]int |
| 595 |
len(foo) |
| 596 |
} |
| 597 |
`, |
| 598 |
}, |
| 599 |
{ |
| 600 |
name: "slice_for", |
| 601 |
allowMultipleItem: true, |
| 602 |
before: ` |
| 603 |
package foo |
| 604 |
|
| 605 |
func _() { |
| 606 |
var foo []int |
| 607 |
foo.for |
| 608 |
} |
| 609 |
`, |
| 610 |
after: ` |
| 611 |
package foo |
| 612 |
|
| 613 |
func _() { |
| 614 |
var foo []int |
| 615 |
for ${1:} := range foo { |
| 616 |
$0 |
| 617 |
} |
| 618 |
} |
| 619 |
`, |
| 620 |
}, |
| 621 |
{ |
| 622 |
name: "map_for", |
| 623 |
allowMultipleItem: true, |
| 624 |
before: ` |
| 625 |
package foo |
| 626 |
|
| 627 |
func _() { |
| 628 |
var foo map[string]int |
| 629 |
foo.for |
| 630 |
} |
| 631 |
`, |
| 632 |
after: ` |
| 633 |
package foo |
| 634 |
|
| 635 |
func _() { |
| 636 |
var foo map[string]int |
| 637 |
for ${1:} := range foo { |
| 638 |
$0 |
| 639 |
} |
| 640 |
} |
| 641 |
`, |
| 642 |
}, |
| 643 |
{ |
| 644 |
name: "chan_for", |
| 645 |
allowMultipleItem: true, |
| 646 |
before: ` |
| 647 |
package foo |
| 648 |
|
| 649 |
func _() { |
| 650 |
var foo chan int |
| 651 |
foo.for |
| 652 |
} |
| 653 |
`, |
| 654 |
after: ` |
| 655 |
package foo |
| 656 |
|
| 657 |
func _() { |
| 658 |
var foo chan int |
| 659 |
for ${1:} := range foo { |
| 660 |
$0 |
| 661 |
} |
| 662 |
} |
| 663 |
`, |
| 664 |
}, |
| 665 |
{ |
| 666 |
name: "slice_forr", |
| 667 |
before: ` |
| 668 |
package foo |
| 669 |
|
| 670 |
func _() { |
| 671 |
var foo []int |
| 672 |
foo.forr |
| 673 |
} |
| 674 |
`, |
| 675 |
after: ` |
| 676 |
package foo |
| 677 |
|
| 678 |
func _() { |
| 679 |
var foo []int |
| 680 |
for ${1:}, ${2:} := range foo { |
| 681 |
$0 |
| 682 |
} |
| 683 |
} |
| 684 |
`, |
| 685 |
}, |
| 686 |
{ |
| 687 |
name: "slice_forr", |
| 688 |
before: ` |
| 689 |
package foo |
| 690 |
|
| 691 |
func _() { |
| 692 |
var foo []int |
| 693 |
foo.forr |
| 694 |
} |
| 695 |
`, |
| 696 |
after: ` |
| 697 |
package foo |
| 698 |
|
| 699 |
func _() { |
| 700 |
var foo []int |
| 701 |
for ${1:}, ${2:} := range foo { |
| 702 |
$0 |
| 703 |
} |
| 704 |
} |
| 705 |
`, |
| 706 |
}, |
| 707 |
{ |
| 708 |
name: "map_forr", |
| 709 |
before: ` |
| 710 |
package foo |
| 711 |
|
| 712 |
func _() { |
| 713 |
var foo map[string]int |
| 714 |
foo.forr |
| 715 |
} |
| 716 |
`, |
| 717 |
after: ` |
| 718 |
package foo |
| 719 |
|
| 720 |
func _() { |
| 721 |
var foo map[string]int |
| 722 |
for ${1:}, ${2:} := range foo { |
| 723 |
$0 |
| 724 |
} |
| 725 |
} |
| 726 |
`, |
| 727 |
}, |
| 728 |
} |
| 729 |
|
| 730 |
r := WithOptions( |
| 731 |
Settings{ |
| 732 |
"experimentalPostfixCompletions": true, |
| 733 |
}, |
| 734 |
) |
| 735 |
r.Run(t, mod, func(t *testing.T, env *Env) { |
| 736 |
env.CreateBuffer("foo.go", "") |
| 737 |
|
| 738 |
for _, c := range cases { |
| 739 |
t.Run(c.name, func(t *testing.T) { |
| 740 |
c.before = strings.Trim(c.before, "\n") |
| 741 |
c.after = strings.Trim(c.after, "\n") |
| 742 |
|
| 743 |
env.SetBufferContent("foo.go", c.before) |
| 744 |
|
| 745 |
loc := env.RegexpSearch("foo.go", `()\n\}`) |
| 746 |
completions := env.Completion(loc) |
| 747 |
if len(completions.Items) < 1 { |
| 748 |
t.Fatalf("expected at least one completion, got %v", completions.Items) |
| 749 |
} |
| 750 |
if !c.allowMultipleItem && len(completions.Items) > 1 { |
| 751 |
t.Fatalf("expected one completion, got %v", completions.Items) |
| 752 |
} |
| 753 |
|
| 754 |
env.AcceptCompletion(loc, completions.Items[0]) |
| 755 |
|
| 756 |
if buf := env.BufferText("foo.go"); buf != c.after { |
| 757 |
t.Errorf("\nGOT:\n%s\nEXPECTED:\n%s", buf, c.after) |
| 758 |
} |
| 759 |
}) |
| 760 |
} |
| 761 |
}) |
| 762 |
} |