|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
| 4 | +from typing import Protocol, override, runtime_checkable |
| 5 | + |
| 6 | +from loguru import logger |
| 7 | + |
| 8 | +from lsp_client.jsonrpc.id import jsonrpc_uuid |
| 9 | +from lsp_client.protocol import ( |
| 10 | + CapabilityClientProtocol, |
| 11 | + TextDocumentCapabilityProtocol, |
| 12 | +) |
| 13 | +from lsp_client.utils.types import AnyPath, lsp_type |
| 14 | + |
| 15 | + |
| 16 | +@runtime_checkable |
| 17 | +class WithRequestPullDiagnostic( |
| 18 | + TextDocumentCapabilityProtocol, |
| 19 | + CapabilityClientProtocol, |
| 20 | + Protocol, |
| 21 | +): |
| 22 | + """ |
| 23 | + `textDocument/diagnostic` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_pullDiagnostics |
| 24 | + """ |
| 25 | + |
| 26 | + @override |
| 27 | + @classmethod |
| 28 | + def methods(cls) -> Sequence[str]: |
| 29 | + return (lsp_type.TEXT_DOCUMENT_DIAGNOSTIC,) |
| 30 | + |
| 31 | + @override |
| 32 | + @classmethod |
| 33 | + def register_text_document_capability( |
| 34 | + cls, cap: lsp_type.TextDocumentClientCapabilities |
| 35 | + ) -> None: |
| 36 | + super().register_text_document_capability(cap) |
| 37 | + cap.diagnostic = lsp_type.DiagnosticClientCapabilities( |
| 38 | + dynamic_registration=True, |
| 39 | + related_document_support=True, |
| 40 | + related_information=True, |
| 41 | + tag_support=lsp_type.ClientDiagnosticsTagOptions( |
| 42 | + value_set=[*lsp_type.DiagnosticTag] |
| 43 | + ), |
| 44 | + code_description_support=True, |
| 45 | + data_support=True, |
| 46 | + ) |
| 47 | + |
| 48 | + @override |
| 49 | + @classmethod |
| 50 | + def check_server_capability( |
| 51 | + cls, |
| 52 | + cap: lsp_type.ServerCapabilities, |
| 53 | + info: lsp_type.ServerInfo | None, |
| 54 | + ) -> None: |
| 55 | + super().check_server_capability(cap, info) |
| 56 | + assert cap.diagnostic_provider |
| 57 | + |
| 58 | + async def request_diagnostic( |
| 59 | + self, |
| 60 | + file_path: AnyPath, |
| 61 | + *, |
| 62 | + identifier: str | None = None, |
| 63 | + previous_result_id: str | None = None, |
| 64 | + ) -> lsp_type.DocumentDiagnosticReport | None: |
| 65 | + """ |
| 66 | + `textDocument/diagnostic` - Request a diagnostic report for a document. |
| 67 | + """ |
| 68 | + return await self.file_request( |
| 69 | + lsp_type.DocumentDiagnosticRequest( |
| 70 | + id=jsonrpc_uuid(), |
| 71 | + params=lsp_type.DocumentDiagnosticParams( |
| 72 | + text_document=lsp_type.TextDocumentIdentifier( |
| 73 | + uri=self.as_uri(file_path) |
| 74 | + ), |
| 75 | + identifier=identifier, |
| 76 | + previous_result_id=previous_result_id, |
| 77 | + ), |
| 78 | + ), |
| 79 | + schema=lsp_type.DocumentDiagnosticResponse, |
| 80 | + file_paths=[file_path], |
| 81 | + ) |
| 82 | + |
| 83 | + async def request_diagnostics( |
| 84 | + self, |
| 85 | + file_path: AnyPath, |
| 86 | + *, |
| 87 | + identifier: str | None = None, |
| 88 | + previous_result_id: str | None = None, |
| 89 | + ) -> Sequence[lsp_type.Diagnostic] | None: |
| 90 | + """ |
| 91 | + Request diagnostics for a document. Returns only the list of diagnostics. |
| 92 | + """ |
| 93 | + match await self.request_diagnostic( |
| 94 | + file_path, |
| 95 | + identifier=identifier, |
| 96 | + previous_result_id=previous_result_id, |
| 97 | + ): |
| 98 | + case lsp_type.RelatedFullDocumentDiagnosticReport(items=items): |
| 99 | + return items |
| 100 | + case lsp_type.FullDocumentDiagnosticReport(items=items): |
| 101 | + return items |
| 102 | + case _: |
| 103 | + logger.warning( |
| 104 | + "Unsupported diagnostic report type for file {}", |
| 105 | + file_path, |
| 106 | + ) |
| 107 | + return None |
0 commit comments