Я столкнулся с запуском Pyright как pyright-langserver --stdio, но я вообще не могу заставить его работать. Например, как мне передать ему эти вызовы Json?
Я пытался создать скрипт Python для имитации вызовов JsonRPC, но он просто блокируется после вывода стандартных трех строк инициализации:
Код: Выделить всё
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)
Все, что я получаю от поиска в Интернете, — это то, как интегрировать Pyright в существующие редакторы кода, такие как neovim, vim, emacs и т. д.
Я попытался смоделировать некоторые вызовы JsonRPC, передав их через скрипт Python процессу Pyright. Однако в результате моих команд ввода никаких результатов не происходит. Вывод, который я получаю, представляет собой стандартный вывод инициализации, который мы получаем, даже если запускаем процесс с терминала, и он просто блокирует любые дальнейшие команды ввода.
Подробнее здесь: https://stackoverflow.com/questions/793 ... t-any-code