1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:04:44 -07:00

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 <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-08-05 19:27:28 -04:00
parent 982211fce4
commit 1dcc99b65a
4 changed files with 34 additions and 17 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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,

View File

@ -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