From 3be912efc39582a0bcd69bfe420b47aa254f728e Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Fri, 10 Jan 2020 17:35:46 -0500 Subject: [PATCH] internal/lsp/protocol: support nonstandard requests Support arbitrary client->server requests in the generated code. This is primitive, with no strong typing, but should be good enough for simple requests. We can do something fancier later if we want. Change-Id: Ib83ff6aa49418bda5222b37cde3b150a26552221 Reviewed-on: https://go-review.googlesource.com/c/tools/+/214582 Run-TryBot: Heschi Kreinick TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/protocol/tsclient.go | 4 ++-- internal/lsp/protocol/tsprotocol.go | 2 +- internal/lsp/protocol/tsserver.go | 24 ++++++++++++++++--- internal/lsp/protocol/typescript/code.ts | 30 ++++++++++++++++++++++-- internal/lsp/server.go | 5 ++++ 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/internal/lsp/protocol/tsclient.go b/internal/lsp/protocol/tsclient.go index 692ae3ebf6..cfb32fe37f 100644 --- a/internal/lsp/protocol/tsclient.go +++ b/internal/lsp/protocol/tsclient.go @@ -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 Fri Jan 10 2020 17:17:33 GMT-0500 (Eastern Standard Time) // Code generated (see typescript/README.md) DO NOT EDIT. @@ -144,9 +144,9 @@ func (h clientHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, deliver log.Error(ctx, "", err) } return true - default: return false + } } diff --git a/internal/lsp/protocol/tsprotocol.go b/internal/lsp/protocol/tsprotocol.go index 3243a84cd6..d285206e0d 100644 --- a/internal/lsp/protocol/tsprotocol.go +++ b/internal/lsp/protocol/tsprotocol.go @@ -1,7 +1,7 @@ // Package protocol contains data types and code for LSP jsonrpcs // generated automatically from vscode-languageserver-node // commit: 635ab1fe6f8c57ce9402e573d007f24d6d290fd3 -// last fetched Mon Oct 14 2019 09:09:30 GMT-0400 (Eastern Daylight Time) +// last fetched Fri Jan 10 2020 17:17:33 GMT-0500 (Eastern Standard Time) package protocol // Code generated (see typescript/README.md) DO NOT EDIT. diff --git a/internal/lsp/protocol/tsserver.go b/internal/lsp/protocol/tsserver.go index 8850d99c60..dbbbd01a8e 100644 --- a/internal/lsp/protocol/tsserver.go +++ b/internal/lsp/protocol/tsserver.go @@ -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 Mon Oct 14 2019 09:09:30 GMT-0400 (Eastern Daylight Time) +// last fetched Fri Jan 10 2020 17:17:33 GMT-0500 (Eastern Standard Time) // Code generated (see typescript/README.md) DO NOT EDIT. @@ -60,6 +60,7 @@ type Server interface { Rename(context.Context, *RenameParams) (*WorkspaceEdit /*WorkspaceEdit | null*/, error) PrepareRename(context.Context, *PrepareRenameParams) (interface{} /* Range | struct{; Range Range`json:"range"`; Placeholder string`json:"placeholder"`; } | nil*/, error) ExecuteCommand(context.Context, *ExecuteCommandParams) (interface{} /*any | null*/, error) + NonstandardRequest(ctx context.Context, method string, params interface{}) (interface{}, error) } func (h serverHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, delivered bool) bool { @@ -526,9 +527,18 @@ func (h serverHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, deliver log.Error(ctx, "", err) } return true - default: - return false + var params interface{} + if err := json.Unmarshal(*r.Params, ¶ms); err != nil { + sendParseError(ctx, r, err) + return true + } + resp, err := h.server.NonstandardRequest(ctx, r.Method, params) + if err := r.Reply(ctx, resp, err); err != nil { + log.Error(ctx, "", err) + } + return true + } } @@ -822,3 +832,11 @@ func (s *serverDispatcher) ExecuteCommand(ctx context.Context, params *ExecuteCo } return result, nil } + +func (s *serverDispatcher) NonstandardRequest(ctx context.Context, method string, params interface{}) (interface{}, error) { + var result interface{} + if err := s.Conn.Call(ctx, method, params, &result); err != nil { + return nil, err + } + return result, nil +} diff --git a/internal/lsp/protocol/typescript/code.ts b/internal/lsp/protocol/typescript/code.ts index 111cc44355..511f3948a7 100644 --- a/internal/lsp/protocol/typescript/code.ts +++ b/internal/lsp/protocol/typescript/code.ts @@ -1043,8 +1043,6 @@ function output(side: side) { switch r.Method {`); side.cases.forEach((v) => {f(v)}); f(` - default: - return false } }`); f(` @@ -1055,6 +1053,33 @@ function output(side: side) { side.calls.forEach((v) => {f(v)}); } +// Handling of non-standard requests, so we can add gopls-specific calls. +function nonstandardRequests() { + server.methods.push('NonstandardRequest(ctx context.Context, method string, params interface{}) (interface{}, error)') + server.calls.push(`func (s *serverDispatcher) NonstandardRequest(ctx context.Context, method string, params interface{}) (interface{}, error) { + var result interface{} + if err := s.Conn.Call(ctx, method, params, &result); err != nil { + return nil, err + } + return result, nil + } + `) + client.cases.push(`default: + return false`) + server.cases.push(`default: + var params interface{} + if err := json.Unmarshal(*r.Params, ¶ms); err != nil { + sendParseError(ctx, r, err) + return true + } + resp, err := h.server.NonstandardRequest(ctx, r.Method, params) + if err := r.Reply(ctx, resp, err); err != nil { + log.Error(ctx, "", err) + } + return true +`) +} + // ----- remember it's a scripting language function main() { if (u.gitHash != u.git()) { @@ -1092,6 +1117,7 @@ function main() { req.forEach( // requests (v, k) => { receives.get(k) == 'client' ? goReq(client, k) : goReq(server, k)}); + nonstandardRequests(); // find all the types implied by seenTypes and rpcs to try to avoid // generating types that aren't used moreTypes(); diff --git a/internal/lsp/server.go b/internal/lsp/server.go index e500b48212..ab8f208123 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -286,6 +286,11 @@ func (s *Server) SelectionRange(context.Context, *protocol.SelectionRangeParams) return nil, notImplemented("SelectionRange") } +// Nonstandard requests +func (s *Server) NonstandardRequest(ctx context.Context, method string, params interface{}) (interface{}, error) { + return nil, notImplemented(method) +} + func notImplemented(method string) *jsonrpc2.Error { return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", method) }