1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:04:42 -07:00
go/internal/lsp/protocol/protocol.go
Ian Cottrell 619de4ed67 internal/jsonrpc2: refactor to enable a more advanced request
This separates hides the wire structures, and then exposes a new Request
type to allow for it to carry advanced features.
It also embeds the connection into the request and changes the signature of the
handler to no longer require a separate Conn argument.

Change-Id: I20b54f146285f7a9cb5f279c6ebdf0f286f4b829
Reviewed-on: https://go-review.googlesource.com/c/tools/+/183717
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-27 03:33:22 +00:00

50 lines
1.6 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protocol
import (
"context"
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/lsp/xlog"
)
const defaultMessageBufferSize = 20
const defaultRejectIfOverloaded = false
func canceller(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID) {
conn.Notify(context.Background(), "$/cancelRequest", &CancelParams{ID: id})
}
func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server, xlog.Logger) {
log := xlog.New(NewLogger(client))
conn := jsonrpc2.NewConn(stream)
conn.Capacity = defaultMessageBufferSize
conn.RejectIfOverloaded = defaultRejectIfOverloaded
conn.Handler = clientHandler(log, client)
conn.Canceler = jsonrpc2.Canceler(canceller)
return conn, &serverDispatcher{Conn: conn}, log
}
func NewServer(stream jsonrpc2.Stream, server Server) (*jsonrpc2.Conn, Client, xlog.Logger) {
conn := jsonrpc2.NewConn(stream)
client := &clientDispatcher{Conn: conn}
log := xlog.New(NewLogger(client))
conn.Capacity = defaultMessageBufferSize
conn.RejectIfOverloaded = defaultRejectIfOverloaded
conn.Handler = serverHandler(log, server)
conn.Canceler = jsonrpc2.Canceler(canceller)
return conn, client, log
}
func sendParseError(ctx context.Context, log xlog.Logger, req *jsonrpc2.Request, err error) {
if _, ok := err.(*jsonrpc2.Error); !ok {
err = jsonrpc2.NewErrorf(jsonrpc2.CodeParseError, "%v", err)
}
if err := req.Reply(ctx, nil, err); err != nil {
log.Errorf(ctx, "%v", err)
}
}