Skip to content

Language Server Protocol

View as Markdown

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.

Follow this order when using LSP in a sandbox:

  1. Create an LSP server instance with create_lsp_server or createLspServer
  2. Call start() to initialize the language server; LSP methods fail until the server is started
  3. Call didOpen() on a file before requesting completions or document symbols for that file
  4. Use completions, document symbols, or sandbox symbols
  5. Call didClose() when you finish with a file
  6. Call stop() when the LSP server is no longer needed

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 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 operations

Stop an LSP server by calling stop() when the LSP server is no longer needed.

# When done with LSP features
lsp.stop() # Clean up resources

Get code completions for a specific position in a file by providing the file path and position.

  • Position values are zero-based (line: 0 is 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}")

Daytona provides methods to notify the LSP server when files are opened or closed. This enables completion and symbol tracking for the specified files.

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

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

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}")

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}")