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.
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.
Example
// Basic LSP server usage// Create and initialize workspaceconst workspace = await daytona.create();
// Create and start LSP serverconst lsp = workspace.createLspServer('typescript', '/workspace/project');await lsp.start();
// Open a file for editingawait lsp.didOpen('/workspace/project/src/index.ts');
// Get completions at a positionconst completions = await lsp.completions( '/workspace/project/src/index.ts', { line: 10, character: 15 });console.log('Completions:', completions);
// Get document symbolsconst symbols = await lsp.documentSymbols('/workspace/project/src/index.ts');symbols.forEach(symbol => { console.log(`${symbol.name}: ${symbol.kind}`);});
// Clean upawait lsp.didClose('/workspace/project/src/index.ts');await lsp.stop();
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.
LspServer
Constructors
new LspServer()
new LspServer( languageId: LspLanguageId, pathToProject: string, toolboxApi: ToolboxApi, instance: Workspace): LspServer
Parameters
Parameter | Type |
---|---|
languageId | LspLanguageId |
pathToProject | string |
toolboxApi | ToolboxApi |
instance | Workspace |
Returns
LspServer
Methods
completions()
completions(path: string, position: Position): Promise<CompletionList>
Gets completion suggestions at a position in a file.
Parameters
Parameter | Type | Description |
---|---|---|
path | string | Absolute path to the file |
position | Position | The position in the file where completion was requested |
Returns
Promise<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 positionconst completions = await lsp.completions('/workspace/project/src/index.ts', { line: 10, character: 15});completions.items.forEach(item => { console.log(`${item.label} (${item.kind}): ${item.detail}`);});
didClose()
didClose(path: string): Promise<void>
Notifies 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.
Parameters
Parameter | Type | Description |
---|---|---|
path | string | Absolute path to the closed file |
Returns
Promise<void>
Example
// When done editing a fileawait lsp.didClose('/workspace/project/src/index.ts');
didOpen()
didOpen(path: string): Promise<void>
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.
Parameters
Parameter | Type | Description |
---|---|---|
path | string | Absolute path to the opened file |
Returns
Promise<void>
Example
// When opening a file for editingawait lsp.didOpen('/workspace/project/src/index.ts');// Now can get completions, symbols, etc. for this file
documentSymbols()
documentSymbols(path: string): Promise<LspSymbol[]>
Get symbol information from a document.
This method returns information about all symbols (functions, classes, variables, etc.) defined in the specified document.
Parameters
Parameter | Type | Description |
---|---|---|
path | string | Absolute path to the file to get symbols from |
Returns
Promise<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 fileconst symbols = await lsp.documentSymbols('/workspace/project/src/index.ts');symbols.forEach(symbol => { console.log(`${symbol.kind} ${symbol.name}: ${symbol.location}`);});
start()
start(): Promise<void>
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.
Returns
Promise<void>
Example
const lsp = workspace.createLspServer('typescript', '/workspace/project');await lsp.start(); // Initialize the server// Now ready for LSP operations
stop()
stop(): Promise<void>
Stops the language server.
This method should be called when the LSP server is no longer needed to free up system resources.
Returns
Promise<void>
Example
// When done with LSP featuresawait lsp.stop(); // Clean up resources
workspaceSymbols()
workspaceSymbols(query: string): Promise<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.
Parameters
Parameter | Type | Description |
---|---|---|
query | string | Search query to match against symbol names |
Returns
Promise<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"const symbols = await lsp.workspaceSymbols('User');symbols.forEach(symbol => { console.log(`${symbol.name} (${symbol.kind}) in ${symbol.location}`);});
Position
Represents a position in a text document.
This interface represents a zero-based position within a text document, specified by line number and character offset.
Position
Example
const position: Position = { line: 10, // Line 11 (zero-based) character: 15 // Character 16 on the line (zero-based)};
Properties
Property | Type | Description |
---|---|---|
character | number | Zero-based character offset on the line |
line | number | Zero-based line number in the document |
LspLanguageId
type LspLanguageId = "python" | "typescript" | "javascript";
Supported language server types. Currently ‘python’, ‘typescript’ and ‘javascript’ are supported.