|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from collections.abc import AsyncGenerator, Sequence |
| 5 | +from contextlib import asynccontextmanager |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import attrs |
| 10 | + |
| 11 | +from lsp_client.capability.request.definition import WithRequestDefinition |
| 12 | +from lsp_client.capability.request.hover import WithRequestHover |
| 13 | +from lsp_client.client.abc import Client |
| 14 | +from lsp_client.utils.types import Position, Range, lsp_type |
| 15 | + |
| 16 | +type LspResponse[R] = R | None |
| 17 | + |
| 18 | + |
| 19 | +@attrs.define |
| 20 | +class LspInteraction[C: Client]: |
| 21 | + client: C |
| 22 | + workspace_root: Path |
| 23 | + |
| 24 | + def full_path(self, relative_path: str) -> Path: |
| 25 | + return (self.workspace_root / relative_path).resolve() |
| 26 | + |
| 27 | + async def create_file(self, relative_path: str, content: str) -> Path: |
| 28 | + path = self.full_path(relative_path) |
| 29 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 30 | + path.write_text(content) |
| 31 | + return path |
| 32 | + |
| 33 | + async def request_definition( |
| 34 | + self, relative_path: str, line: int, column: int |
| 35 | + ) -> DefinitionAssertion: |
| 36 | + assert isinstance(self.client, WithRequestDefinition) |
| 37 | + path = self.full_path(relative_path) |
| 38 | + resp = await self.client.request_definition( |
| 39 | + file_path=path, |
| 40 | + position=Position(line=line, character=column), |
| 41 | + ) |
| 42 | + return DefinitionAssertion(self, resp) |
| 43 | + |
| 44 | + async def request_hover( |
| 45 | + self, relative_path: str, line: int, column: int |
| 46 | + ) -> HoverAssertion: |
| 47 | + assert isinstance(self.client, WithRequestHover) |
| 48 | + path = self.full_path(relative_path) |
| 49 | + resp = await self.client.request_hover( |
| 50 | + file_path=path, |
| 51 | + position=Position(line=line, character=column), |
| 52 | + ) |
| 53 | + return HoverAssertion(self, resp) |
| 54 | + |
| 55 | + |
| 56 | +@attrs.define |
| 57 | +class DefinitionAssertion: |
| 58 | + interaction: LspInteraction[Any] |
| 59 | + response: ( |
| 60 | + lsp_type.Location |
| 61 | + | Sequence[lsp_type.Location] |
| 62 | + | Sequence[lsp_type.LocationLink] |
| 63 | + | None |
| 64 | + ) |
| 65 | + |
| 66 | + def expect_definition( |
| 67 | + self, |
| 68 | + relative_path: str, |
| 69 | + start_line: int, |
| 70 | + start_col: int, |
| 71 | + end_line: int, |
| 72 | + end_col: int, |
| 73 | + ) -> None: |
| 74 | + assert self.response is not None, "Definition response is None" |
| 75 | + |
| 76 | + expected_path = self.interaction.full_path(relative_path) |
| 77 | + expected_range = Range( |
| 78 | + start=Position(line=start_line, character=start_col), |
| 79 | + end=Position(line=end_line, character=end_col), |
| 80 | + ) |
| 81 | + |
| 82 | + match self.response: |
| 83 | + case lsp_type.Location() as loc: |
| 84 | + actual_path = self.interaction.client.from_uri(loc.uri) |
| 85 | + assert Path(actual_path).resolve() == expected_path, ( |
| 86 | + f"Expected path {expected_path}, got {actual_path}" |
| 87 | + ) |
| 88 | + assert loc.range == expected_range |
| 89 | + case list() | Sequence() as locs: |
| 90 | + found = False |
| 91 | + for loc in locs: |
| 92 | + if isinstance(loc, lsp_type.Location): |
| 93 | + actual_path = self.interaction.client.from_uri(loc.uri) |
| 94 | + actual_range = loc.range |
| 95 | + elif isinstance(loc, lsp_type.LocationLink): |
| 96 | + actual_path = self.interaction.client.from_uri(loc.target_uri) |
| 97 | + actual_range = loc.target_selection_range |
| 98 | + else: |
| 99 | + continue |
| 100 | + |
| 101 | + if ( |
| 102 | + Path(actual_path).resolve() == expected_path |
| 103 | + and actual_range == expected_range |
| 104 | + ): |
| 105 | + found = True |
| 106 | + break |
| 107 | + |
| 108 | + assert found, ( |
| 109 | + f"Definition not found at {expected_path}:{expected_range}" |
| 110 | + ) |
| 111 | + case _: |
| 112 | + raise TypeError( |
| 113 | + f"Unexpected definition response type: {type(self.response)}" |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +@attrs.define |
| 118 | +class HoverAssertion: |
| 119 | + interaction: LspInteraction[Any] |
| 120 | + response: lsp_type.MarkupContent | None |
| 121 | + |
| 122 | + def expect_content(self, pattern: str) -> None: |
| 123 | + assert self.response is not None, "Hover response is None" |
| 124 | + assert pattern in self.response.value, ( |
| 125 | + f"Expected '{pattern}' in hover content, got '{self.response.value}'" |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +@asynccontextmanager |
| 130 | +async def lsp_interaction_context[C: Client]( |
| 131 | + client_cls: type[C], workspace_root: Path | None = None, **client_kwargs: Any |
| 132 | +) -> AsyncGenerator[LspInteraction[C], None]: |
| 133 | + if workspace_root is None: |
| 134 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 135 | + root = Path(tmpdir).resolve() |
| 136 | + async with client_cls(workspace=root, **client_kwargs) as client: |
| 137 | + yield LspInteraction(client=client, workspace_root=root) |
| 138 | + else: |
| 139 | + root = workspace_root.resolve() |
| 140 | + async with client_cls(workspace=root, **client_kwargs) as client: |
| 141 | + yield LspInteraction(client=client, workspace_root=root) |
0 commit comments