@@ -44,21 +44,11 @@ def send_data(self, dict: Dict[str, object]):
44
44
self .process .stdin .write (data )
45
45
self .process .stdin .flush ()
46
46
47
- def send_request (self , method : str , params : Dict [ str , object ] ) -> str :
47
+ def read_message_from_lsp_server (self ) -> str :
48
48
"""
49
- Send a request of the given method and parameters to the LSP server and wait for the response.
49
+ Read a single message sent from the LSP server to the client.
50
+ This can be a request reply, notification or request sent from the server to the client.
50
51
"""
51
- self .request_id += 1
52
-
53
- self .send_data (
54
- {
55
- "jsonrpc" : "2.0" ,
56
- "id" : self .request_id ,
57
- "method" : method ,
58
- "params" : params ,
59
- }
60
- )
61
-
62
52
assert self .process .stdout
63
53
# Read Content-Length: 123\r\n
64
54
# Note: Even though the Content-Length header ends with \r\n, `readline` returns it with a single \n.
@@ -71,11 +61,44 @@ def send_request(self, method: str, params: Dict[str, object]) -> str:
71
61
assert empty_line == "\n " , f"Expected empty line, got '{ empty_line } '"
72
62
73
63
# Read the actual response
74
- response = self .process .stdout .read (int (match .group (1 )))
64
+ return self .process .stdout .read (int (match .group (1 )))
65
+
66
+ def read_request_reply_from_lsp_server (self , request_id : int ) -> str :
67
+ """
68
+ Read all messages sent from the LSP server until we see a request reply.
69
+ Assert that this request reply was for the given request_id and return it.
70
+ """
71
+ message = self .read_message_from_lsp_server ()
72
+ message_obj = json .loads (message )
73
+ if "result" not in message_obj :
74
+ # We received a message that wasn't the request reply.
75
+ # Log it, ignore it and wait for the next message.
76
+ print ("Received message" )
77
+ print (message )
78
+ return self .read_request_reply_from_lsp_server (request_id )
79
+ # We always wait for a request reply before sending the next request.
80
+ # If we received a request reply, it should thus have the request ID of the last request that we sent.
75
81
assert (
76
- f'"id":{ self .request_id } ' in response
77
- ), f"Expected response for request { self .request_id } , got '{ response } '"
78
- return response
82
+ message_obj ["id" ] == self .request_id
83
+ ), f"Expected response for request { self .request_id } , got '{ message } '"
84
+ return message
85
+
86
+ def send_request (self , method : str , params : Dict [str , object ]) -> str :
87
+ """
88
+ Send a request of the given method and parameters to the LSP server and wait for the response.
89
+ """
90
+ self .request_id += 1
91
+
92
+ self .send_data (
93
+ {
94
+ "jsonrpc" : "2.0" ,
95
+ "id" : self .request_id ,
96
+ "method" : method ,
97
+ "params" : params ,
98
+ }
99
+ )
100
+
101
+ return self .read_request_reply_from_lsp_server (self .request_id )
79
102
80
103
def send_notification (self , method : str , params : Dict [str , object ]):
81
104
"""
0 commit comments