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

internal/lsp: new requests.ts to generate new versions of tsclient.go and tsserver.go

Adjust the output of requests.ts to use the new facilities of jsonrpc2.go.

Change-Id: I316f7846db9f683345b836915d992e751f126196
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184081
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
pjw 2019-06-27 16:49:48 -07:00 committed by Peter Weinberger
parent 212fb13d59
commit 6cdbf07be9

View File

@ -62,11 +62,15 @@ function generate(files: string[], options: ts.CompilerOptions): void {
// 2. serverHandler(...) { return func(...) { switch r.method}}
// 3. func (x *xDispatcher) Method(ctx, parm)
not.forEach(
(v, k) => {receives.get(k) == 'client' ? goNot(client, k) :
goNot(server, k)});
(v, k) => {
receives.get(k) == 'client' ? goNot(client, k) :
goNot(server, k)
});
req.forEach(
(v, k) => {receives.get(k) == 'client' ? goReq(client, k) :
goReq(server, k)});
(v, k) => {
receives.get(k) == 'client' ? goReq(client, k) :
goReq(server, k)
});
// and print the Go code
output(client);
output(server);
@ -94,7 +98,7 @@ function sig(nm: string, a: string, b: string, names?: boolean): string {
}
const notNil = `if r.Params != nil {
conn.Reply(ctx, r, nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidParams, "Expected no params"))
r.Reply(ctx, nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidParams, "Expected no params"))
return
}`;
// Go code for notifications. Side is client or server, m is the request method
@ -109,7 +113,7 @@ function goNot(side: side, m: string) {
if (a != '') {
case1 = `var params ${a}
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, log, conn, r, err)
sendParseError(ctx, log, r, err)
return
}
if err := ${side.name}.${nm}(ctx, &params); err != nil {
@ -148,7 +152,7 @@ function goReq(side: side, m: string) {
if (a != '') {
case1 = `var params ${a}
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, log, conn, r, err)
sendParseError(ctx, log, r, err)
return
}`;
}
@ -158,12 +162,12 @@ function goReq(side: side, m: string) {
}`;
if (b != '') {
case2 = `resp, err := ${side.name}.${nm}(ctx${arg2})
if err := conn.Reply(ctx, r, resp, err); err != nil {
if err := r.Reply(ctx, resp, err); err != nil {
log.Errorf(ctx, "%v", err)
}`;
} else { // response is nil
case2 = `err := ${side.name}.${nm}(ctx${arg2})
if err := conn.Reply(ctx, r, nil, err); err != nil {
if err := r.Reply(ctx, nil, err); err != nil {
log.Errorf(ctx, "%v", err)
}`
}
@ -227,20 +231,20 @@ function output(side: side) {
f('}\n');
f(`func ${side.name}Handler(log xlog.Logger, ${side.name} ${
side.goName}) jsonrpc2.Handler {
return func(ctx context.Context, conn *jsonrpc2.Conn, r *jsonrpc2.Request) {
return func(ctx context.Context, r *jsonrpc2.Request) {
switch r.Method {
case "$/cancelRequest":
var params CancelParams
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, log, conn, r, err)
sendParseError(ctx, log, r, err)
return
}
conn.Cancel(params.ID)`);
r.Conn().Cancel(params.ID)`);
side.cases.forEach((v) => { f(v) });
f(`
default:
if r.IsNotify() {
conn.Reply(ctx, r, nil, jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not found", r.Method))
r.Reply(ctx, nil, jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not found", r.Method))
}
}
}