Skip to content

Commit 19f4668

Browse files
authored
Merge pull request #134 from ahoppen/ahoppen/ignore-notifications-from-lsp
Ignore notifications sent from sourcekit-lsp when waiting for a request reply
2 parents ef8ee97 + 231a477 commit 19f4668

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

test-sourcekit-lsp/test-sourcekit-lsp.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,11 @@ def send_data(self, dict: Dict[str, object]):
4444
self.process.stdin.write(data)
4545
self.process.stdin.flush()
4646

47-
def send_request(self, method: str, params: Dict[str, object]) -> str:
47+
def read_message_from_lsp_server(self) -> str:
4848
"""
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.
5051
"""
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-
6252
assert self.process.stdout
6353
# Read Content-Length: 123\r\n
6454
# 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:
7161
assert empty_line == "\n", f"Expected empty line, got '{empty_line}'"
7262

7363
# 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.
7581
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)
79102

80103
def send_notification(self, method: str, params: Dict[str, object]):
81104
"""

0 commit comments

Comments
 (0)