1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/tools/internal/event"
13 "golang.org/x/tools/internal/jsonrpc2"
14 "golang.org/x/tools/internal/xcontext"
15 errors "golang.org/x/xerrors"
19 // RequestCancelledError should be used when a request is cancelled early.
20 RequestCancelledError = jsonrpc2.NewError(-32800, "JSON RPC cancelled")
23 // ClientDispatcher returns a Client that dispatches LSP requests across the
24 // given jsonrpc2 connection.
25 func ClientDispatcher(conn jsonrpc2.Conn) Client {
26 return &clientDispatcher{Conn: conn}
29 type clientDispatcher struct {
33 // ServerDispatcher returns a Server that dispatches LSP requests across the
34 // given jsonrpc2 connection.
35 func ServerDispatcher(conn jsonrpc2.Conn) Server {
36 return &serverDispatcher{Conn: conn}
39 type serverDispatcher struct {
43 func ClientHandler(client Client, handler jsonrpc2.Handler) jsonrpc2.Handler {
44 return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
46 ctx := xcontext.Detach(ctx)
47 return reply(ctx, nil, RequestCancelledError)
49 handled, err := clientDispatch(ctx, client, reply, req)
50 if handled || err != nil {
53 return handler(ctx, reply, req)
57 func ServerHandler(server Server, handler jsonrpc2.Handler) jsonrpc2.Handler {
58 return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
60 ctx := xcontext.Detach(ctx)
61 return reply(ctx, nil, RequestCancelledError)
63 handled, err := serverDispatch(ctx, server, reply, req)
64 if handled || err != nil {
67 //TODO: This code is wrong, it ignores handler and assumes non standard
68 // request handles everything
69 // non standard request should just be a layered handler.
70 var params interface{}
71 if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
72 return sendParseError(ctx, reply, err)
74 resp, err := server.NonstandardRequest(ctx, req.Method(), params)
75 return reply(ctx, resp, err)
79 func Handlers(handler jsonrpc2.Handler) jsonrpc2.Handler {
81 jsonrpc2.AsyncHandler(
82 jsonrpc2.MustReplyHandler(handler)))
85 func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
86 handler, canceller := jsonrpc2.CancelHandler(handler)
87 return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
88 if req.Method() != "$/cancelRequest" {
89 // TODO(iancottrell): See if we can generate a reply for the request to be cancelled
90 // at the point of cancellation rather than waiting for gopls to naturally reply.
91 // To do that, we need to keep track of whether a reply has been sent already and
92 // be careful about racing between the two paths.
93 // TODO(iancottrell): Add a test that watches the stream and verifies the response
94 // for the cancelled request flows.
95 replyWithDetachedContext := func(ctx context.Context, resp interface{}, err error) error {
96 // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest
97 if ctx.Err() != nil && err == nil {
98 err = RequestCancelledError
100 ctx = xcontext.Detach(ctx)
101 return reply(ctx, resp, err)
103 return handler(ctx, replyWithDetachedContext, req)
105 var params CancelParams
106 if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
107 return sendParseError(ctx, reply, err)
109 if n, ok := params.ID.(float64); ok {
110 canceller(jsonrpc2.NewIntID(int64(n)))
111 } else if s, ok := params.ID.(string); ok {
112 canceller(jsonrpc2.NewStringID(s))
114 return sendParseError(ctx, reply, fmt.Errorf("request ID %v malformed", params.ID))
116 return reply(ctx, nil, nil)
120 func Call(ctx context.Context, conn jsonrpc2.Conn, method string, params interface{}, result interface{}) error {
121 id, err := conn.Call(ctx, method, params, result)
122 if ctx.Err() != nil {
123 cancelCall(ctx, conn, id)
128 func cancelCall(ctx context.Context, conn jsonrpc2.Conn, id jsonrpc2.ID) {
129 ctx = xcontext.Detach(ctx)
130 ctx, done := event.Start(ctx, "protocol.canceller")
132 // Note that only *jsonrpc2.ID implements json.Marshaler.
133 conn.Notify(ctx, "$/cancelRequest", &CancelParams{ID: &id})
136 func sendParseError(ctx context.Context, reply jsonrpc2.Replier, err error) error {
137 return reply(ctx, nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err))