From 1dcc99b65a21c088c193b0ee94fe25cd9d16276e Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Mon, 5 Aug 2019 19:27:28 -0400 Subject: [PATCH] internal/lsp: do not show errors for code actions on go.mod files This change keys the supported code actions map by file kind, so that we can extend it more easily for go.mod files. Change-Id: Ic28f91bd517700cf070281b1c4d4ded14a702790 Reviewed-on: https://go-review.googlesource.com/c/tools/+/189039 Run-TryBot: Rebecca Stambler Reviewed-by: Ian Cottrell --- internal/lsp/code_action.go | 28 +++++++++++++++++++--------- internal/lsp/general.go | 10 +++++++--- internal/lsp/lsp_test.go | 11 +++++++---- internal/lsp/server.go | 2 +- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/internal/lsp/code_action.go b/internal/lsp/code_action.go index 44475324838..feae17b09ae 100644 --- a/internal/lsp/code_action.go +++ b/internal/lsp/code_action.go @@ -18,29 +18,35 @@ import ( ) func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) { + uri := span.NewURI(params.TextDocument.URI) + view := s.session.ViewOf(uri) + f, m, err := getSourceFile(ctx, view, uri) + if err != nil { + return nil, err + } + + // Determine the supported actions for this file kind. + fileKind := f.Handle(ctx).Kind() + supportedCodeActions, ok := s.supportedCodeActions[fileKind] + if !ok { + return nil, fmt.Errorf("no supported code actions for %v file kind", fileKind) + } // The Only field of the context specifies which code actions the client wants. // If Only is empty, assume that the client wants all of the possible code actions. var wanted map[protocol.CodeActionKind]bool if len(params.Context.Only) == 0 { - wanted = s.supportedCodeActions + wanted = supportedCodeActions } else { wanted = make(map[protocol.CodeActionKind]bool) for _, only := range params.Context.Only { - wanted[only] = s.supportedCodeActions[only] + wanted[only] = supportedCodeActions[only] } } - - uri := span.NewURI(params.TextDocument.URI) if len(wanted) == 0 { return nil, fmt.Errorf("no supported code action to execute for %s, wanted %v", uri, params.Context.Only) } - view := s.session.ViewOf(uri) - gof, m, err := getGoFile(ctx, view, uri) - if err != nil { - return nil, err - } spn, err := m.RangeSpan(params.Range) if err != nil { return nil, err @@ -58,6 +64,10 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara // First, add the quick fixes reported by go/analysis. // TODO: Enable this when this actually works. For now, it's needless work. if s.wantSuggestedFixes { + gof, ok := f.(source.GoFile) + if !ok { + return nil, fmt.Errorf("%s is not a Go file", f.URI()) + } qf, err := quickFixes(ctx, view, gof) if err != nil { log.Error(ctx, "quick fixes failed", err, telemetry.File.Of(uri)) diff --git a/internal/lsp/general.go b/internal/lsp/general.go index f1f93517942..3bedf96bbe9 100644 --- a/internal/lsp/general.go +++ b/internal/lsp/general.go @@ -42,9 +42,13 @@ func (s *Server) initialize(ctx context.Context, params *protocol.InitializePara // Default to using synopsis as a default for hover information. s.hoverKind = source.SynopsisDocumentation - s.supportedCodeActions = map[protocol.CodeActionKind]bool{ - protocol.SourceOrganizeImports: true, - protocol.QuickFix: true, + s.supportedCodeActions = map[source.FileKind]map[protocol.CodeActionKind]bool{ + source.Go: { + protocol.SourceOrganizeImports: true, + protocol.QuickFix: true, + }, + source.Mod: {}, + source.Sum: {}, } s.setClientCapabilities(params.Capabilities) diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 5b563cd507a..6f270eef879 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -52,10 +52,13 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) { server: &Server{ session: session, undelivered: make(map[span.URI][]source.Diagnostic), - supportedCodeActions: map[protocol.CodeActionKind]bool{ - protocol.SourceOrganizeImports: true, - protocol.QuickFix: true, - }, + supportedCodeActions: map[source.FileKind]map[protocol.CodeActionKind]bool{ + source.Go: { + protocol.SourceOrganizeImports: true, + protocol.QuickFix: true, + }, + source.Mod: {}, + source.Sum: {}}, hoverKind: source.SynopsisDocumentation, }, data: data, diff --git a/internal/lsp/server.go b/internal/lsp/server.go index 26d2c216f5c..6ab758b6c05 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -89,7 +89,7 @@ type Server struct { disabledAnalyses map[string]struct{} wantSuggestedFixes bool - supportedCodeActions map[protocol.CodeActionKind]bool + supportedCodeActions map[source.FileKind]map[protocol.CodeActionKind]bool textDocumentSyncKind protocol.TextDocumentSyncKind