|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Iterator |
| 4 | +from typing import Protocol, override, runtime_checkable |
| 5 | + |
| 6 | +from lsp_client.jsonrpc.id import jsonrpc_uuid |
| 7 | +from lsp_client.protocol import ( |
| 8 | + CapabilityClientProtocol, |
| 9 | + ServerRequestHook, |
| 10 | + ServerRequestHookProtocol, |
| 11 | + ServerRequestHookRegistry, |
| 12 | + WorkspaceCapabilityProtocol, |
| 13 | +) |
| 14 | +from lsp_client.utils.types import lsp_type |
| 15 | + |
| 16 | + |
| 17 | +@runtime_checkable |
| 18 | +class WithWorkspaceDiagnostic( |
| 19 | + WorkspaceCapabilityProtocol, |
| 20 | + ServerRequestHookProtocol, |
| 21 | + CapabilityClientProtocol, |
| 22 | + Protocol, |
| 23 | +): |
| 24 | + """ |
| 25 | + `workspace/diagnostic` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_diagnostic |
| 26 | + `workspace/diagnostic/refresh` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnostic_refresh |
| 27 | + """ |
| 28 | + |
| 29 | + @override |
| 30 | + @classmethod |
| 31 | + def iter_methods(cls) -> Iterator[str]: |
| 32 | + yield from super().iter_methods() |
| 33 | + yield lsp_type.WORKSPACE_DIAGNOSTIC |
| 34 | + yield lsp_type.WORKSPACE_DIAGNOSTIC_REFRESH |
| 35 | + |
| 36 | + @override |
| 37 | + @classmethod |
| 38 | + def register_workspace_capability( |
| 39 | + cls, cap: lsp_type.WorkspaceClientCapabilities |
| 40 | + ) -> None: |
| 41 | + super().register_workspace_capability(cap) |
| 42 | + cap.diagnostics = lsp_type.DiagnosticWorkspaceClientCapabilities( |
| 43 | + refresh_support=True, |
| 44 | + ) |
| 45 | + |
| 46 | + @override |
| 47 | + @classmethod |
| 48 | + def check_server_capability(cls, cap: lsp_type.ServerCapabilities) -> None: |
| 49 | + super().check_server_capability(cap) |
| 50 | + assert cap.diagnostic_provider |
| 51 | + assert cap.diagnostic_provider.workspace_diagnostics |
| 52 | + |
| 53 | + async def _request_workspace_diagnostic( |
| 54 | + self, params: lsp_type.WorkspaceDiagnosticParams |
| 55 | + ) -> lsp_type.WorkspaceDiagnosticReport: |
| 56 | + return await self.request( |
| 57 | + lsp_type.WorkspaceDiagnosticRequest( |
| 58 | + id=jsonrpc_uuid(), |
| 59 | + params=params, |
| 60 | + ), |
| 61 | + schema=lsp_type.WorkspaceDiagnosticResponse, |
| 62 | + ) |
| 63 | + |
| 64 | + async def request_workspace_diagnostic( |
| 65 | + self, |
| 66 | + *, |
| 67 | + identifier: str | None = None, |
| 68 | + previous_result_ids: list[lsp_type.PreviousResultId] | None = None, |
| 69 | + ) -> lsp_type.WorkspaceDiagnosticReport | None: |
| 70 | + """ |
| 71 | + `workspace/diagnostic` - Request diagnostic reports for the whole workspace. |
| 72 | + """ |
| 73 | + return await self._request_workspace_diagnostic( |
| 74 | + lsp_type.WorkspaceDiagnosticParams( |
| 75 | + identifier=identifier, |
| 76 | + previous_result_ids=previous_result_ids or [], |
| 77 | + ) |
| 78 | + ) |
| 79 | + |
| 80 | + async def _respond_diagnostic_refresh(self, params: None) -> None: |
| 81 | + return None |
| 82 | + |
| 83 | + async def respond_diagnostic_refresh( |
| 84 | + self, req: lsp_type.DiagnosticRefreshRequest |
| 85 | + ) -> lsp_type.DiagnosticRefreshResponse: |
| 86 | + return lsp_type.DiagnosticRefreshResponse( |
| 87 | + id=req.id, |
| 88 | + result=await self._respond_diagnostic_refresh(req.params), |
| 89 | + ) |
| 90 | + |
| 91 | + @override |
| 92 | + def register_server_request_hooks( |
| 93 | + self, registry: ServerRequestHookRegistry |
| 94 | + ) -> None: |
| 95 | + super().register_server_request_hooks(registry) |
| 96 | + registry.register( |
| 97 | + lsp_type.WORKSPACE_DIAGNOSTIC_REFRESH, |
| 98 | + ServerRequestHook( |
| 99 | + cls=lsp_type.DiagnosticRefreshRequest, |
| 100 | + execute=self.respond_diagnostic_refresh, |
| 101 | + ), |
| 102 | + ) |
0 commit comments