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>
diff --git a/internal/lsp/cmd/imports.go b/internal/lsp/cmd/imports.go
index 2b8b13e..2127e25 100644
--- a/internal/lsp/cmd/imports.go
+++ b/internal/lsp/cmd/imports.go
@@ -69,11 +69,7 @@
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
}
diff --git a/internal/lsp/cmd/suggested_fix.go b/internal/lsp/cmd/suggested_fix.go
index 2687821..15c6bc9 100644
--- a/internal/lsp/cmd/suggested_fix.go
+++ b/internal/lsp/cmd/suggested_fix.go
@@ -87,11 +87,7 @@
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
}
diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go
index 4ddc5ab..23d3927 100644
--- a/internal/lsp/lsp_test.go
+++ b/internal/lsp/lsp_test.go
@@ -314,9 +314,8 @@
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 @@
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)
}
diff --git a/internal/lsp/protocol/tsserver.go b/internal/lsp/protocol/tsserver.go
index f6593cb..8850d99 100644
--- a/internal/lsp/protocol/tsserver.go
+++ b/internal/lsp/protocol/tsserver.go
@@ -3,7 +3,7 @@
// 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 @@
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 @@
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 @@
log.Error(ctx, "", err)
}
return true
- case "$/cancelRequest": // notif
- var params CancelParams
- if err := json.Unmarshal(*r.Params, ¶ms); err != nil {
- sendParseError(ctx, r, err)
- return true
- }
- if err := h.server.CancelRequest(ctx, ¶ms); err != nil {
- log.Error(ctx, "", err)
- }
- return true
case "$/progress": // notif
var params ProgressParams
if err := json.Unmarshal(*r.Params, ¶ms); err != nil {
@@ -587,10 +576,6 @@
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 @@
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
}
diff --git a/internal/lsp/server.go b/internal/lsp/server.go
index c779ada..c13047c 100644
--- a/internal/lsp/server.go
+++ b/internal/lsp/server.go
@@ -201,7 +201,7 @@
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)
}