AsyncLspServer
class AsyncLspServer()コードインテリジェンスのための Language Server Protocol 機能を提供し、コード補完やシンボル検索などの IDE 相当の機能を実現します。
AsyncLspServer.init
def __init__(language_id: LspLanguageId, path_to_project: str, toolbox_api: ToolboxApi, sandbox_id: str)新しい LSP サーバーインスタンスを初期化します。
引数:
language_idLspLanguageId - 言語サーバーの種類(例: LspLanguageId.TYPESCRIPT)。path_to_projectstr - プロジェクトのルートディレクトリへの絶対パス。toolbox_apiToolboxApi - サンドボックス操作用の API クライアント。instanceSandboxInstance - このサーバーが属するサンドボックスインスタンス。
AsyncLspServer.start
@intercept_errors(message_prefix="Failed to start LSP server: ")async def start() -> None言語サーバーを起動します。
このメソッドは、他の LSP 機能を使用する前に呼び出す必要があります。指定した言語とプロジェクト向けに言語サーバーを初期化します。
例:
lsp = sandbox.create_lsp_server("typescript", "workspace/project")await lsp.start() # Initialize the server# Now ready for LSP operationsAsyncLspServer.stop
@intercept_errors(message_prefix="Failed to stop LSP server: ")async def stop() -> None言語サーバーを停止します。
LSP サーバーが不要になったときは、システムリソースを解放するためにこのメソッドを呼び出してください。
例:
# When done with LSP featuresawait lsp.stop() # Clean up resourcesAsyncLspServer.did_open
@intercept_errors(message_prefix="Failed to open file: ")async def did_open(path: str) -> Noneファイルが開かれたことを言語サーバーに通知します。
このメソッドは、エディタでファイルを開いたときに呼び出し、そのファイルに対する診断や補完などの言語機能を有効化します。サーバーはファイル内容の追跡を開始し、言語機能を提供します。
引数:
pathstr - 開いたファイルのパス。相対パスは LSP サーバーのコンストラクタで設定されたプロジェクトパスに基づいて解決されます。
例:
# When opening a file for editingawait lsp.did_open("workspace/project/src/index.ts")# Now can get completions, symbols, etc. for this fileAsyncLspServer.did_close
@intercept_errors(message_prefix="Failed to close file: ")async def did_close(path: str) -> Noneファイルが閉じられたことを言語サーバーに通知します。
このメソッドは、エディタでファイルを閉じたときに呼び出し、そのファイルに関連するリソースを言語サーバーがクリーンアップできるようにします。
引数:
pathstr - 閉じたファイルのパス。相対パスは LSP サーバーのコンストラクタで設定されたプロジェクトパスに基づいて解決されます。
例:
# When done editing a fileawait lsp.did_close("workspace/project/src/index.ts")AsyncLspServer.document_symbols
@intercept_errors(message_prefix="Failed to get symbols from document: ")async def document_symbols(path: str) -> List[LspSymbol]ドキュメントからシンボル情報(関数、クラス、変数など)を取得します。
引数:
pathstr - シンボルを取得するファイルのパス。相対パスは LSP サーバーのコンストラクタで設定されたプロジェクトパスに基づいて解決されます。
戻り値:
List[LspSymbol]- ドキュメント内のシンボルのリスト。各シンボルには以下が含まれます:- name: シンボル名
- kind: シンボルの種類(関数、クラス、変数など)
- location: ファイル内のシンボルの位置
例:
# Get all symbols in a filesymbols = await lsp.document_symbols("workspace/project/src/index.ts")for symbol in symbols: print(f"{symbol.kind} {symbol.name}: {symbol.location}")AsyncLspServer.workspace_symbols
@deprecated( reason= "Method is deprecated. Use `sandbox_symbols` instead. This method will be removed in a future version.")async def workspace_symbols(query: str) -> List[LspSymbol]サンドボックス内のすべてのファイルを対象に、クエリ文字列に一致するシンボルを検索します。
引数:
querystr - シンボル名に対してマッチさせる検索クエリ。
戻り値:
List[LspSymbol]- すべてのファイルから一致したシンボルのリスト。
AsyncLspServer.sandbox_symbols
@intercept_errors(message_prefix="Failed to get symbols from sandbox: ")async def sandbox_symbols(query: str) -> List[LspSymbol]サンドボックス内のすべてのファイルを対象に、クエリ文字列に一致するシンボルを検索します。
引数:
querystr - シンボル名に対してマッチさせる検索クエリ。
戻り値:
List[LspSymbol]- すべてのファイルから一致したシンボルのリスト。各シンボルには次が含まれます:- name: シンボル名
- kind: シンボルの種別(関数、クラス、変数など)
- location: ファイル内でのシンボルの位置
例:
# "User" を含むすべてのシンボルを検索symbols = await lsp.sandbox_symbols("User")for symbol in symbols: print(f"{symbol.name} in {symbol.location}")AsyncLspServer.completions
@intercept_errors(message_prefix="Failed to get completions: ")async def completions(path: str, position: Position) -> CompletionListファイル内の指定位置での補完候補を取得します。
引数:
pathstr - ファイルパス。相対パスは、LSP サーバーのコンストラクタで設定されたプロジェクトパスを基準に解決されます。positionPosition - 補完候補を取得するカーソル位置。
戻り値:
CompletionList- 補完候補のリスト。次を含みます:- isIncomplete: 追加の項目が存在する可能性があるか
- items: 補完アイテムのリスト。各アイテムには次が含まれます:
- label: 挿入するテキスト
- kind: 補完の種別
- detail: アイテムに関する追加情報
- documentation: アイテムのドキュメント
- sortText: リスト内での並び替えに用いるテキスト
- filterText: フィルタリングに用いるテキスト
- insertText: 実際に挿入するテキスト(label と異なる場合)
例:
# 指定位置で補完候補を取得pos = Position(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}")LspLanguageId
class LspLanguageId(Enum)LSP(Language Server Protocol)の言語ID。
列挙メンバー:
PYTHON(“python”)TYPESCRIPT(“typescript”)JAVASCRIPT(“javascript”)
Position
class Position()テキストドキュメント内の0始まりの位置を表し、行番号と文字オフセットで指定します。
属性:
lineint - ドキュメント内の0始まりの行番号。characterint - 行内の0始まりの文字オフセット。
Position.init
def __init__(line: int, character: int)新しい Position インスタンスを初期化します。
引数:
lineint - ドキュメント内の0始まりの行番号。characterint - 行内の0始まりの文字オフセット。