1package lsp
2
3import (
4 "encoding/json"
5)
6
7// Message represents a JSON-RPC 2.0 message
8type Message struct {
9 JSONRPC string `json:"jsonrpc"`
10 ID int32 `json:"id,omitempty"`
11 Method string `json:"method,omitempty"`
12 Params json.RawMessage `json:"params,omitempty"`
13 Result json.RawMessage `json:"result,omitempty"`
14 Error *ResponseError `json:"error,omitempty"`
15}
16
17// ResponseError represents a JSON-RPC 2.0 error
18type ResponseError struct {
19 Code int `json:"code"`
20 Message string `json:"message"`
21}
22
23func NewRequest(id int32, method string, params any) (*Message, error) {
24 paramsJSON, err := json.Marshal(params)
25 if err != nil {
26 return nil, err
27 }
28
29 return &Message{
30 JSONRPC: "2.0",
31 ID: id,
32 Method: method,
33 Params: paramsJSON,
34 }, nil
35}
36
37func NewNotification(method string, params any) (*Message, error) {
38 paramsJSON, err := json.Marshal(params)
39 if err != nil {
40 return nil, err
41 }
42
43 return &Message{
44 JSONRPC: "2.0",
45 Method: method,
46 Params: paramsJSON,
47 }, nil
48}