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

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 <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-01-10 17:35:46 -05:00
parent 9c46c2c3da
commit 3be912efc3
5 changed files with 57 additions and 8 deletions

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

View File

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

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 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, &params); 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
}

View File

@ -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, &params); 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();

View File

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