@@ -41,13 +41,13 @@ const message = "Hello World."
41
41
func TestGoToInternalDefinition (t * testing.T ) {
42
42
Run (t , internalDefinition , func (t * testing.T , env * Env ) {
43
43
env .OpenFile ("main.go" )
44
- loc := env .GoToDefinition (env .RegexpSearch ("main.go" , "message" ))
44
+ loc := env .FirstDefinition (env .RegexpSearch ("main.go" , "message" ))
45
45
name := env .Sandbox .Workdir .URIToPath (loc .URI )
46
46
if want := "const.go" ; name != want {
47
- t .Errorf ("GoToDefinition : got file %q, want %q" , name , want )
47
+ t .Errorf ("Definition : got file %q, want %q" , name , want )
48
48
}
49
49
if want := env .RegexpSearch ("const.go" , "message" ); loc != want {
50
- t .Errorf ("GoToDefinition : got location %v, want %v" , loc , want )
50
+ t .Errorf ("Definition : got location %v, want %v" , loc , want )
51
51
}
52
52
})
53
53
}
@@ -90,13 +90,13 @@ func TestGoToLinknameDefinition(t *testing.T) {
90
90
91
91
// Jump from directives 2nd arg.
92
92
start := env .RegexpSearch ("upper/upper.go" , `lower.bar` )
93
- loc := env .GoToDefinition (start )
93
+ loc := env .FirstDefinition (start )
94
94
name := env .Sandbox .Workdir .URIToPath (loc .URI )
95
95
if want := "lower/lower.go" ; name != want {
96
- t .Errorf ("GoToDefinition : got file %q, want %q" , name , want )
96
+ t .Errorf ("Definition : got file %q, want %q" , name , want )
97
97
}
98
98
if want := env .RegexpSearch ("lower/lower.go" , `bar` ); loc != want {
99
- t .Errorf ("GoToDefinition : got position %v, want %v" , loc , want )
99
+ t .Errorf ("Definition : got position %v, want %v" , loc , want )
100
100
}
101
101
})
102
102
}
@@ -139,13 +139,13 @@ func TestGoToLinknameDefinitionInReverseDep(t *testing.T) {
139
139
140
140
// Jump from directives 2nd arg.
141
141
start := env .RegexpSearch ("lower/lower.go" , `upper.foo` )
142
- loc := env .GoToDefinition (start )
142
+ loc := env .FirstDefinition (start )
143
143
name := env .Sandbox .Workdir .URIToPath (loc .URI )
144
144
if want := "upper/upper.go" ; name != want {
145
- t .Errorf ("GoToDefinition : got file %q, want %q" , name , want )
145
+ t .Errorf ("Definition : got file %q, want %q" , name , want )
146
146
}
147
147
if want := env .RegexpSearch ("upper/upper.go" , `foo` ); loc != want {
148
- t .Errorf ("GoToDefinition : got position %v, want %v" , loc , want )
148
+ t .Errorf ("Definition : got position %v, want %v" , loc , want )
149
149
}
150
150
})
151
151
}
@@ -178,13 +178,13 @@ func TestGoToLinknameDefinitionDisconnected(t *testing.T) {
178
178
179
179
// Jump from directives 2nd arg.
180
180
start := env .RegexpSearch ("a/a.go" , `b.bar` )
181
- loc := env .GoToDefinition (start )
181
+ loc := env .FirstDefinition (start )
182
182
name := env .Sandbox .Workdir .URIToPath (loc .URI )
183
183
if want := "b/b.go" ; name != want {
184
- t .Errorf ("GoToDefinition : got file %q, want %q" , name , want )
184
+ t .Errorf ("Definition : got file %q, want %q" , name , want )
185
185
}
186
186
if want := env .RegexpSearch ("b/b.go" , `bar` ); loc != want {
187
- t .Errorf ("GoToDefinition : got position %v, want %v" , loc , want )
187
+ t .Errorf ("Definition : got position %v, want %v" , loc , want )
188
188
}
189
189
})
190
190
}
@@ -206,30 +206,32 @@ func main() {
206
206
func TestGoToStdlibDefinition_Issue37045 (t * testing.T ) {
207
207
Run (t , stdlibDefinition , func (t * testing.T , env * Env ) {
208
208
env .OpenFile ("main.go" )
209
- loc := env .GoToDefinition (env .RegexpSearch ("main.go" , `fmt.(Printf)` ))
209
+ loc := env .FirstDefinition (env .RegexpSearch ("main.go" , `fmt.(Printf)` ))
210
210
name := env .Sandbox .Workdir .URIToPath (loc .URI )
211
211
if got , want := path .Base (name ), "print.go" ; got != want {
212
- t .Errorf ("GoToDefinition : got file %q, want %q" , name , want )
212
+ t .Errorf ("Definition : got file %q, want %q" , name , want )
213
213
}
214
+ env .OpenFile (name )
214
215
215
216
// Test that we can jump to definition from outside our workspace.
216
217
// See golang.org/issues/37045.
217
- newLoc := env .GoToDefinition (loc )
218
+ newLoc := env .FirstDefinition (loc )
218
219
newName := env .Sandbox .Workdir .URIToPath (newLoc .URI )
219
220
if newName != name {
220
- t .Errorf ("GoToDefinition is not idempotent: got %q, want %q" , newName , name )
221
+ t .Errorf ("Definition is not idempotent: got %q, want %q" , newName , name )
221
222
}
222
223
if newLoc != loc {
223
- t .Errorf ("GoToDefinition is not idempotent: got %v, want %v" , newLoc , loc )
224
+ t .Errorf ("Definition is not idempotent: got %v, want %v" , newLoc , loc )
224
225
}
225
226
})
226
227
}
227
228
228
229
func TestUnexportedStdlib_Issue40809 (t * testing.T ) {
229
230
Run (t , stdlibDefinition , func (t * testing.T , env * Env ) {
230
231
env .OpenFile ("main.go" )
231
- loc := env .GoToDefinition (env .RegexpSearch ("main.go" , `fmt.(Printf)` ))
232
+ loc := env .FirstDefinition (env .RegexpSearch ("main.go" , `fmt.(Printf)` ))
232
233
name := env .Sandbox .Workdir .URIToPath (loc .URI )
234
+ env .OpenFile (name )
233
235
234
236
loc = env .RegexpSearch (name , `:=\s*(newPrinter)\(\)` )
235
237
@@ -239,7 +241,7 @@ func TestUnexportedStdlib_Issue40809(t *testing.T) {
239
241
t .Errorf ("expected 5+ references to newPrinter, found: %#v" , refs )
240
242
}
241
243
242
- loc = env .GoToDefinition (loc )
244
+ loc = env .FirstDefinition (loc )
243
245
content , _ := env .Hover (loc )
244
246
if ! strings .Contains (content .Value , "newPrinter" ) {
245
247
t .Fatal ("definition of newPrinter went to the incorrect place" )
@@ -306,7 +308,7 @@ func main() {}
306
308
Settings {"importShortcut" : tt .importShortcut },
307
309
).Run (t , mod , func (t * testing.T , env * Env ) {
308
310
env .OpenFile ("main.go" )
309
- loc := env .GoToDefinition (env .RegexpSearch ("main.go" , `"fmt"` ))
311
+ loc := env .FirstDefinition (env .RegexpSearch ("main.go" , `"fmt"` ))
310
312
if loc == (protocol.Location {}) {
311
313
t .Fatalf ("expected definition, got none" )
312
314
}
@@ -354,7 +356,7 @@ func main() {}
354
356
Run (t , mod , func (t * testing.T , env * Env ) {
355
357
env .OpenFile ("main.go" )
356
358
357
- loc , err := env .Editor .TypeDefinition (env .Ctx , env .RegexpSearch ("main.go" , tt .re ))
359
+ locs , err := env .Editor .TypeDefinitions (env .Ctx , env .RegexpSearch ("main.go" , tt .re ))
358
360
if tt .wantError {
359
361
if err == nil {
360
362
t .Fatal ("expected error, got nil" )
@@ -364,10 +366,13 @@ func main() {}
364
366
if err != nil {
365
367
t .Fatalf ("expected nil error, got %s" , err )
366
368
}
369
+ if len (locs ) == 0 {
370
+ t .Fatalf ("TypeDefinitions: empty result" )
371
+ }
367
372
368
373
typeLoc := env .RegexpSearch ("main.go" , tt .wantTypeRe )
369
- if loc != typeLoc {
370
- t .Errorf ("invalid pos: want %+v, got %+v" , typeLoc , loc )
374
+ if locs [ 0 ] != typeLoc {
375
+ t .Errorf ("invalid pos: want %+v, got %+v" , typeLoc , locs [ 0 ] )
371
376
}
372
377
})
373
378
})
@@ -389,7 +394,16 @@ func F[T comparable]() {}
389
394
Run (t , mod , func (t * testing.T , env * Env ) {
390
395
env .OpenFile ("main.go" )
391
396
392
- _ = env .TypeDefinition (env .RegexpSearch ("main.go" , "comparable" )) // must not panic
397
+ // TypeDefinition of comparable should
398
+ // returns an empty result, not panic.
399
+ loc := env .RegexpSearch ("main.go" , "comparable" )
400
+ locs , err := env .Editor .TypeDefinitions (env .Ctx , loc )
401
+ if err != nil {
402
+ t .Fatal (err )
403
+ }
404
+ if len (locs ) > 0 {
405
+ t .Fatalf ("unexpected result: %v" , locs )
406
+ }
393
407
})
394
408
}
395
409
@@ -429,7 +443,7 @@ package client
429
443
`
430
444
Run (t , mod , func (t * testing.T , env * Env ) {
431
445
env .OpenFile ("client/client_role_test.go" )
432
- env .GoToDefinition (env .RegexpSearch ("client/client_role_test.go" , "RoleSetup" ))
446
+ env .FirstDefinition (env .RegexpSearch ("client/client_role_test.go" , "RoleSetup" ))
433
447
})
434
448
}
435
449
@@ -487,11 +501,11 @@ const _ = b.K
487
501
refLoc := env .RegexpSearch ("a.go" , "K" ) // find "b.K" reference
488
502
489
503
// Initially, b.K is defined in the module cache.
490
- gotLoc := env .GoToDefinition (refLoc )
504
+ gotLoc := env .FirstDefinition (refLoc )
491
505
gotFile := env .Sandbox .Workdir .URIToPath (gotLoc .URI )
492
506
wantCache := filepath .
ToSlash (
env .
Sandbox .
GOPATH ())
+ "/pkg/mod/other.com/[email protected] /b.go"
493
507
if gotFile != wantCache {
494
- t .Errorf ("GoToDefinition , before: got file %q, want %q" , gotFile , wantCache )
508
+ t .Errorf ("Definition , before: got file %q, want %q" , gotFile , wantCache )
495
509
}
496
510
497
511
// Run 'go mod vendor' outside the editor.
@@ -501,10 +515,10 @@ const _ = b.K
501
515
env .Await (env .DoneWithChangeWatchedFiles ())
502
516
503
517
// Now, b.K is defined in the vendor tree.
504
- gotLoc = env .GoToDefinition (refLoc )
518
+ gotLoc = env .FirstDefinition (refLoc )
505
519
wantVendor := "vendor/other.com/b/b.go"
506
520
if gotFile != wantVendor {
507
- t .Errorf ("GoToDefinition , after go mod vendor: got file %q, want %q" , gotFile , wantVendor )
521
+ t .Errorf ("Definition , after go mod vendor: got file %q, want %q" , gotFile , wantVendor )
508
522
}
509
523
510
524
// Delete the vendor tree.
@@ -520,10 +534,10 @@ const _ = b.K
520
534
env .Await (env .DoneWithChangeWatchedFiles ())
521
535
522
536
// b.K is once again defined in the module cache.
523
- gotLoc = env .GoToDefinition (gotLoc )
537
+ gotLoc = env .FirstDefinition (gotLoc )
524
538
gotFile = env .Sandbox .Workdir .URIToPath (gotLoc .URI )
525
539
if gotFile != wantCache {
526
- t .Errorf ("GoToDefinition , after rm -rf vendor: got file %q, want %q" , gotFile , wantCache )
540
+ t .Errorf ("Definition , after rm -rf vendor: got file %q, want %q" , gotFile , wantCache )
527
541
}
528
542
})
529
543
}
@@ -554,16 +568,16 @@ FOO
554
568
SKIP
555
569
`
556
570
557
- func TestGoToEmbedDefinition (t * testing.T ) {
571
+ func TestEmbedDefinition (t * testing.T ) {
558
572
Run (t , embedDefinition , func (t * testing.T , env * Env ) {
559
573
env .OpenFile ("main.go" )
560
574
561
575
start := env .RegexpSearch ("main.go" , `\*.txt` )
562
- loc := env .GoToDefinition (start )
576
+ loc := env .FirstDefinition (start )
563
577
564
578
name := env .Sandbox .Workdir .URIToPath (loc .URI )
565
579
if want := "foo.txt" ; name != want {
566
- t .Errorf ("GoToDefinition : got file %q, want %q" , name , want )
580
+ t .Errorf ("Definition : got file %q, want %q" , name , want )
567
581
}
568
582
})
569
583
}
@@ -588,10 +602,10 @@ func _(err error) {
588
602
env .OpenFile ("a.go" )
589
603
590
604
start := env .RegexpSearch ("a.go" , `Error` )
591
- loc := env .GoToDefinition (start )
605
+ loc := env .FirstDefinition (start )
592
606
593
607
if ! strings .HasSuffix (string (loc .URI ), "builtin.go" ) {
594
- t .Errorf ("GoToDefinition (err.Error) = %#v, want builtin.go" , loc )
608
+ t .Errorf ("Definition (err.Error) = %#v, want builtin.go" , loc )
595
609
}
596
610
})
597
611
}
@@ -628,13 +642,13 @@ var _ = foo(123) // call
628
642
629
643
// Definition at the call"foo(123)" takes us to the Go declaration.
630
644
callLoc := env .RegexpSearch ("a.go" , regexp .QuoteMeta ("foo(123)" ))
631
- declLoc := env .GoToDefinition (callLoc )
645
+ declLoc := env .FirstDefinition (callLoc )
632
646
if got , want := locString (declLoc ), "a.go:5:5-5:8" ; got != want {
633
647
t .Errorf ("Definition(call): got %s, want %s" , got , want )
634
648
}
635
649
636
650
// Definition a second time takes us to the assembly implementation.
637
- implLoc := env .GoToDefinition (declLoc )
651
+ implLoc := env .FirstDefinition (declLoc )
638
652
if got , want := locString (implLoc ), "foo_darwin_arm64.s:2:6-2:9" ; got != want {
639
653
t .Errorf ("Definition(go decl): got %s, want %s" , got , want )
640
654
}
@@ -670,7 +684,7 @@ func Foo() {
670
684
env .OpenFile ("a.go" )
671
685
672
686
fooLoc := env .RegexpSearch ("a.go" , "()Foo" )
673
- loc0 := env .GoToDefinition (fooLoc )
687
+ loc0 := env .FirstDefinition (fooLoc )
674
688
675
689
// Insert a space that will be removed by formatting.
676
690
env .EditBuffer ("a.go" , protocol.TextEdit {
@@ -679,7 +693,7 @@ func Foo() {
679
693
})
680
694
env .SaveBuffer ("a.go" ) // reformats the file before save
681
695
env .AfterChange ()
682
- loc1 := env .GoToDefinition (env .RegexpSearch ("a.go" , "Foo" ))
696
+ loc1 := env .FirstDefinition (env .RegexpSearch ("a.go" , "Foo" ))
683
697
if diff := cmp .Diff (loc0 , loc1 ); diff != "" {
684
698
t .Errorf ("mismatching locations (-want +got):\n %s" , diff )
685
699
}
0 commit comments