apps/docs/src/content/docs/en/python-sdk/async/async-lsp-server.mdx
class AsyncLspServer()
Provides Language Server Protocol functionality for code intelligence to provide IDE-like features such as code completion, symbol search, and more.
def __init__(language_id: LspLanguageId | LspLanguageIdLiteral,
path_to_project: str, api_client: LspApi)
Initializes a new LSP server instance.
Arguments:
language_id LspLanguageId | LspLanguageIdLiteral - The language server type
(e.g., LspLanguageId.TYPESCRIPT).path_to_project str - Absolute path to the project root directory.api_client LspApi - API client for Sandbox operations.@intercept_errors(message_prefix="Failed to start LSP server: ")
@with_instrumentation()
async 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 = sandbox.create_lsp_server("typescript", "workspace/project")
await lsp.start() # Initialize the server
# Now ready for LSP operations
@intercept_errors(message_prefix="Failed to stop LSP server: ")
@with_instrumentation()
async 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 features
await lsp.stop() # Clean up resources
@intercept_errors(message_prefix="Failed to open file: ")
@with_instrumentation()
async 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 - Path to the opened file. Relative paths are resolved based on the project path
set in the LSP server constructor.Example:
# When opening a file for editing
await lsp.did_open("workspace/project/src/index.ts")
# Now can get completions, symbols, etc. for this file
@intercept_errors(message_prefix="Failed to close file: ")
@with_instrumentation()
async 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 - Path to the closed file. Relative paths are resolved based on the project path
set in the LSP server constructor.Example:
# When done editing a file
await lsp.did_close("workspace/project/src/index.ts")
@intercept_errors(message_prefix="Failed to get symbols from document: ")
@with_instrumentation()
async def document_symbols(path: str) -> list[LspSymbol]
Gets symbol information (functions, classes, variables, etc.) from a document.
Arguments:
path str - Path to the file to get symbols from. Relative paths are resolved based on the project path
set in the LSP server constructor.Returns:
list[LspSymbol] - List of symbols in the document. Each symbol includes:
Example:
# Get all symbols in a file
symbols = await lsp.document_symbols("workspace/project/src/index.ts")
for symbol in symbols:
print(f"{symbol.kind} {symbol.name}: {symbol.location}")
@deprecated(
reason=
"Method is deprecated. Use `sandbox_symbols` instead. This method will be removed in a future version."
)
@with_instrumentation()
async def workspace_symbols(query: str) -> list[LspSymbol]
Searches for symbols matching the query string across all files in the Sandbox.
Arguments:
query str - Search query to match against symbol names.Returns:
list[LspSymbol] - List of matching symbols from all files.@intercept_errors(message_prefix="Failed to get symbols from sandbox: ")
@with_instrumentation()
async def sandbox_symbols(query: str) -> list[LspSymbol]
Searches for symbols matching the query string across all files in the Sandbox.
Arguments:
query str - Search query to match against symbol names.Returns:
list[LspSymbol] - List of matching symbols from all files. Each symbol
includes:
Example:
# Search for all symbols containing "User"
symbols = await lsp.sandbox_symbols("User")
for symbol in symbols:
print(f"{symbol.name} in {symbol.location}")
@intercept_errors(message_prefix="Failed to get completions: ")
@with_instrumentation()
async def completions(path: str,
position: LspCompletionPosition) -> CompletionList
Gets completion suggestions at a position in a file.
Arguments:
path str - Path to the file. Relative paths are resolved based on the project path
set in the LSP server constructor.position LspCompletionPosition - Cursor position to get completions for.Returns:
CompletionList - List of completion suggestions. The list includes:
Example:
# Get completions at a specific position
pos = LspCompletionPosition(line=10, character=15)
completions = await lsp.completions("workspace/project/src/index.ts", pos)
for item in completions.items:
print(f"{item.label} ({item.kind}): {item.detail}")
class LspLanguageId(str, Enum)
Language IDs for Language Server Protocol (LSP).
Enum Members:
- PYTHON ("python")
- TYPESCRIPT ("typescript")
- JAVASCRIPT ("javascript")
@dataclass
class LspCompletionPosition()
Represents a zero-based completion position in 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.