LspServer
Provides Language Server Protocol functionality for code intelligence to provide IDE-like features such as code completion, symbol search, and more.
Constructors
new LspServer()
new LspServer( languageId: LspLanguageId, pathToProject: string, apiClient: LspApi): LspServerParameters:
languageIdLspLanguageIdpathToProjectstringapiClientLspApi
Returns:
LspServer
Methods
completions()
completions(path: string, position: Position): Promise<CompletionList>Gets completion suggestions at a position in a file.
Parameters:
pathstring - Path to the file. Relative paths are resolved based on the project path set in the LSP server constructor.positionPosition - 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, 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:
pathstring - Path to the closed file. Relative paths are resolved based on the project path set in the LSP server constructor.
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, enabling language features like diagnostics and completions for that file. The server will begin tracking the file’s contents and providing language features.
Parameters:
pathstring - Path to the opened file. Relative paths are resolved based on the sandbox working directory.
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 filedocumentSymbols()
documentSymbols(path: string): Promise<LspSymbol[]>Get symbol information (functions, classes, variables, etc.) from a document.
Parameters:
pathstring - Path to the file to get symbols from. Relative paths are resolved based on the project path set in the LSP server constructor.
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}`);});sandboxSymbols()
sandboxSymbols(query: string): Promise<LspSymbol[]>Searches for symbols matching the query string across the entire Sandbox.
Parameters:
querystring - 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.sandboxSymbols('User');symbols.forEach(symbol => { console.log(`${symbol.name} (${symbol.kind}) in ${symbol.location}`);});start()
start(): Promise<void>Starts the language server, 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 = await sandbox.createLspServer('typescript', 'workspace/project');await lsp.start(); // Initialize the server// Now ready for LSP operationsstop()
stop(): Promise<void>Stops the language server, 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 resourcesworkspaceSymbols()
workspaceSymbols(query: string): Promise<LspSymbol[]>Searches for symbols matching the query string across the entire Sandbox.
Parameters:
querystring - 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
Deprecated
Use sandboxSymbols instead. This method will be removed in a future version.
LspLanguageId
Supported language server types.
Enum Members:
JAVASCRIPT(“javascript”)PYTHON(“python”)TYPESCRIPT(“typescript”)
Position
Represents a zero-based position within a text document, specified by line number and character offset.
Properties:
characternumber - Zero-based character offset on the linelinenumber - Zero-based line number in the document
Example:
const position: Position = { line: 10, // Line 11 (zero-based) character: 15 // Character 16 on the line (zero-based)};