コンテンツにスキップ

LspServer

class LspServer()

コードインテリジェンスのための Language Server Protocol 機能を提供し、コード補完やシンボル検索などの IDE 風の機能を実現します。

LspServer.init

def __init__(language_id: LspLanguageId, path_to_project: str,
toolbox_api: ToolboxApi, sandbox_id: str)

新しい LSP サーバーインスタンスを初期化します。

引数:

  • language_id LspLanguageId - 言語サーバーの種類(例: LspLanguageId.TYPESCRIPT)。
  • path_to_project str - プロジェクトのルートディレクトリへの絶対パス。
  • toolbox_api ToolboxApi - サンドボックス操作用の API クライアント。
  • instance SandboxInstance - このサーバーが属するサンドボックスインスタンス。

LspServer.start

@intercept_errors(message_prefix="Failed to start LSP server: ")
def start() -> None

言語サーバーを起動します。

このメソッドは他の LSP 機能を使用する前に呼び出す必要があります。指定された言語とプロジェクトに対して言語サーバーを初期化します。

:

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: ")
def stop() -> None

言語サーバーを停止します。

このメソッドは LSP サーバーが不要になったときに呼び出して、システムリソースを解放します。

:

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

LspServer.did_open

@intercept_errors(message_prefix="Failed to open file: ")
def did_open(path: str) -> None

ファイルが開かれたことを言語サーバーに通知します。

エディタでファイルを開いたときにこのメソッドを呼び出すと、そのファイルに対する診断や補完などの言語機能が有効になります。サーバーはファイル内容の追跡を開始し、言語機能を提供します。

引数:

  • path str - 開いたファイルへのパス。相対パスは LSP サーバーのコンストラクタで設定されたプロジェクトパスに基づいて解決されます。

:

# 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: ")
def did_close(path: str) -> None

ファイルが閉じられたことを言語サーバーに通知します。

エディタでファイルを閉じたときにこのメソッドを呼び出すことで、そのファイルに関連するリソースを言語サーバーがクリーンアップできます。

引数:

  • path str - 閉じたファイルへのパス。相対パスは LSP サーバーのコンストラクタで設定されたプロジェクトパスに基づいて解決されます。

:

# 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: ")
def document_symbols(path: str) -> List[LspSymbol]

ドキュメントからシンボル情報(関数、クラス、変数など)を取得します。

引数:

  • path str - シンボルを取得するファイルへのパス。相対パスは LSP サーバーのコンストラクタで設定されたプロジェクトパスに基づいて解決されます。

戻り値:

  • List[LspSymbol] - ドキュメント内のシンボル一覧。各シンボルには以下が含まれます:
    • name: シンボル名
    • kind: シンボルの種類(function、class、variable など)
    • location: ファイル内でのシンボルの位置

:

# 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."
)
def workspace_symbols(query: str) -> List[LspSymbol]

クエリ文字列に一致するシンボルをサンドボックス内の全ファイルから検索します。

引数:

  • query str - シンボル名に照合する検索クエリ。

戻り値:

  • List[LspSymbol] - すべてのファイルからの一致シンボルの一覧。

LspServer.sandbox_symbols

@intercept_errors(message_prefix="Failed to get symbols from sandbox: ")
def sandbox_symbols(query: str) -> List[LspSymbol]

クエリ文字列に一致するシンボルを、サンドボックス内のすべてのファイルから検索します。

引数:

  • query str - シンボル名に対して照合する検索クエリ。

返り値:

  • List[LspSymbol] - すべてのファイルから一致したシンボルの一覧。各シンボルには次が含まれます:
    • name: シンボル名
    • kind: シンボルの種別(function、class、variable など)
    • location: ファイル内でのシンボル位置

:

# 「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: ")
def completions(path: str, position: Position) -> CompletionList

ファイル内の指定位置で補完候補を取得します。

引数:

  • path str - ファイルパス。相対パスは、LSP サーバーのコンストラクターで設定したプロジェクトパスを基準に解決されます。
  • position Position - 補完を取得するカーソル位置。

返り値:

  • CompletionList - 補完候補の一覧。以下を含みます:
    • isIncomplete: さらに項目が利用可能な可能性があるか
    • items: 補完項目のリスト。各項目には次が含まれます:
    • label: 挿入するテキスト
    • kind: 補完の種別
    • detail: 項目に関する追加情報
    • documentation: 項目のドキュメント
    • sortText: 一覧でのソートに使うテキスト
    • filterText: フィルターに使うテキスト
    • insertText: 実際に挿入するテキスト(label と異なる場合)

:

# 特定の位置で補完候補を取得
pos = Position(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(Enum)

LSP(言語サーバープロトコル)で使用する言語ID。

列挙メンバー:

  • PYTHON (“python”)
  • TYPESCRIPT (“typescript”)
  • JAVASCRIPT (“javascript”)

Position

class Position()

テキストドキュメント内のゼロ基準の位置を表し、行番号と文字オフセットで指定します。

属性:

  • line int - ドキュメント内のゼロ基準の行番号。
  • character int - 行内のゼロ基準の文字オフセット。

Position.__init__

def __init__(line: int, character: int)

新しい Position インスタンスを初期化します。

引数:

  • line int - ドキュメント内のゼロ基準の行番号。
  • character int - 行内のゼロ基準の文字オフセット。