LspServer | Daytona

LspServer

class LspServer()

Provides Language Server Protocol functionality for code intelligence to provide IDE-like features such as code completion, symbol search, and more.

LspServer.init

def __init__(language_id: LspLanguageId | LspLanguageIdLiteral,
             path_to_project: str, api_client: LspApi)

Initializes a new LSP server instance.

Arguments:

LspServer.start

@intercept_errors(message_prefix="Failed to start LSP server: ")
@with_instrumentation()

def start(request_timeout: float | None = None) -> 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.

Arguments:

Example:

lsp = sandbox.create_lsp_server("typescript", "workspace/project")
lsp.start()  # Initialize the server
# Now ready for LSP operations

LspServer.stop

@intercept_errors(message_prefix="Failed to stop LSP server: ")
@with_instrumentation()

def stop(request_timeout: float | None = None) -> None

Stops the language server.

This method should be called when the LSP server is no longer needed to free up system resources.

Arguments:

Example:

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

LspServer.did_open

@intercept_errors(message_prefix="Failed to open file: ")
@with_instrumentation()

def did_open(path: str, request_timeout: float | None = None) -> 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:

Example:

# When opening a file for editing
lsp.did_open("workspace/project/src/index.ts")
# Now can get completions, symbols, etc. for this file

LspServer.did_close

@intercept_errors(message_prefix="Failed to close file: ")
@with_instrumentation()

def did_close(path: str, request_timeout: float | None = None) -> 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:

Example:

# When done editing a file
lsp.did_close("workspace/project/src/index.ts")

LspServer.document_symbols

@intercept_errors(message_prefix="Failed to get symbols from document: ")
@with_instrumentation()

def document_symbols(path: str, request_timeout: float | None = None) -> list[LspSymbol]

Gets symbol information (functions, classes, variables, etc.) from a document.

Arguments:

Returns:

Example:

# Get all symbols in a file
symbols = lsp.document_symbols("workspace/project/src/index.ts")
for symbol in symbols:
    print(f"{symbol.kind} {symbol.name}: {symbol.location}")

LspServer.workspace_symbols

@deprecated(
    reason=
    "Method is deprecated. Use `sandbox_symbols` instead. This method will be removed in a future version."
)
@with_instrumentation()

def workspace_symbols(query: str, request_timeout: float | None = None) -> list[LspSymbol]

Searches for symbols matching the query string across all files in the Sandbox.

Arguments:

Returns:

LspServer.sandbox_symbols

@intercept_errors(message_prefix="Failed to get symbols from sandbox: ")
@with_instrumentation()

def sandbox_symbols(query: str, request_timeout: float | None = None) -> list[LspSymbol]

Searches for symbols matching the query string across all files in the Sandbox.

Arguments:

Returns:

Example:

# Search for all symbols containing "User"
symbols = lsp.sandbox_symbols("User")
for symbol in symbols:
    print(f"{symbol.name} in {symbol.location}")

LspServer.completions

@intercept_errors(message_prefix="Failed to get completions: ")
@with_instrumentation()

def completions(path: str,
                position: LspCompletionPosition,
                request_timeout: float | None = None) -> CompletionList

Gets completion suggestions at a position in a file.

Arguments:

Returns:

Example:

# Get completions at a specific position
pos = LspCompletionPosition(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}")

LspLanguageId

class LspLanguageId(str, Enum)

Language IDs for Language Server Protocol (LSP).

Enum Members:

LspCompletionPosition

@dataclass
class LspCompletionPosition()

Represents a zero-based completion position in a text document, specified by line number and character offset.

Attributes: