Skip to content

Commit 6f3fbea

Browse files
authored
Merge pull request #539 from Jafaral/json-logger-lib
uni: move jsonrpc and logger to lib
2 parents d378ffa + fa7c8f8 commit 6f3fbea

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed

uni/lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ UFILES=gui.u file_dlg.u db.u \
1818
undoableedit.u undomanager.u url.u webup.u \
1919
notification.u md5.u httprequest.u httpresponse.u exception.u \
2020
thread.u matrix_util.u struct.u db_util.u database.u addressdb.u \
21-
union.u pdb_util.u propertydb.u property.u utf8.u unittest.u
21+
union.u pdb_util.u propertydb.u property.u utf8.u unittest.u jsonrpc.u logger.u
2222

2323
# COPYINS are library files copied in from other directories for
2424
# organizational purposes. Inspired by ipl/lib. They have to be
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@
3131
# In case of an error, and error objet should bre returned.
3232
#
3333

34-
package ulsp
35-
import json
34+
package json
3635
link 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
4041
class 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"
192214
end
@@ -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)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
# TODO: Make the class thread-safe
5252
#
5353

54-
package ulsp
54+
package logger
5555

5656
link strings
5757

uni/ulsp/Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ UFLAGS=-s -u
66
prog=ulsp
77

88
SRC=launch-lsp.icn file_handler.icn database.icn server.icn completion.icn signature.icn hover.icn \
9-
definition.icn jsonrpc.icn logger.icn lsif.icn symbols.icn folding.icn formatting.icn
9+
definition.icn lsif.icn symbols.icn folding.icn formatting.icn
1010
OBJ=launch-lsp.u file_handler.u database.u server.u completion.u signature.u hover.u definition.u \
11-
jsonrpc.u logger.u lsif.u symbols.u folding.u formatting.u
11+
lsif.u symbols.u folding.u formatting.u
1212

1313
export IPATH=$(UNI)/unidoc
1414

@@ -19,14 +19,14 @@ $(prog): $(OBJ)
1919
$(UC) -o $(prog) $(OBJ)
2020
$(CP) $(prog)$(EXE) ../../bin
2121

22-
jsonrpc-test: jsonrpc-test.u jsonrpc.u
23-
$(UC) -o jsonrpc-test jsonrpc-test.u jsonrpc.u
22+
jsonrpc-test: jsonrpc-test.u
23+
$(UC) -o jsonrpc-test jsonrpc-test.u
2424

2525
launch-lsp.u:launch-lsp.icn file_handler.u database.u server.u completion.u signature.u hover.u \
2626
definition.u symbols.u folding.u formatting.u
2727

28-
server.u:server.icn database.u completion.u file_handler.u signature.u hover.u definition.u jsonrpc.u \
29-
logger.u symbols.u folding.u formatting.u
28+
server.u:server.icn database.u completion.u file_handler.u signature.u hover.u definition.u \
29+
symbols.u folding.u formatting.u
3030
hover.u:hover.icn signature.u
3131
definition.u: definition.icn hover.u
3232

uni/ulsp/database.icn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
package ulsp
1212

13+
1314
import UniDoc
1415
link paths
16+
import logger
1517

16-
global Logger
1718

1819
class LSPDB(
1920
package_db,

uni/ulsp/jsonrpc-test.icn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ulsp
2+
import json
23

34
global work_items
45

uni/ulsp/launch-lsp.icn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# -Instantiates Server class and calls run() to start server on validated port number.
99
#
1010
import ulsp
11+
import logger
1112
link options
1213
link basename
1314
link ximage

uni/ulsp/lsif.icn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import UniDoc
22
import ulsp
33
import json
4+
import logger
45
link ximage
56

67
#<p>

uni/ulsp/server.icn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package ulsp
1111
link ximage
1212
link strings
1313
import json
14+
import logger
1415
#import UniDoc
1516

1617

0 commit comments

Comments
 (0)