@@ -30,8 +30,7 @@ def __init__(self, server_path: str):
30
30
self .process = subprocess .Popen (
31
31
[server_path ],
32
32
stdin = subprocess .PIPE ,
33
- stdout = subprocess .PIPE ,
34
- encoding = "utf-8" ,
33
+ stdout = subprocess .PIPE
35
34
)
36
35
37
36
def send_data (self , dict : Dict [str , object ]):
@@ -40,8 +39,8 @@ def send_data(self, dict: Dict[str, object]):
40
39
"""
41
40
assert self .process .stdin
42
41
body = json .dumps (dict )
43
- data = "Content-Length: {}\r \n \r \n {}" . format ( len ( body ), body )
44
- self .process .stdin .write (data )
42
+ data = f "Content-Length: { len ( body ) } \r \n \r \n { body } "
43
+ self .process .stdin .write (data . encode ( 'utf-8' ) )
45
44
self .process .stdin .flush ()
46
45
47
46
def read_message_from_lsp_server (self ) -> str :
@@ -51,17 +50,16 @@ def read_message_from_lsp_server(self) -> str:
51
50
"""
52
51
assert self .process .stdout
53
52
# Read Content-Length: 123\r\n
54
- # Note: Even though the Content-Length header ends with \r\n, `readline` returns it with a single \n.
55
- header = self .process .stdout .readline ()
56
- match = re .match (r"Content-Length: ([0-9]+)\n$" , header )
53
+ header = self .process .stdout .readline ().decode ('utf-8' )
54
+ match = re .match (r"Content-Length: ([0-9]+)\r\n$" , header )
57
55
assert match , f"Expected Content-Length header, got '{ header } '"
58
56
59
57
# The Content-Length header is followed by an empty line
60
- empty_line = self .process .stdout .readline ()
61
- assert empty_line == "\n " , f"Expected empty line, got '{ empty_line } '"
58
+ empty_line = self .process .stdout .readline (). decode ( 'utf-8' )
59
+ assert empty_line == "\r \ n " , f"Expected empty line, got '{ empty_line } '"
62
60
63
61
# Read the actual response
64
- return self .process .stdout .read (int (match .group (1 )))
62
+ return self .process .stdout .read (int (match .group (1 ))). decode ( 'utf-8' )
65
63
66
64
def read_request_reply_from_lsp_server (self , request_id : int ) -> str :
67
65
"""
@@ -71,7 +69,7 @@ def read_request_reply_from_lsp_server(self, request_id: int) -> str:
71
69
message = self .read_message_from_lsp_server ()
72
70
message_obj = json .loads (message )
73
71
if "result" not in message_obj :
74
- # We received a message that wasn't the request reply.
72
+ # We received a message that wasn't the request reply.
75
73
# Log it, ignore it and wait for the next message.
76
74
print ("Received message" )
77
75
print (message )
0 commit comments