|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from asyncio import CancelledError |
| 4 | +from itertools import chain |
| 5 | +from typing import TYPE_CHECKING, Any, List, Optional, Union, cast |
| 6 | + |
| 7 | +from ....jsonrpc2.protocol import rpc_method |
| 8 | +from ....utils.async_tools import async_tasking_event, threaded |
| 9 | +from ....utils.logging import LoggingDescriptor |
| 10 | +from ..decorators import HasCodeActionKinds, language_id_filter |
| 11 | +from ..has_extend_capabilities import HasExtendCapabilities |
| 12 | +from ..lsp_types import ( |
| 13 | + CodeAction, |
| 14 | + CodeActionContext, |
| 15 | + CodeActionOptions, |
| 16 | + CodeActionParams, |
| 17 | + Command, |
| 18 | + Range, |
| 19 | + ServerCapabilities, |
| 20 | + TextDocumentIdentifier, |
| 21 | +) |
| 22 | +from ..text_document import TextDocument |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from ..protocol import LanguageServerProtocol |
| 26 | + |
| 27 | +from .protocol_part import LanguageServerProtocolPart |
| 28 | + |
| 29 | + |
| 30 | +class CodeActionProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities): |
| 31 | + |
| 32 | + _logger = LoggingDescriptor() |
| 33 | + |
| 34 | + def __init__(self, parent: LanguageServerProtocol) -> None: |
| 35 | + super().__init__(parent) |
| 36 | + |
| 37 | + @async_tasking_event |
| 38 | + async def collect( |
| 39 | + sender, document: TextDocument, range: Range, context: CodeActionContext # NOSONAR |
| 40 | + ) -> Optional[List[Union[Command, CodeAction]]]: |
| 41 | + ... |
| 42 | + |
| 43 | + @async_tasking_event |
| 44 | + async def resolve(sender, code_action: CodeAction) -> CodeAction: # NOSONAR |
| 45 | + ... |
| 46 | + |
| 47 | + def extend_capabilities(self, capabilities: ServerCapabilities) -> None: |
| 48 | + if len(self.collect): |
| 49 | + code_action_kinds = [ |
| 50 | + k |
| 51 | + for k in chain( |
| 52 | + *[ |
| 53 | + cast(HasCodeActionKinds, e).__code_action_kinds__ |
| 54 | + for e in self.collect |
| 55 | + if isinstance(e, HasCodeActionKinds) |
| 56 | + ] |
| 57 | + ) |
| 58 | + ] |
| 59 | + |
| 60 | + capabilities.code_action_provider = CodeActionOptions( |
| 61 | + code_action_kinds=code_action_kinds if code_action_kinds else None, |
| 62 | + resolve_provider=len(self.resolve) > 0, |
| 63 | + ) |
| 64 | + |
| 65 | + @rpc_method(name="textDocument/codeAction", param_type=CodeActionParams) |
| 66 | + @threaded() |
| 67 | + async def _text_document_code_action( |
| 68 | + self, |
| 69 | + text_document: TextDocumentIdentifier, |
| 70 | + range: Range, |
| 71 | + context: CodeActionContext, |
| 72 | + *args: Any, |
| 73 | + **kwargs: Any, |
| 74 | + ) -> Optional[List[Union[Command, CodeAction]]]: |
| 75 | + |
| 76 | + results: List[Union[Command, CodeAction]] = [] |
| 77 | + |
| 78 | + document = await self.parent.documents.get(text_document.uri) |
| 79 | + if document is None: |
| 80 | + return None |
| 81 | + |
| 82 | + for result in await self.collect( |
| 83 | + self, |
| 84 | + document, |
| 85 | + range, |
| 86 | + context, |
| 87 | + callback_filter=language_id_filter(document), |
| 88 | + ): |
| 89 | + if isinstance(result, BaseException): |
| 90 | + if not isinstance(result, CancelledError): |
| 91 | + self._logger.exception(result, exc_info=result) |
| 92 | + else: |
| 93 | + if result is not None: |
| 94 | + results.extend(result) |
| 95 | + |
| 96 | + if len(results) > 0: |
| 97 | + return results |
| 98 | + |
| 99 | + return None |
| 100 | + |
| 101 | + @rpc_method(name="textDocument/codeAction/resolve", param_type=CodeAction) |
| 102 | + @threaded() |
| 103 | + async def _text_document_code_action_resolve( |
| 104 | + self, |
| 105 | + params: CodeAction, |
| 106 | + *args: Any, |
| 107 | + **kwargs: Any, |
| 108 | + ) -> CodeAction: |
| 109 | + |
| 110 | + results: List[CodeAction] = [] |
| 111 | + |
| 112 | + for result in await self.resolve(self, params): |
| 113 | + if isinstance(result, BaseException): |
| 114 | + if not isinstance(result, CancelledError): |
| 115 | + self._logger.exception(result, exc_info=result) |
| 116 | + else: |
| 117 | + if result is not None: |
| 118 | + results.append(result) |
| 119 | + |
| 120 | + if len(results) > 0: |
| 121 | + if len(results) > 1: |
| 122 | + self._logger.warning("More then one resolve result. Use the last one.") |
| 123 | + |
| 124 | + return results[-1] |
| 125 | + |
| 126 | + return params |
0 commit comments