@@ -533,27 +533,17 @@ checkFiles:
533
533
// Views and viewDefinitions.
534
534
type viewDefiner interface { definition () * viewDefinition }
535
535
536
- // bestView returns the best View or viewDefinition that contains the
537
- // given file, or (nil, nil) if no matching view is found.
538
- //
539
- // bestView only returns an error in the event of context cancellation.
540
- //
541
- // Making this function generic is convenient so that we can avoid mapping view
542
- // definitions back to views inside Session.DidModifyFiles, where performance
543
- // matters. It is, however, not the cleanest application of generics.
536
+ // BestViews returns the most relevant subset of views for a given uri.
544
537
//
545
- // Note: keep this function in sync with defineView.
546
- func bestView [V viewDefiner ](ctx context.Context , fs file.Source , fh file.Handle , views []V ) (V , error ) {
547
- var zero V
548
-
538
+ // This may be used to filter diagnostics to the most relevant builds.
539
+ func BestViews [V viewDefiner ](ctx context.Context , fs file.Source , uri protocol.DocumentURI , views []V ) ([]V , error ) {
549
540
if len (views ) == 0 {
550
- return zero , nil // avoid the call to findRootPattern
541
+ return nil , nil // avoid the call to findRootPattern
551
542
}
552
- uri := fh .URI ()
553
543
dir := uri .Dir ()
554
544
modURI , err := findRootPattern (ctx , dir , "go.mod" , fs )
555
545
if err != nil {
556
- return zero , err
546
+ return nil , err
557
547
}
558
548
559
549
// Prefer GoWork > GoMod > GOPATH > GoPackages > AdHoc.
@@ -631,8 +621,26 @@ func bestView[V viewDefiner](ctx context.Context, fs file.Source, fh file.Handle
631
621
bestViews = goPackagesViews
632
622
case len (adHocViews ) > 0 :
633
623
bestViews = adHocViews
634
- default :
635
- return zero , nil
624
+ }
625
+
626
+ return bestViews , nil
627
+ }
628
+
629
+ // bestView returns the best View or viewDefinition that contains the
630
+ // given file, or (nil, nil) if no matching view is found.
631
+ //
632
+ // bestView only returns an error in the event of context cancellation.
633
+ //
634
+ // Making this function generic is convenient so that we can avoid mapping view
635
+ // definitions back to views inside Session.DidModifyFiles, where performance
636
+ // matters. It is, however, not the cleanest application of generics.
637
+ //
638
+ // Note: keep this function in sync with defineView.
639
+ func bestView [V viewDefiner ](ctx context.Context , fs file.Source , fh file.Handle , views []V ) (V , error ) {
640
+ var zero V
641
+ bestViews , err := BestViews (ctx , fs , fh .URI (), views )
642
+ if err != nil || len (bestViews ) == 0 {
643
+ return zero , err
636
644
}
637
645
638
646
content , err := fh .Content ()
0 commit comments