|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from asyncio import CancelledError |
| 4 | +from typing import TYPE_CHECKING, Any, List, Optional |
| 5 | + |
| 6 | +from ....jsonrpc2.protocol import rpc_method |
| 7 | +from ....utils.async_tools import async_tasking_event, threaded |
| 8 | +from ....utils.logging import LoggingDescriptor |
| 9 | +from ..decorators import language_id_filter |
| 10 | +from ..has_extend_capabilities import HasExtendCapabilities |
| 11 | +from ..lsp_types import ( |
| 12 | + InlayHint, |
| 13 | + InlayHintOptions, |
| 14 | + InlayHintParams, |
| 15 | + Range, |
| 16 | + ServerCapabilities, |
| 17 | + TextDocumentIdentifier, |
| 18 | +) |
| 19 | +from ..text_document import TextDocument |
| 20 | + |
| 21 | +if TYPE_CHECKING: |
| 22 | + from ..protocol import LanguageServerProtocol |
| 23 | + |
| 24 | +from .protocol_part import LanguageServerProtocolPart |
| 25 | + |
| 26 | + |
| 27 | +class InlayHintProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities): |
| 28 | + |
| 29 | + _logger = LoggingDescriptor() |
| 30 | + |
| 31 | + def __init__(self, parent: LanguageServerProtocol) -> None: |
| 32 | + super().__init__(parent) |
| 33 | + |
| 34 | + @async_tasking_event |
| 35 | + async def collect(sender, document: TextDocument, range: Range) -> Optional[List[InlayHint]]: # NOSONAR |
| 36 | + ... |
| 37 | + |
| 38 | + @async_tasking_event |
| 39 | + async def resolve(sender, hint: InlayHint) -> Optional[InlayHint]: # NOSONAR |
| 40 | + ... |
| 41 | + |
| 42 | + def extend_capabilities(self, capabilities: ServerCapabilities) -> None: |
| 43 | + if len(self.collect): |
| 44 | + if len(self.resolve): |
| 45 | + capabilities.inlay_hint_provider = InlayHintOptions(resolve_provider=bool(len(self.resolve))) |
| 46 | + else: |
| 47 | + capabilities.inlay_hint_provider = InlayHintOptions() |
| 48 | + |
| 49 | + @rpc_method(name="textDocument/inlayHint", param_type=InlayHintParams) |
| 50 | + @threaded() |
| 51 | + async def _text_document_inlay_hint( |
| 52 | + self, |
| 53 | + text_document: TextDocumentIdentifier, |
| 54 | + range: Range, |
| 55 | + *args: Any, |
| 56 | + **kwargs: Any, |
| 57 | + ) -> Optional[List[InlayHint]]: |
| 58 | + |
| 59 | + results: List[InlayHint] = [] |
| 60 | + |
| 61 | + document = await self.parent.documents.get(text_document.uri) |
| 62 | + if document is None: |
| 63 | + return None |
| 64 | + |
| 65 | + for result in await self.collect( |
| 66 | + self, |
| 67 | + document, |
| 68 | + range, |
| 69 | + callback_filter=language_id_filter(document), |
| 70 | + ): |
| 71 | + if isinstance(result, BaseException): |
| 72 | + if not isinstance(result, CancelledError): |
| 73 | + self._logger.exception(result, exc_info=result) |
| 74 | + else: |
| 75 | + if result is not None: |
| 76 | + results.extend(result) |
| 77 | + |
| 78 | + if len(results) > 0: |
| 79 | + return results |
| 80 | + |
| 81 | + return None |
| 82 | + |
| 83 | + @rpc_method(name="inlayHint/resolve", param_type=InlayHint) |
| 84 | + @threaded() |
| 85 | + async def _inlay_hint_resolve( |
| 86 | + self, |
| 87 | + params: InlayHint, |
| 88 | + *args: Any, |
| 89 | + **kwargs: Any, |
| 90 | + ) -> Optional[InlayHint]: |
| 91 | + |
| 92 | + for result in await self.resolve(self, params): |
| 93 | + if isinstance(result, BaseException): |
| 94 | + if not isinstance(result, CancelledError): |
| 95 | + self._logger.exception(result, exc_info=result) |
| 96 | + else: |
| 97 | + if isinstance(result, InlayHint): |
| 98 | + return result |
| 99 | + |
| 100 | + return params |
| 101 | + |
| 102 | + async def refresh(self) -> None: |
| 103 | + if ( |
| 104 | + self.parent.client_capabilities is not None |
| 105 | + and self.parent.client_capabilities.workspace is not None |
| 106 | + and self.parent.client_capabilities.workspace.inlay_hint is not None |
| 107 | + and self.parent.client_capabilities.workspace.inlay_hint.refresh_support |
| 108 | + ): |
| 109 | + await self.parent.send_request_async("workspace/inlayHint/refresh") |
0 commit comments