Skip to content

Commit c22c65c

Browse files
committed
all: support new jsonrpc2 Handler logic
1 parent 8ff7402 commit c22c65c

File tree

9 files changed

+324
-188
lines changed

9 files changed

+324
-188
lines changed

client.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
"go.lsp.dev/pkg/xcontext"
1414
)
1515

16-
func ClientHandler(client ClientInterface, handler jsonrpc2.Handler) jsonrpc2.Handler {
17-
h := func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Requester) error {
16+
// ClientHandler handler of LSP client.
17+
func ClientHandler(client Client, handler jsonrpc2.Handler) jsonrpc2.Handler {
18+
h := func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
1819
if ctx.Err() != nil {
19-
ctx := xcontext.Detach(ctx)
20-
return reply(ctx, nil, RequestCancelledError)
20+
xctx := xcontext.Detach(ctx)
21+
return reply(xctx, nil, ErrRequestCancelled)
2122
}
2223
handled, err := clientDispatch(ctx, client, reply, req)
2324
if handled || err != nil {
@@ -29,9 +30,8 @@ func ClientHandler(client ClientInterface, handler jsonrpc2.Handler) jsonrpc2.Ha
2930
return h
3031
}
3132

32-
// ClientInterface represents a Language Server Protocol client.
33-
type ClientInterface interface {
34-
// Run(ctx context.Context) (err error)
33+
// Client represents a Language Server Protocol client.
34+
type Client interface {
3535
LogMessage(ctx context.Context, params *LogMessageParams) (err error)
3636
PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error)
3737
ShowMessage(ctx context.Context, params *ShowMessageParams) (err error)
@@ -79,22 +79,16 @@ const (
7979
// client implements a Language Server Protocol client.
8080
type client struct {
8181
jsonrpc2.Conn
82+
8283
logger *zap.Logger
8384
}
8485

8586
// compiler time check whether the Client implements ClientInterface interface.
86-
var _ ClientInterface = (*client)(nil)
87-
88-
// Run runs the Language Server Protocol client.
89-
// func (c *client) Run(ctx context.Context) (err error) {
90-
// _, err = c.Conn.Run(ctx)
91-
// return
92-
// }
87+
var _ Client = (*client)(nil)
9388

9489
// LogMessage sends the notification from the server to the client to ask the client to log a particular message.
9590
func (c *client) LogMessage(ctx context.Context, params *LogMessageParams) (err error) {
96-
err = c.Conn.Notify(ctx, MethodWindowLogMessage, params)
97-
return
91+
return c.Conn.Notify(ctx, MethodWindowLogMessage, params)
9892
}
9993

10094
// PublishDiagnostics sends the notification from the server to the client to signal results of validation runs.
@@ -108,15 +102,13 @@ func (c *client) LogMessage(ctx context.Context, params *LogMessageParams) (err
108102
// If the computed set is empty it has to push the empty array to clear former diagnostics.
109103
// Newly pushed diagnostics always replace previously pushed diagnostics. There is no merging that happens on the client side.
110104
func (c *client) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error) {
111-
err = c.Conn.Notify(ctx, MethodTextDocumentPublishDiagnostics, params)
112-
return
105+
return c.Conn.Notify(ctx, MethodTextDocumentPublishDiagnostics, params)
113106
}
114107

115108
// ShowMessage sends the notification from a server to a client to ask the
116109
// client to display a particular message in the user interface.
117110
func (c *client) ShowMessage(ctx context.Context, params *ShowMessageParams) (err error) {
118-
err = c.Conn.Notify(ctx, MethodWindowShowMessage, params)
119-
return
111+
return c.Conn.Notify(ctx, MethodWindowShowMessage, params)
120112
}
121113

122114
// ShowMessageRequest sends the request from a server to a client to ask the client to display a particular message in the user interface.
@@ -131,8 +123,7 @@ func (c *client) ShowMessageRequest(ctx context.Context, params *ShowMessageRequ
131123

132124
// Telemetry sends the notification from the server to the client to ask the client to log a telemetry event.
133125
func (c *client) Telemetry(ctx context.Context, params interface{}) (err error) {
134-
err = c.Conn.Notify(ctx, MethodTelemetryEvent, params)
135-
return
126+
return c.Conn.Notify(ctx, MethodTelemetryEvent, params)
136127
}
137128

138129
// RegisterCapability sends the request from the server to the client to register for a new capability on the client side.

client_gojay.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ import (
2020

2121
// clientDispatch implements jsonrpc2.Conn.
2222
//nolint:funlen,gocognit
23-
func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.Replier, r jsonrpc2.Requester) (bool, error) {
23+
func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, req jsonrpc2.Request) (bool, error) {
2424
if ctx.Err() != nil {
25-
return true, reply(ctx, nil, RequestCancelledError)
25+
return true, reply(ctx, nil, ErrRequestCancelled)
2626
}
2727

28-
dec := gojaypool.BorrowSizedDecoder(bytes.NewReader(r.Params()), len(r.Params()))
28+
dec := gojaypool.BorrowSizedDecoder(bytes.NewReader(req.Params()), len(req.Params()))
2929
defer dec.Release()
3030
logger := LoggerFromContext(ctx)
3131

32-
switch r.Method() {
32+
switch req.Method() {
3333
case MethodClientRegisterCapability: // request
3434
var params RegistrationParams
3535
if err := dec.DecodeObject(&params); err != nil {
36-
return true, sendParseError(ctx, reply, err)
36+
return true, replyParseError(ctx, reply, err)
3737
}
3838

3939
err := client.RegisterCapability(ctx, &params)
@@ -49,7 +49,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
4949
case MethodClientUnregisterCapability: // request
5050
var params UnregistrationParams
5151
if err := dec.DecodeObject(&params); err != nil {
52-
return true, sendParseError(ctx, reply, err)
52+
return true, replyParseError(ctx, reply, err)
5353
}
5454

5555
err := client.UnregisterCapability(ctx, &params)
@@ -65,7 +65,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
6565
case MethodTelemetryEvent: // notification
6666
var params interface{}
6767
if err := dec.Decode(&params); err != nil {
68-
return true, sendParseError(ctx, reply, err)
68+
return true, replyParseError(ctx, reply, err)
6969
}
7070

7171
err := client.Telemetry(ctx, &params)
@@ -81,7 +81,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
8181
case MethodTextDocumentPublishDiagnostics: // notification
8282
var params PublishDiagnosticsParams
8383
if err := dec.DecodeObject(&params); err != nil {
84-
return true, sendParseError(ctx, reply, err)
84+
return true, replyParseError(ctx, reply, err)
8585
}
8686

8787
err := client.PublishDiagnostics(ctx, &params)
@@ -97,7 +97,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
9797
case MethodWindowLogMessage: // notification
9898
var params LogMessageParams
9999
if err := dec.DecodeObject(&params); err != nil {
100-
return true, sendParseError(ctx, reply, err)
100+
return true, replyParseError(ctx, reply, err)
101101
}
102102

103103
err := client.LogMessage(ctx, &params)
@@ -113,7 +113,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
113113
case MethodWindowShowMessage: // notification
114114
var params ShowMessageParams
115115
if err := dec.DecodeObject(&params); err != nil {
116-
return true, sendParseError(ctx, reply, err)
116+
return true, replyParseError(ctx, reply, err)
117117
}
118118

119119
err := client.ShowMessage(ctx, &params)
@@ -129,7 +129,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
129129
case MethodWindowShowMessageRequest: // request
130130
var params ShowMessageRequestParams
131131
if err := dec.DecodeObject(&params); err != nil {
132-
return true, sendParseError(ctx, reply, err)
132+
return true, replyParseError(ctx, reply, err)
133133
}
134134

135135
resp, err := client.ShowMessageRequest(ctx, &params)
@@ -145,7 +145,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
145145
case MethodWorkspaceApplyEdit: // request
146146
var params ApplyWorkspaceEditParams
147147
if err := dec.DecodeObject(&params); err != nil {
148-
return true, sendParseError(ctx, reply, err)
148+
return true, replyParseError(ctx, reply, err)
149149
}
150150

151151
resp, err := client.WorkspaceApplyEdit(ctx, &params)
@@ -161,7 +161,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
161161
case MethodWorkspaceConfiguration: // request
162162
var params ConfigurationParams
163163
if err := dec.DecodeObject(&params); err != nil {
164-
return true, sendParseError(ctx, reply, err)
164+
return true, replyParseError(ctx, reply, err)
165165
}
166166

167167
resp, err := client.WorkspaceConfiguration(ctx, &params)
@@ -175,7 +175,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
175175
return true, reply(ctx, resp, err)
176176

177177
case MethodWorkspaceWorkspaceFolders: // request
178-
if len(r.Params()) > 0 {
178+
if len(req.Params()) > 0 {
179179
return true, reply(ctx, nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams))
180180
}
181181

client_json.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ import (
1818
)
1919

2020
// clientDispatch implements jsonrpc2.Handler.
21-
func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.Replier, r jsonrpc2.Requester) (bool, error) {
21+
//nolint:gocognit,funlen
22+
func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, req jsonrpc2.Request) (bool, error) {
2223
if ctx.Err() != nil {
23-
return true, reply(ctx, nil, RequestCancelledError)
24+
return true, reply(ctx, nil, ErrRequestCancelled)
2425
}
2526

26-
dec := json.NewDecoder(bytes.NewReader(r.Params()))
27+
dec := json.NewDecoder(bytes.NewReader(req.Params()))
2728
logger := LoggerFromContext(ctx)
2829

29-
switch r.Method() {
30+
switch req.Method() {
3031
case MethodClientRegisterCapability: // request
3132
var params RegistrationParams
3233
if err := dec.Decode(&params); err != nil {
33-
return true, sendParseError(ctx, reply, err)
34+
return true, replyParseError(ctx, reply, err)
3435
}
3536

3637
err := client.RegisterCapability(ctx, &params)
@@ -46,7 +47,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
4647
case MethodClientUnregisterCapability: // request
4748
var params UnregistrationParams
4849
if err := dec.Decode(&params); err != nil {
49-
return true, sendParseError(ctx, reply, err)
50+
return true, replyParseError(ctx, reply, err)
5051
}
5152

5253
err := client.UnregisterCapability(ctx, &params)
@@ -62,7 +63,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
6263
case MethodTelemetryEvent: // notification
6364
var params interface{}
6465
if err := dec.Decode(&params); err != nil {
65-
return true, sendParseError(ctx, reply, err)
66+
return true, replyParseError(ctx, reply, err)
6667
}
6768

6869
err := client.Telemetry(ctx, &params)
@@ -78,7 +79,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
7879
case MethodTextDocumentPublishDiagnostics: // notification
7980
var params PublishDiagnosticsParams
8081
if err := dec.Decode(&params); err != nil {
81-
return true, sendParseError(ctx, reply, err)
82+
return true, replyParseError(ctx, reply, err)
8283
}
8384

8485
err := client.PublishDiagnostics(ctx, &params)
@@ -94,7 +95,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
9495
case MethodWindowLogMessage: // notification
9596
var params LogMessageParams
9697
if err := dec.Decode(&params); err != nil {
97-
return true, sendParseError(ctx, reply, err)
98+
return true, replyParseError(ctx, reply, err)
9899
}
99100

100101
err := client.LogMessage(ctx, &params)
@@ -110,7 +111,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
110111
case MethodWindowShowMessage: // notification
111112
var params ShowMessageParams
112113
if err := dec.Decode(&params); err != nil {
113-
return true, sendParseError(ctx, reply, err)
114+
return true, replyParseError(ctx, reply, err)
114115
}
115116

116117
err := client.ShowMessage(ctx, &params)
@@ -126,7 +127,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
126127
case MethodWindowShowMessageRequest: // request
127128
var params ShowMessageRequestParams
128129
if err := dec.Decode(&params); err != nil {
129-
return true, sendParseError(ctx, reply, err)
130+
return true, replyParseError(ctx, reply, err)
130131
}
131132

132133
resp, err := client.ShowMessageRequest(ctx, &params)
@@ -142,7 +143,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
142143
case MethodWorkspaceApplyEdit: // request
143144
var params ApplyWorkspaceEditParams
144145
if err := dec.Decode(&params); err != nil {
145-
return true, sendParseError(ctx, reply, err)
146+
return true, replyParseError(ctx, reply, err)
146147
}
147148

148149
resp, err := client.WorkspaceApplyEdit(ctx, &params)
@@ -158,7 +159,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
158159
case MethodWorkspaceConfiguration: // request
159160
var params ConfigurationParams
160161
if err := dec.Decode(&params); err != nil {
161-
return true, sendParseError(ctx, reply, err)
162+
return true, replyParseError(ctx, reply, err)
162163
}
163164

164165
resp, err := client.WorkspaceConfiguration(ctx, &params)
@@ -172,7 +173,7 @@ func clientDispatch(ctx context.Context, client ClientInterface, reply jsonrpc2.
172173
return true, reply(ctx, resp, err)
173174

174175
case MethodWorkspaceWorkspaceFolders: // request
175-
if len(r.Params()) > 0 {
176+
if len(req.Params()) > 0 {
176177
return true, reply(ctx, nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams))
177178
}
178179

context.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import (
1010
"go.uber.org/zap"
1111
)
1212

13-
type contextKey int
14-
15-
const (
16-
ctxLogger contextKey = 1 + iota
17-
ctxClient
13+
var (
14+
ctxLogger struct{}
15+
ctxClient struct{}
1816
)
1917

2018
// WithLogger returns the context with zap.Logger value.
@@ -33,6 +31,6 @@ func LoggerFromContext(ctx context.Context) *zap.Logger {
3331
}
3432

3533
// WithClient returns the context with Client value.
36-
func WithClient(ctx context.Context, client ClientInterface) context.Context {
34+
func WithClient(ctx context.Context, client Client) context.Context {
3735
return context.WithValue(ctx, ctxClient, client)
3836
}

0 commit comments

Comments
 (0)