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 a language server
Section titled “Start a 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", ))const apiUrl = "https://api.leap0.dev"const apiKey = "<your-api-key>"const sandboxId = "<sandbox_id>"
const res = await fetch(`${apiUrl}/v1/sandbox/${sandboxId}/lsp/start`, {method: "POST",headers: { authorization: apiKey, "Content-Type": "application/json",},body: JSON.stringify({ language_id: "python", path_to_project: "/home/user/project",}),})
console.log(await res.json()) // { success: true }Open a document
Section titled “Open a document”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,)await fetch(`${apiUrl}/v1/sandbox/${sandboxId}/lsp/did-open`, {method: "POST",headers: { authorization: apiKey, "Content-Type": "application/json",},body: JSON.stringify({ language_id: "python", path_to_project: "/home/user/project", uri: "file:///home/user/project/main.py", text: fileContent, version: 1,}),})Get completions
Section titled “Get completions”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"])const res = await fetch(`${apiUrl}/v1/sandbox/${sandboxId}/lsp/completions`, {method: "POST",headers: { authorization: apiKey, "Content-Type": "application/json",},body: JSON.stringify({ language_id: "python", path_to_project: "/home/user/project", uri: "file:///home/user/project/main.py", position: { line: 1, character: 8 },}),})
const data = await res.json()data.items?.slice(0, 5).forEach(item => console.log(item.label))Get document symbols
Section titled “Get document symbols”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"])const res = await fetch(`${apiUrl}/v1/sandbox/${sandboxId}/lsp/document-symbols`,{ method: "POST", headers: { authorization: apiKey, "Content-Type": "application/json", }, body: JSON.stringify({ language_id: "python", path_to_project: "/home/user/project", uri: "file:///home/user/project/main.py", }),})
const symbols = await res.json()symbols.forEach(s => console.log(s.name, s.kind))Close a document
Section titled “Close a document”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",)await fetch(`${apiUrl}/v1/sandbox/${sandboxId}/lsp/did-close`, {method: "POST",headers: { authorization: apiKey, "Content-Type": "application/json",},body: JSON.stringify({ language_id: "python", path_to_project: "/home/user/project", uri: "file:///home/user/project/main.py",}),})Stop the language server
Section titled “Stop the language server”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",)await fetch(`${apiUrl}/v1/sandbox/${sandboxId}/lsp/stop`, {method: "POST",headers: { authorization: apiKey, "Content-Type": "application/json",},body: JSON.stringify({ language_id: "python", path_to_project: "/home/user/project",}),})