Skip to content

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]])

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.