Language Server Protocol
The Daytona SDK provides Language Server Protocol (LSP) support through Sandbox instances. This enables advanced language features like code completion, diagnostics, and more.
Example:
Basic LSP server usage:
workspace = daytona.create()
# Create and start LSP serverlsp = workspace.create_lsp_server("typescript", "/workspace/project")lsp.start()
# Open a file for editinglsp.did_open("/workspace/project/src/index.ts")
# Get completions at a positionpos = Position(line=10, character=15)completions = lsp.completions("/workspace/project/src/index.ts", pos)print(f"Completions: {completions}")
# Get document symbolssymbols = lsp.document_symbols("/workspace/project/src/index.ts")for symbol in symbols: print(f"{symbol.name}: {symbol.kind}")
# Clean uplsp.did_close("/workspace/project/src/index.ts")lsp.stop()
Notes:
The LSP server must be started with start() before using any other methods, and should be stopped with stop() when no longer needed to free resources.
LspServer
class LspServer()
Provides Language Server Protocol functionality for code intelligence.
This class implements a subset of the Language Server Protocol (LSP) to provide IDE-like features such as code completion, symbol search, and more.
Attributes:
language_id
LspLanguageId - The language server type (e.g., “python”, “typescript”).path_to_project
str - Absolute path to the project root directory.instance
WorkspaceInstance - The Sandbox instance this server belongs to.
LspServer.__init__
def __init__(language_id: LspLanguageId, path_to_project: str, toolbox_api: ToolboxApi, instance: WorkspaceInstance)
Initializes a new LSP server instance.
Arguments:
language_id
LspLanguageId - The language server type (e.g., “typescript”).path_to_project
str - Absolute path to the project root directory.toolbox_api
ToolboxApi - API client for Sandbox operations.instance
WorkspaceInstance - The Sandbox instance this server belongs to.
LspServer.start
def start() -> None
Starts the language server.
This method must be called before using any other LSP functionality. It initializes the language server for the specified language and project.
Example:
lsp = workspace.create_lsp_server("typescript", "/workspace/project")lsp.start() # Initialize the server# Now ready for LSP operations
LspServer.stop
def stop() -> None
Stops the language server.
This method should be called when the LSP server is no longer needed to free up system resources.
Example:
# When done with LSP featureslsp.stop() # Clean up resources
LspServer.did_open
def did_open(path: str) -> None
Notifies the language server that a file has been opened.
This method should be called when a file is opened in the editor to enable language features like diagnostics and completions for that file. The server will begin tracking the file’s contents and providing language features.
Arguments:
path
str - Absolute path to the opened file.
Example:
# When opening a file for editinglsp.did_open("/workspace/project/src/index.ts")# Now can get completions, symbols, etc. for this file
LspServer.did_close
def did_close(path: str) -> None
Notify the language server that a file has been closed.
This method should be called when a file is closed in the editor to allow the language server to clean up any resources associated with that file.
Arguments:
path
str - Absolute path to the closed file.
Example:
# When done editing a filelsp.did_close("/workspace/project/src/index.ts")
LspServer.document_symbols
def document_symbols(path: str) -> List[LspSymbol]
Gets symbol information from a document.
This method returns information about all symbols (functions, classes, variables, etc.) defined in the specified document.
Arguments:
path
str - Absolute path to the file to get symbols from.
Returns:
List[LspSymbol]
- List of symbols in the document. Each symbol includes:- name: The symbol’s name
- kind: The symbol’s kind (function, class, variable, etc.)
- location: The location of the symbol in the file
Example:
# Get all symbols in a filesymbols = lsp.document_symbols("/workspace/project/src/index.ts")for symbol in symbols: print(f"{symbol.kind} {symbol.name}: {symbol.location}")
LspServer.workspace_symbols
def workspace_symbols(query: str) -> List[LspSymbol]
Searches for symbols across the entire Sandbox.
This method searches for symbols matching the query string across all files in the Sandbox. It’s useful for finding declarations and definitions without knowing which file they’re in.
Arguments:
query
str - Search query to match against symbol names.
Returns:
List[LspSymbol]
- List of matching symbols from all files. Each symbol includes:- name: The symbol’s name
- kind: The symbol’s kind (function, class, variable, etc.)
- location: The location of the symbol in the file
Example:
# Search for all symbols containing "User"symbols = lsp.workspace_symbols("User")for symbol in symbols: print(f"{symbol.name} in {symbol.location}")
LspServer.completions
def completions(path: str, position: Position) -> CompletionList
Gets completion suggestions at a position in a file.
Arguments:
path
str - Absolute path to the file.position
Position - Cursor position to get completions for.
Returns:
CompletionList
- List of completion suggestions. The list includes:- isIncomplete: Whether more items might be available
- items: List of completion items, each containing:
- label: The text to insert
- kind: The kind of completion
- detail: Additional details about the item
- documentation: Documentation for the item
- sortText: Text used to sort the item in the list
- filterText: Text used to filter the item
- insertText: The actual text to insert (if different from label)
Example:
# Get completions at a specific positionpos = Position(line=10, character=15)completions = lsp.completions("/workspace/project/src/index.ts", pos)for item in completions.items: print(f"{item.label} ({item.kind}): {item.detail}")
Position
class Position()
Represents a position in a text document.
This class represents a zero-based position within a text document, specified by line number and character offset.
Attributes:
line
int - Zero-based line number in the document.character
int - Zero-based character offset on the line.
Position.__init__
def __init__(line: int, character: int)
Initialize a new Position instance.
Arguments:
line
int - Zero-based line number in the document.character
int - Zero-based character offset on the line.