Leap0

Language Server Protocol

Start and interact with language servers for code intelligence inside a sandbox.

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

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",
    )
)
import { Leap0Client } from "leap0";

const client = new Leap0Client();
const sandbox = await client.sandboxes.create();

console.log(
  await sandbox.lsp.start({
    languageId: "python",
    pathToProject: "/home/user/project",
  }),
);
await client.close();

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,
)
import { Leap0Client } from "leap0";

const client = new Leap0Client();
const sandbox = await client.sandboxes.create();

await sandbox.lsp.didOpenPath({
  languageId: "python",
  pathToProject: "/home/user/project",
  path: "/home/user/project/main.py",
  text: "import os\nos.path.",
  version: 1,
});

await client.close();

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"])
import { Leap0Client } from "leap0";

const client = new Leap0Client();
const sandbox = await client.sandboxes.create();
const response = await sandbox.lsp.completionsPath({
  languageId: "python",
  pathToProject: "/home/user/project",
  path: "/home/user/project/main.py",
  line: 1,
  character: 8,
});

const items = response.result ?? [];
items.slice(0, 5).forEach((item) => console.log(item.label));
await client.close();

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"])
import { Leap0Client } from "leap0";

const client = new Leap0Client();
const sandbox = await client.sandboxes.create();
const response = await sandbox.lsp.documentSymbolsPath({
  languageId: "python",
  pathToProject: "/home/user/project",
  path: "/home/user/project/main.py",
});

for (const symbol of response.result ?? []) {
  console.log(symbol.name, symbol.kind);
}

await client.close();

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",
)
import { Leap0Client } from "leap0";

const client = new Leap0Client();
const sandbox = await client.sandboxes.create();

await sandbox.lsp.didClosePath({
  languageId: "python",
  pathToProject: "/home/user/project",
  path: "/home/user/project/main.py",
});

await client.close();

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",
)
import { Leap0Client } from "leap0";

const client = new Leap0Client();
const sandbox = await client.sandboxes.create();

await sandbox.lsp.stop({
  languageId: "python",
  pathToProject: "/home/user/project",
});
await client.close();

Was this page helpful?

On this page