| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package protocol |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import ( |
| 14 |
"context" |
| 15 |
"encoding/json" |
| 16 |
"fmt" |
| 17 |
|
| 18 |
"golang.org/x/tools/internal/jsonrpc2" |
| 19 |
) |
| 20 |
|
| 21 |
type Client interface { |
| 22 |
|
| 23 |
LogTrace(context.Context, *LogTraceParams) error |
| 24 |
|
| 25 |
Progress(context.Context, *ProgressParams) error |
| 26 |
|
| 27 |
RegisterCapability(context.Context, *RegistrationParams) error |
| 28 |
|
| 29 |
UnregisterCapability(context.Context, *UnregistrationParams) error |
| 30 |
|
| 31 |
Event(context.Context, *any) error |
| 32 |
|
| 33 |
PublishDiagnostics(context.Context, *PublishDiagnosticsParams) error |
| 34 |
|
| 35 |
LogMessage(context.Context, *LogMessageParams) error |
| 36 |
|
| 37 |
ShowDocument(context.Context, *ShowDocumentParams) (*ShowDocumentResult, error) |
| 38 |
|
| 39 |
ShowMessage(context.Context, *ShowMessageParams) error |
| 40 |
|
| 41 |
ShowMessageRequest(context.Context, *ShowMessageRequestParams) (*MessageActionItem, error) |
| 42 |
|
| 43 |
WorkDoneProgressCreate(context.Context, *WorkDoneProgressCreateParams) error |
| 44 |
|
| 45 |
ApplyEdit(context.Context, *ApplyWorkspaceEditParams) (*ApplyWorkspaceEditResult, error) |
| 46 |
|
| 47 |
CodeLensRefresh(context.Context) error |
| 48 |
|
| 49 |
Configuration(context.Context, *ParamConfiguration) ([]LSPAny, error) |
| 50 |
|
| 51 |
DiagnosticRefresh(context.Context) error |
| 52 |
|
| 53 |
FoldingRangeRefresh(context.Context) error |
| 54 |
|
| 55 |
InlayHintRefresh(context.Context) error |
| 56 |
|
| 57 |
InlineValueRefresh(context.Context) error |
| 58 |
|
| 59 |
SemanticTokensRefresh(context.Context) error |
| 60 |
|
| 61 |
TextDocumentContentRefresh(context.Context, *TextDocumentContentRefreshParams) error |
| 62 |
|
| 63 |
WorkspaceFolders(context.Context) ([]WorkspaceFolder, error) |
| 64 |
} |
| 65 |
|
| 66 |
func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) { |
| 67 |
resp, valid, err := ClientDispatchCall(ctx, client, r.Method(), r.Params()) |
| 68 |
if !valid { |
| 69 |
return false, nil |
| 70 |
} |
| 71 |
|
| 72 |
if err != nil { |
| 73 |
return valid, reply(ctx, nil, err) |
| 74 |
} else { |
| 75 |
return valid, reply(ctx, resp, nil) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func ClientDispatchCall(ctx context.Context, client Client, method string, raw json.RawMessage) (resp any, _ bool, err error) { |
| 80 |
switch method { |
| 81 |
case "$/logTrace": |
| 82 |
var params LogTraceParams |
| 83 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 84 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 85 |
} |
| 86 |
err := client.LogTrace(ctx, ¶ms) |
| 87 |
return nil, true, err |
| 88 |
|
| 89 |
case "$/progress": |
| 90 |
var params ProgressParams |
| 91 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 92 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 93 |
} |
| 94 |
err := client.Progress(ctx, ¶ms) |
| 95 |
return nil, true, err |
| 96 |
|
| 97 |
case "client/registerCapability": |
| 98 |
var params RegistrationParams |
| 99 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 100 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 101 |
} |
| 102 |
err := client.RegisterCapability(ctx, ¶ms) |
| 103 |
return nil, true, err |
| 104 |
|
| 105 |
case "client/unregisterCapability": |
| 106 |
var params UnregistrationParams |
| 107 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 108 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 109 |
} |
| 110 |
err := client.UnregisterCapability(ctx, ¶ms) |
| 111 |
return nil, true, err |
| 112 |
|
| 113 |
case "telemetry/event": |
| 114 |
var params any |
| 115 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 116 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 117 |
} |
| 118 |
err := client.Event(ctx, ¶ms) |
| 119 |
return nil, true, err |
| 120 |
|
| 121 |
case "textDocument/publishDiagnostics": |
| 122 |
var params PublishDiagnosticsParams |
| 123 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 124 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 125 |
} |
| 126 |
err := client.PublishDiagnostics(ctx, ¶ms) |
| 127 |
return nil, true, err |
| 128 |
|
| 129 |
case "window/logMessage": |
| 130 |
var params LogMessageParams |
| 131 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 132 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 133 |
} |
| 134 |
err := client.LogMessage(ctx, ¶ms) |
| 135 |
return nil, true, err |
| 136 |
|
| 137 |
case "window/showDocument": |
| 138 |
var params ShowDocumentParams |
| 139 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 140 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 141 |
} |
| 142 |
resp, err := client.ShowDocument(ctx, ¶ms) |
| 143 |
if err != nil { |
| 144 |
return nil, true, err |
| 145 |
} |
| 146 |
return resp, true, nil |
| 147 |
|
| 148 |
case "window/showMessage": |
| 149 |
var params ShowMessageParams |
| 150 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 151 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 152 |
} |
| 153 |
err := client.ShowMessage(ctx, ¶ms) |
| 154 |
return nil, true, err |
| 155 |
|
| 156 |
case "window/showMessageRequest": |
| 157 |
var params ShowMessageRequestParams |
| 158 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 159 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 160 |
} |
| 161 |
resp, err := client.ShowMessageRequest(ctx, ¶ms) |
| 162 |
if err != nil { |
| 163 |
return nil, true, err |
| 164 |
} |
| 165 |
return resp, true, nil |
| 166 |
|
| 167 |
case "window/workDoneProgress/create": |
| 168 |
var params WorkDoneProgressCreateParams |
| 169 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 170 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 171 |
} |
| 172 |
err := client.WorkDoneProgressCreate(ctx, ¶ms) |
| 173 |
return nil, true, err |
| 174 |
|
| 175 |
case "workspace/applyEdit": |
| 176 |
var params ApplyWorkspaceEditParams |
| 177 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 178 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 179 |
} |
| 180 |
resp, err := client.ApplyEdit(ctx, ¶ms) |
| 181 |
if err != nil { |
| 182 |
return nil, true, err |
| 183 |
} |
| 184 |
return resp, true, nil |
| 185 |
|
| 186 |
case "workspace/codeLens/refresh": |
| 187 |
err := client.CodeLensRefresh(ctx) |
| 188 |
return nil, true, err |
| 189 |
|
| 190 |
case "workspace/configuration": |
| 191 |
var params ParamConfiguration |
| 192 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 193 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 194 |
} |
| 195 |
resp, err := client.Configuration(ctx, ¶ms) |
| 196 |
if err != nil { |
| 197 |
return nil, true, err |
| 198 |
} |
| 199 |
return resp, true, nil |
| 200 |
|
| 201 |
case "workspace/diagnostic/refresh": |
| 202 |
err := client.DiagnosticRefresh(ctx) |
| 203 |
return nil, true, err |
| 204 |
|
| 205 |
case "workspace/foldingRange/refresh": |
| 206 |
err := client.FoldingRangeRefresh(ctx) |
| 207 |
return nil, true, err |
| 208 |
|
| 209 |
case "workspace/inlayHint/refresh": |
| 210 |
err := client.InlayHintRefresh(ctx) |
| 211 |
return nil, true, err |
| 212 |
|
| 213 |
case "workspace/inlineValue/refresh": |
| 214 |
err := client.InlineValueRefresh(ctx) |
| 215 |
return nil, true, err |
| 216 |
|
| 217 |
case "workspace/semanticTokens/refresh": |
| 218 |
err := client.SemanticTokensRefresh(ctx) |
| 219 |
return nil, true, err |
| 220 |
|
| 221 |
case "workspace/textDocumentContent/refresh": |
| 222 |
var params TextDocumentContentRefreshParams |
| 223 |
if err := UnmarshalJSON(raw, ¶ms); err != nil { |
| 224 |
return nil, true, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 225 |
} |
| 226 |
err := client.TextDocumentContentRefresh(ctx, ¶ms) |
| 227 |
return nil, true, err |
| 228 |
|
| 229 |
case "workspace/workspaceFolders": |
| 230 |
resp, err := client.WorkspaceFolders(ctx) |
| 231 |
if err != nil { |
| 232 |
return nil, true, err |
| 233 |
} |
| 234 |
return resp, true, nil |
| 235 |
|
| 236 |
default: |
| 237 |
return nil, false, nil |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func (s *clientDispatcher) LogTrace(ctx context.Context, params *LogTraceParams) error { |
| 242 |
return s.sender.Notify(ctx, "$/logTrace", params) |
| 243 |
} |
| 244 |
func (s *clientDispatcher) Progress(ctx context.Context, params *ProgressParams) error { |
| 245 |
return s.sender.Notify(ctx, "$/progress", params) |
| 246 |
} |
| 247 |
func (s *clientDispatcher) RegisterCapability(ctx context.Context, params *RegistrationParams) error { |
| 248 |
return s.sender.Call(ctx, "client/registerCapability", params, nil) |
| 249 |
} |
| 250 |
func (s *clientDispatcher) UnregisterCapability(ctx context.Context, params *UnregistrationParams) error { |
| 251 |
return s.sender.Call(ctx, "client/unregisterCapability", params, nil) |
| 252 |
} |
| 253 |
func (s *clientDispatcher) Event(ctx context.Context, params *any) error { |
| 254 |
return s.sender.Notify(ctx, "telemetry/event", params) |
| 255 |
} |
| 256 |
func (s *clientDispatcher) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) error { |
| 257 |
return s.sender.Notify(ctx, "textDocument/publishDiagnostics", params) |
| 258 |
} |
| 259 |
func (s *clientDispatcher) LogMessage(ctx context.Context, params *LogMessageParams) error { |
| 260 |
return s.sender.Notify(ctx, "window/logMessage", params) |
| 261 |
} |
| 262 |
func (s *clientDispatcher) ShowDocument(ctx context.Context, params *ShowDocumentParams) (*ShowDocumentResult, error) { |
| 263 |
var result *ShowDocumentResult |
| 264 |
if err := s.sender.Call(ctx, "window/showDocument", params, &result); err != nil { |
| 265 |
return nil, err |
| 266 |
} |
| 267 |
return result, nil |
| 268 |
} |
| 269 |
func (s *clientDispatcher) ShowMessage(ctx context.Context, params *ShowMessageParams) error { |
| 270 |
return s.sender.Notify(ctx, "window/showMessage", params) |
| 271 |
} |
| 272 |
func (s *clientDispatcher) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (*MessageActionItem, error) { |
| 273 |
var result *MessageActionItem |
| 274 |
if err := s.sender.Call(ctx, "window/showMessageRequest", params, &result); err != nil { |
| 275 |
return nil, err |
| 276 |
} |
| 277 |
return result, nil |
| 278 |
} |
| 279 |
func (s *clientDispatcher) WorkDoneProgressCreate(ctx context.Context, params *WorkDoneProgressCreateParams) error { |
| 280 |
return s.sender.Call(ctx, "window/workDoneProgress/create", params, nil) |
| 281 |
} |
| 282 |
func (s *clientDispatcher) ApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (*ApplyWorkspaceEditResult, error) { |
| 283 |
var result *ApplyWorkspaceEditResult |
| 284 |
if err := s.sender.Call(ctx, "workspace/applyEdit", params, &result); err != nil { |
| 285 |
return nil, err |
| 286 |
} |
| 287 |
return result, nil |
| 288 |
} |
| 289 |
func (s *clientDispatcher) CodeLensRefresh(ctx context.Context) error { |
| 290 |
return s.sender.Call(ctx, "workspace/codeLens/refresh", nil, nil) |
| 291 |
} |
| 292 |
func (s *clientDispatcher) Configuration(ctx context.Context, params *ParamConfiguration) ([]LSPAny, error) { |
| 293 |
var result []LSPAny |
| 294 |
if err := s.sender.Call(ctx, "workspace/configuration", params, &result); err != nil { |
| 295 |
return nil, err |
| 296 |
} |
| 297 |
return result, nil |
| 298 |
} |
| 299 |
func (s *clientDispatcher) DiagnosticRefresh(ctx context.Context) error { |
| 300 |
return s.sender.Call(ctx, "workspace/diagnostic/refresh", nil, nil) |
| 301 |
} |
| 302 |
func (s *clientDispatcher) FoldingRangeRefresh(ctx context.Context) error { |
| 303 |
return s.sender.Call(ctx, "workspace/foldingRange/refresh", nil, nil) |
| 304 |
} |
| 305 |
func (s *clientDispatcher) InlayHintRefresh(ctx context.Context) error { |
| 306 |
return s.sender.Call(ctx, "workspace/inlayHint/refresh", nil, nil) |
| 307 |
} |
| 308 |
func (s *clientDispatcher) InlineValueRefresh(ctx context.Context) error { |
| 309 |
return s.sender.Call(ctx, "workspace/inlineValue/refresh", nil, nil) |
| 310 |
} |
| 311 |
func (s *clientDispatcher) SemanticTokensRefresh(ctx context.Context) error { |
| 312 |
return s.sender.Call(ctx, "workspace/semanticTokens/refresh", nil, nil) |
| 313 |
} |
| 314 |
func (s *clientDispatcher) TextDocumentContentRefresh(ctx context.Context, params *TextDocumentContentRefreshParams) error { |
| 315 |
return s.sender.Call(ctx, "workspace/textDocumentContent/refresh", params, nil) |
| 316 |
} |
| 317 |
func (s *clientDispatcher) WorkspaceFolders(ctx context.Context) ([]WorkspaceFolder, error) { |
| 318 |
var result []WorkspaceFolder |
| 319 |
if err := s.sender.Call(ctx, "workspace/workspaceFolders", nil, &result); err != nil { |
| 320 |
return nil, err |
| 321 |
} |
| 322 |
return result, nil |
| 323 |
} |