1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:34:51 -07:00
go/internal/lsp/protocol/protocol.go
Ian Cottrell 5d16bdd7b5 internal/lsp: add the ability to log back to the client
Also use it for errors that were otherwise silently dropped
This makes it much easier to debug problems.

Also added command line control over whether the rpc trace messages are printed, which allows you to read the
log, otherwise the file edit messages swamp the log.

Change-Id: I7b70fd18034a87b2964e6d6d5f6f33dcaf7d8ea8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/170178
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-04-01 16:22:08 +00:00

49 lines
1.5 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
func canceller(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
conn.Notify(context.Background(), "$/cancelRequest", &CancelParams{ID: *req.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 = true
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 = true
conn.Handler = serverHandler(log, server)
conn.Canceler = jsonrpc2.Canceler(canceller)
return conn, client, log
}
func sendParseError(ctx context.Context, log xlog.Logger, conn *jsonrpc2.Conn, req *jsonrpc2.Request, err error) {
if _, ok := err.(*jsonrpc2.Error); !ok {
err = jsonrpc2.NewErrorf(jsonrpc2.CodeParseError, "%v", err)
}
if err := conn.Reply(ctx, req, nil, err); err != nil {
log.Errorf(ctx, "%v", err)
}
}