|
| 1 | +import collections.abc |
| 2 | +from pathlib import Path |
| 3 | +from types import TracebackType |
| 4 | +from typing import Any, Protocol, Self |
| 5 | + |
| 6 | + |
| 7 | +class ILspSession(Protocol): |
| 8 | + """An active LSP session with a language server. |
| 9 | +
|
| 10 | + Use as an async context manager: |
| 11 | +
|
| 12 | + - ``__aenter__`` starts the process, sends ``initialize`` request, |
| 13 | + sends ``initialized`` notification. |
| 14 | + - ``__aexit__`` sends ``shutdown`` request, sends ``exit`` notification, |
| 15 | + stops the process. |
| 16 | + """ |
| 17 | + |
| 18 | + async def __aenter__(self) -> Self: ... |
| 19 | + |
| 20 | + async def __aexit__( |
| 21 | + self, |
| 22 | + exc_type: type[BaseException] | None, |
| 23 | + exc_val: BaseException | None, |
| 24 | + exc_tb: TracebackType | None, |
| 25 | + ) -> None: ... |
| 26 | + |
| 27 | + # -- Async API --------------------------------------------------------- |
| 28 | + |
| 29 | + async def send_request( |
| 30 | + self, |
| 31 | + method: str, |
| 32 | + params: dict[str, Any] | None = None, |
| 33 | + timeout: float | None = None, |
| 34 | + ) -> Any: |
| 35 | + """Send an LSP request and return the result.""" |
| 36 | + ... |
| 37 | + |
| 38 | + async def send_notification( |
| 39 | + self, |
| 40 | + method: str, |
| 41 | + params: dict[str, Any] | None = None, |
| 42 | + ) -> None: |
| 43 | + """Send an LSP notification.""" |
| 44 | + ... |
| 45 | + |
| 46 | + # -- Sync API ---------------------------------------------------------- |
| 47 | + |
| 48 | + def send_request_sync( |
| 49 | + self, |
| 50 | + method: str, |
| 51 | + params: dict[str, Any] | None = None, |
| 52 | + timeout: float | None = None, |
| 53 | + ) -> Any: |
| 54 | + """Send an LSP request synchronously (blocks caller thread).""" |
| 55 | + ... |
| 56 | + |
| 57 | + def send_notification_sync( |
| 58 | + self, |
| 59 | + method: str, |
| 60 | + params: dict[str, Any] | None = None, |
| 61 | + ) -> None: |
| 62 | + """Send an LSP notification synchronously.""" |
| 63 | + ... |
| 64 | + |
| 65 | + # -- Server-initiated messages ----------------------------------------- |
| 66 | + |
| 67 | + def on_notification( |
| 68 | + self, |
| 69 | + method: str, |
| 70 | + handler: collections.abc.Callable[ |
| 71 | + [dict[str, Any] | None], collections.abc.Awaitable[None] |
| 72 | + ], |
| 73 | + ) -> None: |
| 74 | + """Register handler for server notifications.""" |
| 75 | + ... |
| 76 | + |
| 77 | + def on_request( |
| 78 | + self, |
| 79 | + method: str, |
| 80 | + handler: collections.abc.Callable[ |
| 81 | + [dict[str, Any] | None], collections.abc.Awaitable[Any] |
| 82 | + ], |
| 83 | + ) -> None: |
| 84 | + """Register handler for server-to-client requests.""" |
| 85 | + ... |
| 86 | + |
| 87 | + # -- Server info ------------------------------------------------------- |
| 88 | + |
| 89 | + @property |
| 90 | + def server_capabilities(self) -> dict[str, Any]: |
| 91 | + """Capabilities returned by the server in the initialize response.""" |
| 92 | + ... |
| 93 | + |
| 94 | + @property |
| 95 | + def server_info(self) -> dict[str, Any] | None: |
| 96 | + """Server info returned in the initialize response, if any.""" |
| 97 | + ... |
| 98 | + |
| 99 | + |
| 100 | +class ILspClient(Protocol): |
| 101 | + """Factory for creating LSP sessions with language servers.""" |
| 102 | + |
| 103 | + def session( |
| 104 | + self, |
| 105 | + cmd: str, |
| 106 | + root_uri: str, |
| 107 | + workspace_folders: list[dict[str, str]] | None = None, |
| 108 | + initialization_options: dict[str, Any] | None = None, |
| 109 | + client_capabilities: dict[str, Any] | None = None, |
| 110 | + cwd: Path | None = None, |
| 111 | + env: dict[str, str] | None = None, |
| 112 | + readable_id: str = "", |
| 113 | + ) -> ILspSession: |
| 114 | + """Create a new LSP session that launches a language server. |
| 115 | +
|
| 116 | + The session automatically performs the LSP initialization handshake. |
| 117 | +
|
| 118 | + Usage:: |
| 119 | +
|
| 120 | + async with lsp_client.session( |
| 121 | + cmd="pyright-langserver --stdio", |
| 122 | + root_uri="file:///path/to/project", |
| 123 | + ) as session: |
| 124 | + result = await session.send_request( |
| 125 | + "textDocument/completion", |
| 126 | + {"textDocument": {"uri": "file:///file.py"}, "position": {"line": 0, "character": 0}}, |
| 127 | + ) |
| 128 | +
|
| 129 | + Args: |
| 130 | + cmd: Shell command to start the language server. |
| 131 | + root_uri: The root URI of the workspace. |
| 132 | + workspace_folders: Optional workspace folders (each with 'uri' and 'name' keys). |
| 133 | + initialization_options: Optional server-specific initialization options. |
| 134 | + client_capabilities: Optional client capabilities override. |
| 135 | + cwd: Working directory for the subprocess. |
| 136 | + env: Environment variables for the subprocess. |
| 137 | + readable_id: Human-readable identifier for logging. |
| 138 | +
|
| 139 | + Returns: |
| 140 | + An async context manager yielding ILspSession. |
| 141 | + """ |
| 142 | + ... |
0 commit comments