Код: Выделить всё
Content-Length: 119
{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"Pyright language server 1.1.391 starting"}}Content-Length: 152
{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"Server root directory: file:///opt/homebrew/lib/node_modules/pyright/dist"}}
Сценарий, который я использую:
import subprocess
import json
import time
process = subprocess.Popen(
["pyright-langserver", "--stdio"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
def send_request(id, method, params):
request = json.dumps(
{"jsonrpc": "2.0", "id": id, "method": method, "params": params}
)
request += "\n" # Add newline to signify end of request
print(f"Sending request: {request}")
process.stdin.write(request.encode())
process.stdin.flush()
def read_response():
buffer = b""
while b"\r\n\r\n" not in buffer:
buffer += process.stdout.read(1)
headers, content = buffer.split(b"\r\n\r\n", 1)
content_length = next(
int(header.split(b":")[1].strip())
for header in headers.split(b"\r\n")
if header.startswith(b"Content-Length")
)
while len(content) < content_length:
content += process.stdout.read(1)
print(f"Raw JSON response: {content.decode()}")
return json.loads(content.decode())
# init request
send_request(
1,
"initialize",
{"rootUri": "file:///Users/arpit/Programming/Python/test-project"},
)
response = read_response()
print("Initialize response:", response)
# Sending a simple completion request
send_request(
2,
"textDocument/completion",
{
"textDocument": {
"uri": "file:///Users/arpit/Programming/Python/test-project/main.py"
},
"position": {"line": 7, "character": 20},
},
)
response = read_response()
print("Completion response:", response)
# Sending a simple completion request again
send_request(
2,
"textDocument/completion",
{
"textDocument": {
"uri": "file:///Users/arpit/Programming/Python/test-project/main.py"
},
"position": {"line": 5, "character": 10},
},
)
response = read_response()
print("Completion response:", response)
< /code>
Любая помощь в этом очень ценится! < /p>
Я попытался имитировать некоторые вызовы JSONRPC, предоставив их через сценарий Python для процесса пирайта. Тем не менее, вывод не исходит в результате моих команд ввода. Вывод, который я получаю, является стандартным выводом инициализации, который мы получаем, даже если запустим процесс с терминала, и он просто блокирует любые дальнейшие команды ввода.
Подробнее здесь: https://stackoverflow.com/questions/793 ... t-any-code