golang.org/x/tools/gopls@v0.15.3/internal/golang/diagnostics.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package golang
     6  
     7  import (
     8  	"context"
     9  
    10  	"golang.org/x/tools/gopls/internal/cache"
    11  	"golang.org/x/tools/gopls/internal/cache/metadata"
    12  	"golang.org/x/tools/gopls/internal/progress"
    13  	"golang.org/x/tools/gopls/internal/protocol"
    14  	"golang.org/x/tools/gopls/internal/settings"
    15  	"golang.org/x/tools/gopls/internal/util/maps"
    16  )
    17  
    18  // Analyze reports go/analysis-framework diagnostics in the specified package.
    19  //
    20  // If the provided tracker is non-nil, it may be used to provide notifications
    21  // of the ongoing analysis pass.
    22  func Analyze(ctx context.Context, snapshot *cache.Snapshot, pkgIDs map[PackageID]*metadata.Package, tracker *progress.Tracker) (map[protocol.DocumentURI][]*cache.Diagnostic, error) {
    23  	// Exit early if the context has been canceled. This also protects us
    24  	// from a race on Options, see golang/go#36699.
    25  	if ctx.Err() != nil {
    26  		return nil, ctx.Err()
    27  	}
    28  
    29  	options := snapshot.Options()
    30  	categories := []map[string]*settings.Analyzer{
    31  		options.DefaultAnalyzers,
    32  		options.StaticcheckAnalyzers,
    33  	}
    34  
    35  	var analyzers []*settings.Analyzer
    36  	for _, cat := range categories {
    37  		for _, a := range cat {
    38  			analyzers = append(analyzers, a)
    39  		}
    40  	}
    41  
    42  	analysisDiagnostics, err := snapshot.Analyze(ctx, pkgIDs, analyzers, tracker)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	byURI := func(d *cache.Diagnostic) protocol.DocumentURI { return d.URI }
    47  	return maps.Group(analysisDiagnostics, byURI), nil
    48  }