internal/lsp: fix the return type of CodeAction()

Paul Jolly observes that returning interface{} is not helpful.
Now CodeAction() returns []CodeAction.

The type in typescript is (Command | CodeAction)[] | null
but the choice is up to gopls, which returns []CodeAction.

Fixes golang/go#35688, golang/go#35679

Change-Id: I91c22bb0752431954ae2f993cb7b44726cf60e5c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/207898
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Peter Weinberger 2019-11-19 08:42:45 -05:00
parent 07fc4c7f2b
commit 11e13f1c3f
5 changed files with 11 additions and 36 deletions

View File

@ -69,11 +69,7 @@ func (t *imports) Run(ctx context.Context, args ...string) error {
return errors.Errorf("%v: %v", from, err)
}
var edits []protocol.TextEdit
v, ok := actions.([]protocol.CodeAction)
if !ok {
return errors.Errorf("expected CodeAction, got %T", actions)
}
for _, a := range v {
for _, a := range actions {
if a.Title != "Organize Imports" {
continue
}

View File

@ -87,11 +87,7 @@ func (s *suggestedfix) Run(ctx context.Context, args ...string) error {
return errors.Errorf("%v: %v", from, err)
}
var edits []protocol.TextEdit
v, ok := actions.([]protocol.CodeAction)
if !ok {
return errors.Errorf("expected CodeAction, got %T", actions)
}
for _, a := range v {
for _, a := range actions {
if !a.IsPreferred && !s.All {
continue
}

View File

@ -314,9 +314,8 @@ func (r *runner) Import(t *testing.T, spn span.Span) {
t.Fatal(err)
}
got := string(m.Content)
xact := actions.([]protocol.CodeAction)
if len(xact) > 0 {
res, err := applyWorkspaceEdits(r, xact[0].Edit)
if len(actions) > 0 {
res, err := applyWorkspaceEdits(r, actions[0].Edit)
if err != nil {
t.Fatal(err)
}
@ -355,11 +354,10 @@ func (r *runner) SuggestedFix(t *testing.T, spn span.Span) {
t.Fatal(err)
}
// TODO: This test should probably be able to handle multiple code actions.
xact := actions.([]protocol.CodeAction)
if len(xact) > 1 {
if len(actions) > 1 {
t.Fatal("expected only 1 code action")
}
res, err := applyWorkspaceEdits(r, xact[0].Edit)
res, err := applyWorkspaceEdits(r, actions[0].Edit)
if err != nil {
t.Fatal(err)
}

View File

@ -3,7 +3,7 @@ package protocol
// Package protocol contains data types and code for LSP jsonrpcs
// generated automatically from vscode-languageserver-node
// commit: 635ab1fe6f8c57ce9402e573d007f24d6d290fd3
// last fetched Sun Oct 13 2019 10:14:32 GMT-0400 (Eastern Daylight Time)
// last fetched Mon Oct 14 2019 09:09:30 GMT-0400 (Eastern Daylight Time)
// Code generated (see typescript/README.md) DO NOT EDIT.
@ -27,7 +27,6 @@ type Server interface {
DidSave(context.Context, *DidSaveTextDocumentParams) error
WillSave(context.Context, *WillSaveTextDocumentParams) error
DidChangeWatchedFiles(context.Context, *DidChangeWatchedFilesParams) error
CancelRequest(context.Context, *CancelParams) error
Progress(context.Context, *ProgressParams) error
SetTraceNotification(context.Context, *SetTraceParams) error
LogTraceNotification(context.Context, *LogTraceParams) error
@ -49,7 +48,7 @@ type Server interface {
References(context.Context, *ReferenceParams) ([]Location /*Location[] | null*/, error)
DocumentHighlight(context.Context, *DocumentHighlightParams) ([]DocumentHighlight /*DocumentHighlight[] | null*/, error)
DocumentSymbol(context.Context, *DocumentSymbolParams) ([]DocumentSymbol /*SymbolInformation[] | DocumentSymbol[] | null*/, error)
CodeAction(context.Context, *CodeActionParams) (interface{} /*Command | CodeAction*/ /*(Command | CodeAction)[] | null*/, error)
CodeAction(context.Context, *CodeActionParams) ([]CodeAction /*(Command | CodeAction)[] | null*/, error)
Symbol(context.Context, *WorkspaceSymbolParams) ([]SymbolInformation /*SymbolInformation[] | null*/, error)
CodeLens(context.Context, *CodeLensParams) ([]CodeLens /*CodeLens[] | null*/, error)
ResolveCodeLens(context.Context, *CodeLens) (*CodeLens, error)
@ -168,16 +167,6 @@ func (h serverHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, deliver
log.Error(ctx, "", err)
}
return true
case "$/cancelRequest": // notif
var params CancelParams
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, r, err)
return true
}
if err := h.server.CancelRequest(ctx, &params); err != nil {
log.Error(ctx, "", err)
}
return true
case "$/progress": // notif
var params ProgressParams
if err := json.Unmarshal(*r.Params, &params); err != nil {
@ -587,10 +576,6 @@ func (s *serverDispatcher) DidChangeWatchedFiles(ctx context.Context, params *Di
return s.Conn.Notify(ctx, "workspace/didChangeWatchedFiles", params)
}
func (s *serverDispatcher) CancelRequest(ctx context.Context, params *CancelParams) error {
return s.Conn.Notify(ctx, "$/cancelRequest", params)
}
func (s *serverDispatcher) Progress(ctx context.Context, params *ProgressParams) error {
return s.Conn.Notify(ctx, "$/progress", params)
}
@ -742,8 +727,8 @@ func (s *serverDispatcher) DocumentSymbol(ctx context.Context, params *DocumentS
return result, nil
}
func (s *serverDispatcher) CodeAction(ctx context.Context, params *CodeActionParams) (interface{} /*Command | CodeAction*/ /*(Command | CodeAction)[] | null*/, error) {
var result interface{} /*Command | CodeAction*/ /*(Command | CodeAction)[] | null*/
func (s *serverDispatcher) CodeAction(ctx context.Context, params *CodeActionParams) ([]CodeAction /*(Command | CodeAction)[] | null*/, error) {
var result []CodeAction /*(Command | CodeAction)[] | null*/
if err := s.Conn.Call(ctx, "textDocument/codeAction", params, &result); err != nil {
return nil, err
}

View File

@ -201,7 +201,7 @@ func (s *Server) DocumentSymbol(ctx context.Context, params *protocol.DocumentSy
return s.documentSymbol(ctx, params)
}
func (s *Server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) (interface{}, error) {
func (s *Server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
return s.codeAction(ctx, params)
}