3131# In case of an error, and error objet should bre returned.
3232#
3333
34- package ulsp
35- import json
34+ package json
3635link ximage
3736
3837$define CONTENT_LENGTH "Content-Length: "
3938
39+ # JRPC_Message is a class that represents a JSON-RPC message
40+ # It is used to parse the JSON-RPC message and to create the JSON-RPC message
4041class JRPC_Message(msg, kind, json_table)
4142
43+ # Parse the JSON-RPC message and set the kind of the message
4244 method parse_json(s)
4345 msg := s
4446 if json_table := jtou(s) then {
@@ -48,6 +50,7 @@ class JRPC_Message(msg, kind, json_table)
4850 kind := "unknown"
4951 end
5052
53+ # Guess the kind of the message based on the JSON-RPC message
5154 method guess_kind()
5255 #if we have a method, the msg is a request
5356 if \json_table["method"] then
@@ -58,59 +61,76 @@ class JRPC_Message(msg, kind, json_table)
5861 kind := "response"
5962 else
6063 kind := "unknown"
64+
65+ return kind
6166 end
6267
68+ # Get the JSON RPC kind of the message
6369 method get_kind()
6470 return kind
6571 end
72+ # Get the JSON RPC id of the message
6673 method get_id()
6774 return json_table["id"]
6875 end
76+ # Get the JSON RPC method of the message
6977 method get_method()
7078 return json_table["method"]
7179 end
80+ # Get the JSON RPC params of the message
7281 method get_params()
7382 return json_table["params"]
7483 end
7584 # many requests operate on a file, stored in uri param
85+ # used by ulsp server to get the file path
7686 method get_param_uri()
7787 return json_table["params"]["textDocument"]["uri"]
7888 end
7989
90+ # Get the result of returned by a json rpc request
8091 method get_result()
8192 return json_table["result"]
8293 end
94+ # Get the error of returned by a json rpc request
8395 method get_error()
8496 return json_table["error"]
8597 end
98+ # Get the raw message content
8699 method get_content()
87100 return msg
88101 end
89102
103+ # Initialize the JSON RPC message
90104 method init_msg()
91105 /json_table := ["jsonrpc" : "2.0"]
92106 end
93107
108+ # Set the result of the JSON RPC message
94109 method set_result(result)
95110 json_table["result"] := result
96111 end
112+ # Set the id of the JSON RPC message
97113 method set_id(id)
98114 return json_table["id"] := id
99115 end
116+ # Set the method of the JSON RPC message
100117 method set_method(meth)
101118 return json_table["method"] := meth
102119 end
120+ # Set the params of the JSON RPC message
103121 method set_params(params)
104122 return json_table["params"] := params
105123 end
106124
125+ # Make a result response JSON RPC message
107126 method make_result_response(result)
108127 delete(json_table, "method", "params", "error")
109128 json_table["result"] := \result | ""
110129 if msg := tojson(json_table) then
111130 return self
112131 end
113132
133+ # Make a request JSON RPC message
114134 method make_request(id, meth, params)
115135 if /json_table then
116136 json_table := [
@@ -176,6 +196,7 @@ class JRPC_Message(msg, kind, json_table)
176196 #
177197 # RequestCancelled: -32800;
178198
199+ # Make an error response JSON RPC message
179200 method make_error_response(err_code, err_msg, err_data)
180201 delete(json_table, "method", "params", "result")
181202 json_table["error"] := [
@@ -187,6 +208,7 @@ class JRPC_Message(msg, kind, json_table)
187208 return self
188209 end
189210
211+ # Initialize the JSON RPC message
190212 initially
191213 kind := "unknown"
192214end
@@ -227,6 +249,7 @@ class JRPC_HTTPSocket(sock)
227249 }
228250 end
229251
252+ # Get the data from the socket
230253 method get_data(size, timeout)
231254 local data := ""
232255 /timeout := -1
@@ -238,6 +261,7 @@ class JRPC_HTTPSocket(sock)
238261 return data
239262 end
240263
264+ # Get the JSON RPC message from the socket
241265 method get_msg(timeout)
242266 local msg_body, len
243267 len := get_http_header(timeout) | fail
@@ -247,12 +271,14 @@ class JRPC_HTTPSocket(sock)
247271 }
248272 end
249273
274+ # Send the JSON RPC message to the socket
250275 method send_msg(msg)
251276 local data
252277 data := msg.get_content()
253278 writes(sock, CONTENT_LENGTH, *data, "\r\n\r\n", data)
254279 end
255280
281+ # Initialize the JSON RPC HTTPSocket
256282 initially(addr, opt)
257283 if string(addr) then
258284 sock := open(addr,opt)
0 commit comments