@@ -10,46 +10,28 @@ import (
10
10
"go.uber.org/zap"
11
11
12
12
"go.lsp.dev/jsonrpc2"
13
+ "go.lsp.dev/pkg/xcontext"
13
14
)
14
15
15
- // clientHandler represents a client handler.
16
- type clientHandler struct {
17
- client ClientInterface
18
- }
19
-
20
- // compile time check whether the clientHandler implements jsonrpc2.Handler interface.
21
- var _ jsonrpc2.Handler = & clientHandler {}
22
-
23
- // Cancel implements Handler interface.
24
- func (clientHandler ) Cancel (ctx context.Context , conn * jsonrpc2.Conn , id jsonrpc2.ID , canceled bool ) bool {
25
- return false
26
- }
27
-
28
- // Request implements Handler interface.
29
- func (clientHandler ) Request (ctx context.Context , conn * jsonrpc2.Conn , direction jsonrpc2.Direction , r * jsonrpc2.WireRequest ) context.Context {
30
- return ctx
31
- }
16
+ func ClientHandler (client ClientInterface , handler jsonrpc2.Handler ) jsonrpc2.Handler {
17
+ h := func (ctx context.Context , reply jsonrpc2.Replier , req jsonrpc2.Requester ) error {
18
+ if ctx .Err () != nil {
19
+ ctx := xcontext .Detach (ctx )
20
+ return reply (ctx , nil , RequestCancelledError )
21
+ }
22
+ handled , err := clientDispatch (ctx , client , reply , req )
23
+ if handled || err != nil {
24
+ return err
25
+ }
26
+ return handler (ctx , reply , req )
27
+ }
32
28
33
- // Response implements Handler interface.
34
- func (clientHandler ) Response (ctx context.Context , conn * jsonrpc2.Conn , direction jsonrpc2.Direction , r * jsonrpc2.WireResponse ) context.Context {
35
- return ctx
29
+ return h
36
30
}
37
31
38
- // Done implements Handler interface.
39
- func (clientHandler ) Done (ctx context.Context , err error ) {}
40
-
41
- // Read implements Handler interface.
42
- func (clientHandler ) Read (ctx context.Context , bytes int64 ) context.Context { return ctx }
43
-
44
- // Write implements Handler interface.
45
- func (clientHandler ) Write (ctx context.Context , bytes int64 ) context.Context { return ctx }
46
-
47
- // Error implements Handler interface.
48
- func (clientHandler ) Error (ctx context.Context , err error ) {}
49
-
50
32
// ClientInterface represents a Language Server Protocol client.
51
33
type ClientInterface interface {
52
- Run (ctx context.Context ) (err error )
34
+ // Run(ctx context.Context) (err error)
53
35
LogMessage (ctx context.Context , params * LogMessageParams ) (err error )
54
36
PublishDiagnostics (ctx context.Context , params * PublishDiagnosticsParams ) (err error )
55
37
ShowMessage (ctx context.Context , params * ShowMessageParams ) (err error )
@@ -96,18 +78,18 @@ const (
96
78
97
79
// client implements a Language Server Protocol client.
98
80
type client struct {
99
- * jsonrpc2.Conn
81
+ jsonrpc2.Conn
100
82
logger * zap.Logger
101
83
}
102
84
103
85
// compiler time check whether the Client implements ClientInterface interface.
104
86
var _ ClientInterface = (* client )(nil )
105
87
106
88
// Run runs the Language Server Protocol client.
107
- func (c * client ) Run (ctx context.Context ) (err error ) {
108
- err = c .Conn .Run (ctx )
109
- return
110
- }
89
+ // func (c *client) Run(ctx context.Context) (err error) {
90
+ // _, err = c.Conn.Run(ctx)
91
+ // return
92
+ // }
111
93
112
94
// LogMessage sends the notification from the server to the client to ask the client to log a particular message.
113
95
func (c * client ) LogMessage (ctx context.Context , params * LogMessageParams ) (err error ) {
@@ -142,7 +124,7 @@ func (c *client) ShowMessage(ctx context.Context, params *ShowMessageParams) (er
142
124
// In addition to the show message notification the request allows to pass actions and to wait for an answer from the client.
143
125
func (c * client ) ShowMessageRequest (ctx context.Context , params * ShowMessageRequestParams ) (result * MessageActionItem , err error ) {
144
126
result = new (MessageActionItem )
145
- err = c .Conn .Call (ctx , MethodWindowShowMessageRequest , params , result )
127
+ _ , err = c .Conn .Call (ctx , MethodWindowShowMessageRequest , params , result )
146
128
147
129
return result , err
148
130
}
@@ -160,19 +142,19 @@ func (c *client) Telemetry(ctx context.Context, params interface{}) (err error)
160
142
// A client opts in via the dynamicRegistration property on the specific client capabilities.
161
143
// A client can even provide dynamic registration for capability A but not for capability B (see TextDocumentClientCapabilities as an example).
162
144
func (c * client ) RegisterCapability (ctx context.Context , params * RegistrationParams ) (err error ) {
163
- err = c .Conn .Call (ctx , MethodClientRegisterCapability , params , nil )
145
+ _ , err = c .Conn .Call (ctx , MethodClientRegisterCapability , params , nil )
164
146
return
165
147
}
166
148
167
149
// UnregisterCapability sends the request from the server to the client to unregister a previously registered capability.
168
150
func (c * client ) UnregisterCapability (ctx context.Context , params * UnregistrationParams ) (err error ) {
169
- err = c .Conn .Call (ctx , MethodClientUnregisterCapability , params , nil )
151
+ _ , err = c .Conn .Call (ctx , MethodClientUnregisterCapability , params , nil )
170
152
return
171
153
}
172
154
173
155
// WorkspaceApplyEdit sends the request from the server to the client to modify resource on the client side.
174
156
func (c * client ) WorkspaceApplyEdit (ctx context.Context , params * ApplyWorkspaceEditParams ) (result bool , err error ) {
175
- err = c .Conn .Call (ctx , MethodWorkspaceApplyEdit , params , & result )
157
+ _ , err = c .Conn .Call (ctx , MethodWorkspaceApplyEdit , params , & result )
176
158
177
159
return result , err
178
160
}
@@ -184,7 +166,7 @@ func (c *client) WorkspaceApplyEdit(ctx context.Context, params *ApplyWorkspaceE
184
166
// passed ConfigurationItems (e.g. the first item in the response is the result for the first configuration item in the params).
185
167
func (c * client ) WorkspaceConfiguration (ctx context.Context , params * ConfigurationParams ) ([]interface {}, error ) {
186
168
var result []interface {}
187
- err := c .Conn .Call (ctx , MethodWorkspaceConfiguration , params , & result )
169
+ _ , err := c .Conn .Call (ctx , MethodWorkspaceConfiguration , params , & result )
188
170
189
171
return result , err
190
172
}
@@ -195,7 +177,7 @@ func (c *client) WorkspaceConfiguration(ctx context.Context, params *Configurati
195
177
//
196
178
// Since version 3.6.0.
197
179
func (c * client ) WorkspaceFolders (ctx context.Context ) (result []WorkspaceFolder , err error ) {
198
- err = c .Conn .Call (ctx , MethodWorkspaceWorkspaceFolders , nil , & result )
180
+ _ , err = c .Conn .Call (ctx , MethodWorkspaceWorkspaceFolders , nil , & result )
199
181
200
182
return result , err
201
183
}
0 commit comments