internal/jsonrpc2: dont add any handlers by default
This pushes the handler construction out to the user, allowing flexability of
use, and is the final stage of the switch to the new handler API.
Change-Id: Id2e61813a817df0d6e4d20dd47ce8c92b0ae87db
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227024
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/internal/jsonrpc2/jsonrpc2.go b/internal/jsonrpc2/jsonrpc2.go
index f93ab68..08234d2 100644
--- a/internal/jsonrpc2/jsonrpc2.go
+++ b/internal/jsonrpc2/jsonrpc2.go
@@ -276,8 +276,6 @@
// It must be called exactly once for each Conn.
// It returns only when the reader is closed or there is an error in the stream.
func (c *Conn) Run(runCtx context.Context, handler Handler) error {
- handler = MustReply(handler)
- handler = AsyncHandler(handler)
for {
// get the data for a message
data, n, err := c.stream.Read(runCtx)
diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go
index cf5342d..e05d2b5 100644
--- a/internal/lsp/cmd/cmd.go
+++ b/internal/lsp/cmd/cmd.go
@@ -234,9 +234,10 @@
cc := jsonrpc2.NewConn(stream)
connection.Server = protocol.ServerDispatcher(cc)
ctx = protocol.WithClient(ctx, connection.Client)
- go cc.Run(ctx, protocol.CancelHandler(
- protocol.ClientHandler(connection.Client,
- jsonrpc2.MethodNotFound)))
+ go cc.Run(ctx,
+ protocol.Handlers(
+ protocol.ClientHandler(connection.Client,
+ jsonrpc2.MethodNotFound)))
return connection, connection.initialize(ctx, app.options)
}
diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go
index d4fb182..1cd213a 100644
--- a/internal/lsp/fake/editor.go
+++ b/internal/lsp/fake/editor.go
@@ -57,7 +57,10 @@
e := NewEditor(ws)
e.server = protocol.ServerDispatcher(conn)
e.client = &Client{Editor: e}
- go conn.Run(ctx, protocol.ClientHandler(e.client, jsonrpc2.MethodNotFound))
+ go conn.Run(ctx,
+ protocol.Handlers(
+ protocol.ClientHandler(e.client,
+ jsonrpc2.MethodNotFound)))
if err := e.initialize(ctx); err != nil {
return nil, err
}
diff --git a/internal/lsp/lsprpc/lsprpc.go b/internal/lsp/lsprpc/lsprpc.go
index c516ab7..5c971f2 100644
--- a/internal/lsp/lsprpc/lsprpc.go
+++ b/internal/lsp/lsprpc/lsprpc.go
@@ -140,10 +140,11 @@
executable = ""
}
ctx = protocol.WithClient(ctx, client)
- return conn.Run(ctx, protocol.CancelHandler(
- handshaker(dc, executable,
- protocol.ServerHandler(server,
- jsonrpc2.MethodNotFound))))
+ return conn.Run(ctx,
+ protocol.Handlers(
+ handshaker(dc, executable,
+ protocol.ServerHandler(server,
+ jsonrpc2.MethodNotFound))))
}
// A Forwarder is a jsonrpc2.StreamServer that handles an LSP stream by
@@ -257,9 +258,10 @@
// Forward between connections.
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
- return serverConn.Run(ctx, protocol.CancelHandler(
- protocol.ClientHandler(client,
- jsonrpc2.MethodNotFound)))
+ return serverConn.Run(ctx,
+ protocol.Handlers(
+ protocol.ClientHandler(client,
+ jsonrpc2.MethodNotFound)))
})
// Don't run the clientConn yet, so that we can complete the handshake before
// processing any client messages.
@@ -298,7 +300,7 @@
}
g.Go(func() error {
return clientConn.Run(ctx,
- protocol.CancelHandler(
+ protocol.Handlers(
forwarderHandler(
protocol.ServerHandler(server,
jsonrpc2.MethodNotFound))))
diff --git a/internal/lsp/lsprpc/lsprpc_test.go b/internal/lsp/lsprpc/lsprpc_test.go
index 1de8b92..37a3e9a 100644
--- a/internal/lsp/lsprpc/lsprpc_test.go
+++ b/internal/lsp/lsprpc/lsprpc_test.go
@@ -129,7 +129,9 @@
t.Run(test.serverType, func(t *testing.T) {
cc := test.ts.Connect(baseCtx)
sd := protocol.ServerDispatcher(cc)
- go cc.Run(baseCtx, protocol.CancelHandler(jsonrpc2.MethodNotFound))
+ go cc.Run(baseCtx,
+ protocol.Handlers(
+ jsonrpc2.MethodNotFound))
ctx := context.Background()
ctx1, cancel1 := context.WithCancel(ctx)
diff --git a/internal/lsp/protocol/protocol.go b/internal/lsp/protocol/protocol.go
index 174cced..00610ad 100644
--- a/internal/lsp/protocol/protocol.go
+++ b/internal/lsp/protocol/protocol.go
@@ -33,6 +33,13 @@
return &serverDispatcher{Conn: conn}
}
+func Handlers(handler jsonrpc2.Handler) jsonrpc2.Handler {
+ return CancelHandler(
+ CancelHandler(
+ jsonrpc2.AsyncHandler(
+ jsonrpc2.MustReply(handler))))
+}
+
func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
handler, canceller := jsonrpc2.CancelHandler(handler)
return func(ctx context.Context, req *jsonrpc2.Request) error {