Language servers
sandbox.lsp answers completions, hover, definitions, references, symbols and diagnostics for one file. It uses a real language server
inside the sandbox. install(language) installs the server once per sandbox, for python, javascript, typescript, go or
java.
from svm import Sandbox
CODE = "import os\n\ndef greet(name: str) -> str:\n return 'hi ' + nme\n\nos.pa\n"
with Sandbox.create(template="base") as sandbox: sandbox.files.write("/tmp/project/app.py", CODE) lsp = sandbox.lsp lsp.install("python") # pyright; needs egress to the package registry
for diagnostic in lsp.diagnostics("python", "/tmp/project", "app.py"): print(diagnostic["range"]["start"]["line"], diagnostic["message"])
# Lines and characters are zero-based: complete after "os.pa". completions = lsp.completions("python", "/tmp/project", "app.py", line=5, character=5) items = completions["items"] if isinstance(completions, dict) else completions print([item["label"] for item in items[:5]])import { Sandbox } from "@svm/sandbox";
const sandbox = await Sandbox.create({ template: "base" });try { await sandbox.files.write("/home/user/proj/app.py", "import os\n\ndef main() -> int:\n return os.\n"); await sandbox.lsp.install("python"); // pyright; needs internet egress
// file is relative to the project root; line and character are 0-based. const diagnostics = await sandbox.lsp.diagnostics("python", "/home/user/proj", "app.py"); console.log(JSON.stringify(diagnostics, null, 2));
const completions = await sandbox.lsp.completions("python", "/home/user/proj", "app.py", 3, 14); console.log(JSON.stringify(completions).slice(0, 500));} finally { await sandbox.kill();}lsp := sandbox.LSP()if err := lsp.Install(ctx, "python"); err != nil { // needs internet egress for the packages return err}
source := "import os\nprint(undefined_name)\nos.pa\n"if _, err := files.Write(ctx, "/tmp/project/main.py", []byte(source), 0o644); err != nil { return err}diagnostics, err := lsp.Diagnostics(ctx, "python", "/tmp/project", "/tmp/project/main.py")if err != nil { return err}fmt.Println("diagnostics:", string(diagnostics))
// Lines and characters are zero-based: line 2, after "os.pa".completions, err := lsp.Completions(ctx, "python", "/tmp/project", "/tmp/project/main.py", 2, 5)if err != nil { return err}fmt.Println("completions:", string(completions))require "svm"
box = SVM::Sandbox.create(template: "base")begin box.files.write("/home/user/proj/app.py", "import os\n\nvalue: int = 'text'\nos.pa\n")
lsp = box.lsp lsp.install("python") # pyright, with internet egress
problems = lsp.diagnostics("python", "/home/user/proj", "app.py") problems.each { |d| puts "line #{d['range']['start']['line']}: #{d['message']}" }
# Completions at line 3, character 5 (both 0-based): after "os.pa". items = lsp.completions("python", "/home/user/proj", "app.py", 3, 5) puts items.inspectensure box.killendimport dev.svm.sandbox.Client;import dev.svm.sandbox.LanguageServers;import dev.svm.sandbox.Sandbox;
public class LanguageServersExample { public static void main(String[] args) { Client client = Client.fromEnvironment(); try (Sandbox sandbox = Sandbox.create(client, "base")) { LanguageServers lsp = sandbox.lsp(); lsp.install("python"); // pyright; needs internet egress
String root = "/home/user/proj"; sandbox.files().write(root + "/app.py", "import os\nx: int = 'text'\nos.pa\n");
Object diagnostics = lsp.diagnostics("python", root, "app.py"); System.out.println("diagnostics: " + diagnostics);
// Line and character are zero-based: after "os.pa" on line 2. Object completions = lsp.completions("python", root, "app.py", 2, 5); System.out.println("completions: " + completions); } }}Diagnostics are pushed by the server when it is ready, so diagnostics() waits up to 8 seconds for the first publish, then waits until
no new publish has arrived for 1.5 seconds. If the server published nothing, the call raises the SDK’s timeout error instead of
returning an empty list that would read as “no problems”. Repeat the call once the project has loaded.