Skip to content

Commit 3995c3c

Browse files
committed
client,server: add debug log
1 parent cdfd535 commit 3995c3c

File tree

2 files changed

+279
-99
lines changed

2 files changed

+279
-99
lines changed

client.go

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

16+
// ClientDispatcher returns a Client that dispatches LSP requests across the
17+
// given jsonrpc2 connection.
18+
func ClientDispatcher(conn jsonrpc2.Conn, logger *zap.Logger) Client {
19+
return &client{
20+
Conn: conn,
21+
logger: logger,
22+
}
23+
}
24+
1625
// ClientHandler handler of LSP client.
1726
func ClientHandler(client Client, handler jsonrpc2.Handler) jsonrpc2.Handler {
1827
h := func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
1928
if ctx.Err() != nil {
2029
xctx := xcontext.Detach(ctx)
2130
return reply(xctx, nil, ErrRequestCancelled)
2231
}
32+
2333
handled, err := clientDispatch(ctx, client, reply, req)
2434
if handled || err != nil {
2535
return err
@@ -88,6 +98,9 @@ var _ Client = (*client)(nil)
8898

8999
// LogMessage sends the notification from the server to the client to ask the client to log a particular message.
90100
func (c *client) LogMessage(ctx context.Context, params *LogMessageParams) (err error) {
101+
c.logger.Debug("call " + MethodWindowLogMessage)
102+
defer c.logger.Debug("end "+MethodWindowLogMessage, zap.Error(err))
103+
91104
return c.Conn.Notify(ctx, MethodWindowLogMessage, params)
92105
}
93106

@@ -102,6 +115,9 @@ func (c *client) LogMessage(ctx context.Context, params *LogMessageParams) (err
102115
// If the computed set is empty it has to push the empty array to clear former diagnostics.
103116
// Newly pushed diagnostics always replace previously pushed diagnostics. There is no merging that happens on the client side.
104117
func (c *client) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error) {
118+
c.logger.Debug("call " + MethodTextDocumentPublishDiagnostics)
119+
defer c.logger.Debug("end "+MethodTextDocumentPublishDiagnostics, zap.Error(err))
120+
105121
return c.Conn.Notify(ctx, MethodTextDocumentPublishDiagnostics, params)
106122
}
107123

@@ -114,15 +130,22 @@ func (c *client) ShowMessage(ctx context.Context, params *ShowMessageParams) (er
114130
// ShowMessageRequest sends the request from a server to a client to ask the client to display a particular message in the user interface.
115131
//
116132
// In addition to the show message notification the request allows to pass actions and to wait for an answer from the client.
117-
func (c *client) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (result *MessageActionItem, err error) {
118-
result = new(MessageActionItem)
119-
_, err = c.Conn.Call(ctx, MethodWindowShowMessageRequest, params, result)
133+
func (c *client) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (_ *MessageActionItem, err error) {
134+
c.logger.Debug("call " + MethodWindowShowMessageRequest)
135+
defer c.logger.Debug("end "+MethodWindowShowMessageRequest, zap.Error(err))
120136

121-
return result, err
137+
var result *MessageActionItem
138+
if err := Call(ctx, c.Conn, MethodWindowShowMessageRequest, params, &result); err != nil {
139+
return nil, err
140+
}
141+
return result, nil
122142
}
123143

124144
// Telemetry sends the notification from the server to the client to ask the client to log a telemetry event.
125145
func (c *client) Telemetry(ctx context.Context, params interface{}) (err error) {
146+
c.logger.Debug("call " + MethodTelemetryEvent)
147+
defer c.logger.Debug("end "+MethodTelemetryEvent, zap.Error(err))
148+
126149
return c.Conn.Notify(ctx, MethodTelemetryEvent, params)
127150
}
128151

@@ -133,33 +156,45 @@ func (c *client) Telemetry(ctx context.Context, params interface{}) (err error)
133156
// A client opts in via the dynamicRegistration property on the specific client capabilities.
134157
// A client can even provide dynamic registration for capability A but not for capability B (see TextDocumentClientCapabilities as an example).
135158
func (c *client) RegisterCapability(ctx context.Context, params *RegistrationParams) (err error) {
136-
_, err = c.Conn.Call(ctx, MethodClientRegisterCapability, params, nil)
137-
return
159+
c.logger.Debug("call " + MethodClientRegisterCapability)
160+
defer c.logger.Debug("end "+MethodClientRegisterCapability, zap.Error(err))
161+
162+
return Call(ctx, c.Conn, MethodClientRegisterCapability, params, nil)
138163
}
139164

140165
// UnregisterCapability sends the request from the server to the client to unregister a previously registered capability.
141166
func (c *client) UnregisterCapability(ctx context.Context, params *UnregistrationParams) (err error) {
142-
_, err = c.Conn.Call(ctx, MethodClientUnregisterCapability, params, nil)
143-
return
167+
c.logger.Debug("call " + MethodClientUnregisterCapability)
168+
defer c.logger.Debug("end "+MethodClientUnregisterCapability, zap.Error(err))
169+
170+
return Call(ctx, c.Conn, MethodClientUnregisterCapability, params, nil)
144171
}
145172

146173
// WorkspaceApplyEdit sends the request from the server to the client to modify resource on the client side.
147174
func (c *client) WorkspaceApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (result bool, err error) {
148-
_, err = c.Conn.Call(ctx, MethodWorkspaceApplyEdit, params, &result)
175+
c.logger.Debug("call " + MethodWorkspaceApplyEdit)
176+
defer c.logger.Debug("end "+MethodWorkspaceApplyEdit, zap.Error(err))
149177

150-
return result, err
178+
if err := Call(ctx, c.Conn, MethodWorkspaceApplyEdit, params, &result); err != nil {
179+
return false, err
180+
}
181+
return result, nil
151182
}
152183

153184
// WorkspaceConfiguration sends the request from the server to the client to fetch configuration settings from the client.
154185
//
155186
// The request can fetch several configuration settings in one roundtrip.
156187
// The order of the returned configuration settings correspond to the order of the
157188
// passed ConfigurationItems (e.g. the first item in the response is the result for the first configuration item in the params).
158-
func (c *client) WorkspaceConfiguration(ctx context.Context, params *ConfigurationParams) ([]interface{}, error) {
159-
var result []interface{}
160-
_, err := c.Conn.Call(ctx, MethodWorkspaceConfiguration, params, &result)
189+
func (c *client) WorkspaceConfiguration(ctx context.Context, params *ConfigurationParams) (_ []interface{}, err error) {
190+
c.logger.Debug("call " + MethodWorkspaceConfiguration)
191+
defer c.logger.Debug("end "+MethodWorkspaceConfiguration, zap.Error(err))
161192

162-
return result, err
193+
var result []interface{}
194+
if err := Call(ctx, c.Conn, MethodWorkspaceConfiguration, params, &result); err != nil {
195+
return nil, err
196+
}
197+
return result, nil
163198
}
164199

165200
// WorkspaceFolders sends the request from the server to the client to fetch the current open list of workspace folders.
@@ -168,7 +203,11 @@ func (c *client) WorkspaceConfiguration(ctx context.Context, params *Configurati
168203
//
169204
// Since version 3.6.0.
170205
func (c *client) WorkspaceFolders(ctx context.Context) (result []WorkspaceFolder, err error) {
171-
_, err = c.Conn.Call(ctx, MethodWorkspaceWorkspaceFolders, nil, &result)
206+
c.logger.Debug("call " + MethodWorkspaceWorkspaceFolders)
207+
defer c.logger.Debug("end "+MethodWorkspaceWorkspaceFolders, zap.Error(err))
172208

173-
return result, err
209+
if err := Call(ctx, c.Conn, MethodWorkspaceWorkspaceFolders, nil, &result); err != nil {
210+
return nil, err
211+
}
212+
return result, nil
174213
}

0 commit comments

Comments
 (0)