Skip to content

Language Server Protocol

LSP endpoints let you start language servers inside a sandbox and interact with them for code completions, document symbols, and document lifecycle notifications. Supported languages: Python (pyright) and TypeScript/JavaScript (typescript-language-server).

Start the LSP server for a language and project. This spawns the server process and sends the LSP initialize handshake automatically.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
print(
sandbox.lsp.start(
language_id="python",
path_to_project="/home/user/project",
)
)

Notify the language server that a document was opened. This must be called before requesting completions or symbols.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.lsp.did_open_path(
language_id="python",
path_to_project="/home/user/project",
path="/home/user/project/main.py",
text="import os\nos.path.",
version=1,
)

Request code completions at a cursor position.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
response = sandbox.lsp.completions_path(
language_id="python",
path_to_project="/home/user/project",
path="/home/user/project/main.py",
line=1,
character=8,
)
items = response.result or []
for item in items[:5]:
print(item["label"])

Request all symbols (functions, classes, variables) in a document.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
response = sandbox.lsp.document_symbols_path(
language_id="python",
path_to_project="/home/user/project",
path="/home/user/project/main.py",
)
for symbol in response.result or []:
print(symbol["name"], symbol["kind"])

Notify the language server that a document was closed.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.lsp.did_close_path(
language_id="python",
path_to_project="/home/user/project",
path="/home/user/project/main.py",
)

Send shutdown and exit to the language server and terminate the process.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.lsp.stop(
language_id="python",
path_to_project="/home/user/project",
)