|
| 1 | +// Copyright 2019 The go-language-server Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package jsonrpc2 |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | +) |
| 10 | + |
| 11 | +// ID is a Request identifier. |
| 12 | +// Only one of either the Name or Number members will be set, using the |
| 13 | +// number form if the Name is the empty string. |
| 14 | +type ID struct { |
| 15 | + Name string |
| 16 | + Number int64 |
| 17 | +} |
| 18 | + |
| 19 | +// Message is a general message as defined by JSON-RPC. The language server protocol always uses "2.0" as the jsonrpc version. |
| 20 | +type Message struct { |
| 21 | + JSONRPC string `json:"jsonrpc"` |
| 22 | +} |
| 23 | + |
| 24 | +// Request is a request message to describe a request between the client and the server. Every processed request must send a response back to the sender of the request. |
| 25 | +type Request struct { |
| 26 | + Message |
| 27 | + |
| 28 | + // The request id. |
| 29 | + ID *ID `json:"id"` |
| 30 | + |
| 31 | + // The method to be invoked. |
| 32 | + Method string `json:"method"` |
| 33 | + |
| 34 | + // The method's params. |
| 35 | + Params *json.RawMessage `json:"params,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +// Response is a response ressage sent as a result of a request. If a request doesn't provide a result value the receiver of a request still needs to return a response message to conform to the JSON RPC specification. The result property of the ResponseMessage should be set to null in this case to signal a successful request. |
| 39 | +type Response struct { |
| 40 | + Message |
| 41 | + |
| 42 | + // The error object in case a request fails. |
| 43 | + Error *ResponseError `json:"error,omitempty"` |
| 44 | + |
| 45 | + // The request id. |
| 46 | + ID *ID `json:"id"` |
| 47 | + |
| 48 | + // The result of a request. This member is REQUIRED on success. |
| 49 | + // This member MUST NOT exist if there was an error invoking the method. |
| 50 | + Result *json.RawMessage `json:"result,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +// ResponseError ... |
| 54 | +type ResponseError struct { |
| 55 | + |
| 56 | + // Code a number indicating the error type that occurred. |
| 57 | + Code ErrorCode `json:"code"` |
| 58 | + |
| 59 | + // Data a Primitive or Structured value that contains additional |
| 60 | + // information about the error. Can be omitted. |
| 61 | + Data *json.RawMessage `json:"data"` |
| 62 | + |
| 63 | + // Message a string providing a short description of the error. |
| 64 | + Message string `json:"message"` |
| 65 | +} |
| 66 | + |
| 67 | +type NotificationMessage struct { |
| 68 | + Message |
| 69 | + |
| 70 | + // Method is the method to be invoked. |
| 71 | + Method string `json:"method"` |
| 72 | + |
| 73 | + // Params is the notification's params. |
| 74 | + Params interface{} `json:"params,omitempty"` |
| 75 | +} |
| 76 | + |
| 77 | +type ErrorCode int |
| 78 | + |
| 79 | +const ( |
| 80 | + // Defined by JSON RPC |
| 81 | + ParseError ErrorCode = -32700 |
| 82 | + InvalidRequest ErrorCode = -32600 |
| 83 | + MethodNotFound ErrorCode = -32601 |
| 84 | + InvalidParams ErrorCode = -32602 |
| 85 | + InternalError ErrorCode = -32603 |
| 86 | + ServerErrorStart ErrorCode = -32099 |
| 87 | + ServerErrorEnd ErrorCode = -32000 |
| 88 | + ServerNotInitialized ErrorCode = -32002 |
| 89 | + UnknownErrorCode ErrorCode = -32001 |
| 90 | + |
| 91 | + // Defined by the protocol. |
| 92 | + RequestCancelled ErrorCode = -32800 |
| 93 | + ContentModified ErrorCode = -32801 |
| 94 | +) |
0 commit comments