PyLSPClient/examples/clangd.py

85 lines
2.7 KiB
Python

# Python imports
import argparse
import subprocess
import threading
# Lib imports
# Application imports
import pylspclient
from .capabilities import Capabilities
class ReadPipe(threading.Thread):
def __init__(self, pipe):
threading.Thread.__init__(self)
self.pipe = pipe
def run(self):
line = self.pipe.readline().decode('utf-8')
while line:
line = self.pipe.readline().decode('utf-8')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = 'PyLSPClient example with clangd')
parser.add_argument('clangd_path', type=str, default="/usr/bin/clangd-6.0", help = 'the clangd path', nargs="?")
args = parser.parse_args()
p = subprocess.Popen([args.clangd_path], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
read_pipe = ReadPipe(p.stderr)
read_pipe.start()
json_rpc_endpoint = pylspclient.JsonRpcEndpoint(p.stdin, p.stdout)
# To work with socket: sock_fd = sock.makefile()
lsp_endpoint = pylspclient.LspEndpoint(json_rpc_endpoint)
lsp_client = pylspclient.LspClient(lsp_endpoint)
root_uri = 'file:///home/osboxes/projects/ctest/'
workspace_folders = [{'name': 'python-lsp', 'uri': root_uri}]
print(lsp_client.initialize(p.pid, None, root_uri, None, Capabilities.data, "off", workspace_folders))
print(lsp_client.initialized())
file_path = "/home/osboxes/projects/ctest/test.c"
uri = f"file://{file_path}"
text = open(file_path, "r").read()
languageId = pylspclient.lsp_structs.LANGUAGE_IDENTIFIER.C
version = 1
lsp_client.didOpen(pylspclient.lsp_structs.TextDocumentItem(uri, languageId, version, text))
try:
symbols = lsp_client.documentSymbol(pylspclient.lsp_structs.TextDocumentIdentifier(uri))
for symbol in symbols:
print(symbol.name)
except pylspclient.lsp_structs.ResponseError:
print("Failed to load document symbols...")
lsp_client.definition(
pylspclient.lsp_structs.TextDocumentIdentifier(uri),
pylspclient.lsp_structs.Position(14, 4)
)
lsp_client.signatureHelp(
pylspclient.lsp_structs.TextDocumentIdentifier(uri),
pylspclient.lsp_structs.Position(14, 4)
)
lsp_client.definition(
pylspclient.lsp_structs.TextDocumentIdentifier(uri),
pylspclient.lsp_structs.Position(14, 4)
)
lsp_client.completion(
pylspclient.lsp_structs.TextDocumentIdentifier(uri),
pylspclient.lsp_structs.Position(14, 4),
pylspclient.lsp_structs.CompletionContext(pylspclient.lsp_structs.CompletionTriggerKind.Invoked)
)
lsp_client.shutdown()
lsp_client.exit()