Language Server Protocol
Interact with language server protocol servers in your sandboxes.
Language Server Protocol (LSP) support is available through LSP server instances created from a sandbox. A language server runs inside the sandbox and analyzes the project's code, so your application gets IDE-grade code intelligence for code that never leaves the sandbox.
An LSP server covers code completions, file open and close notifications, document symbols, and sandbox-wide symbol search. Each server instance is scoped to one language and one project directory; Python and TypeScript language servers are available by default.
Workflow
Follow this order when using LSP in a sandbox:
- Create an LSP server instance with
create_lsp_serverorcreateLspServer - Call
start()to initialize the language server; LSP methods fail until the server is started - Call
didOpen()on a file before requesting completions or document symbols for that file - Use completions, document symbols, or sandbox symbols
- Call
didClose()when you finish with a file - Call
stop()when the LSP server is no longer needed
Create LSP servers
Create an LSP server instance by providing the language ID and the path to the project.
- Python:
LspLanguageId.PYTHON - TypeScript and JavaScript:
LspLanguageId.TYPESCRIPT - Custom: Install the language server for your target language
from daytona import Daytona, LspLanguageId
# Create Sandbox
daytona = Daytona()
sandbox = daytona.create()
# Create LSP server for Python
lsp_server = sandbox.create_lsp_server(
language_id=LspLanguageId.PYTHON,
path_to_project="workspace/project"
)Start LSP servers
Start an LSP server by calling start() before any other LSP operation.
lsp = sandbox.create_lsp_server(
language_id=LspLanguageId.PYTHON,
path_to_project="workspace/project"
)
lsp.start() # Initialize the server
# Now ready for LSP operationsStop LSP servers
Stop an LSP server by calling stop() when the LSP server is no longer needed.
# When done with LSP features
lsp.stop() # Clean up resourcesCode completions
Get code completions for a specific position in a file by providing the file path and position.
- Position values are zero-based (
line: 0is the first line) - Call
didOpen()on the file before requesting completions
completions = lsp_server.completions(
path="workspace/project/main.py",
position={"line": 10, "character": 15}
)
print(f"Completions: {completions}")File notifications
Daytona provides methods to notify the LSP server when files are opened or closed. This enables completion and symbol tracking for the specified files.
Open file
Notify the language server that a file has been opened for editing by providing the path to the file. The server reads the file contents from disk at open time.
# Notify server that a file is open
lsp_server.did_open("workspace/project/main.py")Close file
Notify the language server that a file has been closed by providing the path to the file. This allows the server to clean up resources associated with that file.
# Notify server that a file is closed
lsp_server.did_close("workspace/project/main.py")Document symbols
Retrieve symbols (functions, classes, variables, etc.) from a document by providing the path to the file.
symbols = lsp_server.document_symbols("workspace/project/main.py")
for symbol in symbols:
print(f"Symbol: {symbol.name}, Kind: {symbol.kind}")Sandbox symbols
Search for symbols across all files in the sandbox by providing the query and the language ID. The Java SDK uses workspaceSymbols() instead of sandboxSymbols().
symbols = lsp_server.sandbox_symbols("MyClass")
for symbol in symbols:
print(f"Found: {symbol.name} at {symbol.location}")