From 2077df36852e9a22c3b78f535833d3e54e9fcc8a Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 24 Oct 2019 18:15:23 -0400 Subject: [PATCH] internal/lsp: remove analyzers from Analyze result Also, address minor comments that I ignored from Heschi because I am obnoxious. Change-Id: I99dcac38578585af2cdd951dd2b9755732ef945f Reviewed-on: https://go-review.googlesource.com/c/tools/+/203281 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick TryBot-Result: Gobot Gobot --- internal/lsp/cache/analysis.go | 10 ++++----- internal/lsp/code_action.go | 3 +-- internal/lsp/source/diagnostics.go | 36 ++++++++++++++---------------- internal/lsp/source/view.go | 2 +- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/internal/lsp/cache/analysis.go b/internal/lsp/cache/analysis.go index 1f8c44cd1f..e707f7365f 100644 --- a/internal/lsp/cache/analysis.go +++ b/internal/lsp/cache/analysis.go @@ -17,7 +17,7 @@ import ( errors "golang.org/x/xerrors" ) -func (s *snapshot) Analyze(ctx context.Context, id string, analyzers []*analysis.Analyzer) (map[*analysis.Analyzer][]*source.Error, error) { +func (s *snapshot) Analyze(ctx context.Context, id string, analyzers []*analysis.Analyzer) ([]*source.Error, error) { var roots []*actionHandle for _, a := range analyzers { @@ -34,14 +34,14 @@ func (s *snapshot) Analyze(ctx context.Context, id string, analyzers []*analysis return nil, ctx.Err() } - results := make(map[*analysis.Analyzer][]*source.Error) + var results []*source.Error for _, ah := range roots { diagnostics, _, err := ah.analyze(ctx) if err != nil { log.Error(ctx, "no results", err) continue } - results[ah.analyzer] = diagnostics + results = append(results, diagnostics...) } return results, nil } @@ -123,7 +123,7 @@ func (s *snapshot) actionHandle(ctx context.Context, id packageID, mode source.P } h := s.view.session.cache.store.Bind(buildActionKey(a, cph), func(ctx context.Context) interface{} { data := &actionData{} - data.diagnostics, data.result, data.err = run(ctx, s.view.session.cache.fset, ah) + data.diagnostics, data.result, data.err = runAnalysis(ctx, s.view.session.cache.fset, ah) return data }) ah.handle = h @@ -186,7 +186,7 @@ func execAll(ctx context.Context, fset *token.FileSet, actions []*actionHandle) return diagnostics, results, g.Wait() } -func run(ctx context.Context, fset *token.FileSet, act *actionHandle) ([]*source.Error, interface{}, error) { +func runAnalysis(ctx context.Context, fset *token.FileSet, act *actionHandle) ([]*source.Error, interface{}, error) { // Analyze dependencies. _, depResults, err := execAll(ctx, fset, act.deps) if err != nil { diff --git a/internal/lsp/code_action.go b/internal/lsp/code_action.go index 386865b496..0d3966146b 100644 --- a/internal/lsp/code_action.go +++ b/internal/lsp/code_action.go @@ -28,10 +28,9 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara } snapshot := view.Snapshot() - fh := snapshot.Handle(ctx, f) // Determine the supported actions for this file kind. - fileKind := fh.Identity().Kind + fileKind := snapshot.Handle(ctx, f).Identity().Kind supportedCodeActions, ok := view.Options().SupportedCodeActions[fileKind] if !ok { return nil, fmt.Errorf("no supported code actions for %v file kind", fileKind) diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index 6d6745c518..fe4d38a438 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -172,26 +172,24 @@ func analyses(ctx context.Context, snapshot Snapshot, cph CheckPackageHandle, di } // Report diagnostics and errors from root analyzers. - for _, diags := range diagnostics { - for _, e := range diags { - // This is a bit of a hack, but clients > 3.15 will be able to grey out unnecessary code. - // If we are deleting code as part of all of our suggested fixes, assume that this is dead code. - // TODO(golang/go/#34508): Return these codes from the diagnostics themselves. - var tags []protocol.DiagnosticTag - if onlyDeletions(e.SuggestedFixes) { - tags = append(tags, protocol.Unnecessary) - } - addReport(snapshot.View(), reports, Diagnostic{ - URI: e.URI, - Range: e.Range, - Message: e.Message, - Source: e.Category, - Severity: protocol.SeverityWarning, - Tags: tags, - SuggestedFixes: e.SuggestedFixes, - Related: e.Related, - }) + for _, e := range diagnostics { + // This is a bit of a hack, but clients > 3.15 will be able to grey out unnecessary code. + // If we are deleting code as part of all of our suggested fixes, assume that this is dead code. + // TODO(golang/go/#34508): Return these codes from the diagnostics themselves. + var tags []protocol.DiagnosticTag + if onlyDeletions(e.SuggestedFixes) { + tags = append(tags, protocol.Unnecessary) } + addReport(snapshot.View(), reports, Diagnostic{ + URI: e.URI, + Range: e.Range, + Message: e.Message, + Source: e.Category, + Severity: protocol.SeverityWarning, + Tags: tags, + SuggestedFixes: e.SuggestedFixes, + Related: e.Related, + }) } return nil } diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index b18db114bd..fad38dc68d 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -262,7 +262,7 @@ type Snapshot interface { View() View // Analyze runs the analyses for the given package at this snapshot. - Analyze(ctx context.Context, id string, analyzers []*analysis.Analyzer) (map[*analysis.Analyzer][]*Error, error) + Analyze(ctx context.Context, id string, analyzers []*analysis.Analyzer) ([]*Error, error) // FindAnalysisError returns the analysis error represented by the diagnostic. // This is used to get the SuggestedFixes associated with that error.