@@ -287,6 +287,54 @@ func hashUnsavedOverlays(files map[span.URI]source.VersionedFileHandle) string {
287
287
func (s * snapshot ) PackagesForFile (ctx context.Context , uri span.URI , mode source.TypecheckMode ) ([]source.Package , error ) {
288
288
ctx = event .Label (ctx , tag .URI .Of (uri ))
289
289
290
+ phs , err := s .packageHandlesForFile (ctx , uri , mode )
291
+ if err != nil {
292
+ return nil , err
293
+ }
294
+ var pkgs []source.Package
295
+ for _ , ph := range phs {
296
+ pkg , err := ph .check (ctx , s )
297
+ if err != nil {
298
+ return nil , err
299
+ }
300
+ pkgs = append (pkgs , pkg )
301
+ }
302
+ return pkgs , nil
303
+ }
304
+
305
+ func (s * snapshot ) PackageForFile (ctx context.Context , uri span.URI , mode source.TypecheckMode , pkgPolicy source.PackageFilter ) (source.Package , error ) {
306
+ ctx = event .Label (ctx , tag .URI .Of (uri ))
307
+
308
+ phs , err := s .packageHandlesForFile (ctx , uri , mode )
309
+ if err != nil {
310
+ return nil , err
311
+ }
312
+
313
+ if len (phs ) < 1 {
314
+ return nil , errors .Errorf ("no packages" )
315
+ }
316
+
317
+ ph := phs [0 ]
318
+ for _ , handle := range phs [1 :] {
319
+ switch pkgPolicy {
320
+ case source .WidestPackage :
321
+ if ph == nil || len (handle .CompiledGoFiles ()) > len (ph .CompiledGoFiles ()) {
322
+ ph = handle
323
+ }
324
+ case source .NarrowestPackage :
325
+ if ph == nil || len (handle .CompiledGoFiles ()) < len (ph .CompiledGoFiles ()) {
326
+ ph = handle
327
+ }
328
+ }
329
+ }
330
+ if ph == nil {
331
+ return nil , errors .Errorf ("no packages in input" )
332
+ }
333
+
334
+ return ph .check (ctx , s )
335
+ }
336
+
337
+ func (s * snapshot ) packageHandlesForFile (ctx context.Context , uri span.URI , mode source.TypecheckMode ) ([]* packageHandle , error ) {
290
338
// Check if we should reload metadata for the file. We don't invalidate IDs
291
339
// (though we should), so the IDs will be a better source of truth than the
292
340
// metadata. If there are no IDs for the file, then we should also reload.
@@ -310,7 +358,7 @@ func (s *snapshot) PackagesForFile(ctx context.Context, uri span.URI, mode sourc
310
358
}
311
359
}
312
360
// Get the list of IDs from the snapshot again, in case it has changed.
313
- var pkgs []source. Package
361
+ var phs []* packageHandle
314
362
for _ , id := range s .getIDsForURI (uri ) {
315
363
var parseModes []source.ParseMode
316
364
switch mode {
@@ -327,22 +375,15 @@ func (s *snapshot) PackagesForFile(ctx context.Context, uri span.URI, mode sourc
327
375
}
328
376
329
377
for _ , parseMode := range parseModes {
330
- pkg , err := s .checkedPackage (ctx , id , parseMode )
378
+ ph , err := s .buildPackageHandle (ctx , id , parseMode )
331
379
if err != nil {
332
380
return nil , err
333
381
}
334
- pkgs = append (pkgs , pkg )
382
+ phs = append (phs , ph )
335
383
}
336
384
}
337
- return pkgs , nil
338
- }
339
385
340
- func (s * snapshot ) checkedPackage (ctx context.Context , id packageID , mode source.ParseMode ) (* pkg , error ) {
341
- ph , err := s .buildPackageHandle (ctx , id , mode )
342
- if err != nil {
343
- return nil , err
344
- }
345
- return ph .check (ctx , s )
386
+ return phs , nil
346
387
}
347
388
348
389
func (s * snapshot ) GetReverseDependencies (ctx context.Context , id string ) ([]source.Package , error ) {
@@ -366,6 +407,14 @@ func (s *snapshot) GetReverseDependencies(ctx context.Context, id string) ([]sou
366
407
return pkgs , nil
367
408
}
368
409
410
+ func (s * snapshot ) checkedPackage (ctx context.Context , id packageID , mode source.ParseMode ) (* pkg , error ) {
411
+ ph , err := s .buildPackageHandle (ctx , id , mode )
412
+ if err != nil {
413
+ return nil , err
414
+ }
415
+ return ph .check (ctx , s )
416
+ }
417
+
369
418
// transitiveReverseDependencies populates the uris map with file URIs
370
419
// belonging to the provided package and its transitive reverse dependencies.
371
420
func (s * snapshot ) transitiveReverseDependencies (id packageID , ids map [packageID ]struct {}) {
0 commit comments