Skip to content

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 workspace
const workspace = await daytona.create();
// Create and start LSP server
const lsp = workspace.createLspServer('typescript', '/workspace/project');
await lsp.start();
// Open a file for editing
await lsp.didOpen('/workspace/project/src/index.ts');
// Get completions at a position
const completions = await lsp.completions(
'/workspace/project/src/index.ts',
{ line: 10, character: 15 }
);
console.log('Completions:', completions);
// Get document symbols
const symbols = await lsp.documentSymbols('/workspace/project/src/index.ts');
symbols.forEach(symbol => {
console.log(`${symbol.name}: ${symbol.kind}`);
});
// Clean up
await lsp.didClose('/workspace/project/src/index.ts');
await lsp.stop();

LspServer

[view_source]

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

[view_source]

Parameters
ParameterType
languageIdLspLanguageId
pathToProjectstring
toolboxApiToolboxApi
instanceWorkspace
Returns

LspServer

Methods

completions()

completions(path: string, position: Position): Promise<CompletionList>

[view_source]

Gets completion suggestions at a position in a file.

Parameters
ParameterTypeDescription
pathstringAbsolute path to the file
positionPositionThe 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 position
const 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>

[view_source]

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
ParameterTypeDescription
pathstringAbsolute path to the closed file
Returns

Promise<void>

Example
// When done editing a file
await lsp.didClose('/workspace/project/src/index.ts');

didOpen()

didOpen(path: string): Promise<void>

[view_source]

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
ParameterTypeDescription
pathstringAbsolute path to the opened file
Returns

Promise<void>

Example
// When opening a file for editing
await lsp.didOpen('/workspace/project/src/index.ts');
// Now can get completions, symbols, etc. for this file

documentSymbols()

documentSymbols(path: string): Promise<LspSymbol[]>

[view_source]

Get symbol information from a document.

This method returns information about all symbols (functions, classes, variables, etc.) defined in the specified document.

Parameters
ParameterTypeDescription
pathstringAbsolute 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 file
const symbols = await lsp.documentSymbols('/workspace/project/src/index.ts');
symbols.forEach(symbol => {
console.log(`${symbol.kind} ${symbol.name}: ${symbol.location}`);
});

start()

start(): Promise<void>

[view_source]

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>

[view_source]

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 features
await lsp.stop(); // Clean up resources

workspaceSymbols()

workspaceSymbols(query: string): Promise<LspSymbol[]>

[view_source]

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
ParameterTypeDescription
querystringSearch 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

[view_source]

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

PropertyTypeDescription
characternumberZero-based character offset on the line
linenumberZero-based line number in the document

LspLanguageId

type LspLanguageId = "python" | "typescript" | "javascript";

[view_source]

Supported language server types. Currently ‘python’, ‘typescript’ and ‘javascript’ are supported.