|
| 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 |
| 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 | + Position, |
| 13 | + PrepareRenameParams, |
| 14 | + PrepareRenameResult, |
| 15 | + RenameOptions, |
| 16 | + RenameParams, |
| 17 | + ServerCapabilities, |
| 18 | + TextDocumentIdentifier, |
| 19 | + WorkspaceEdit, |
| 20 | +) |
| 21 | +from ..text_document import TextDocument |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from ..protocol import LanguageServerProtocol |
| 25 | + |
| 26 | +from .protocol_part import LanguageServerProtocolPart |
| 27 | + |
| 28 | + |
| 29 | +class RenameProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities): |
| 30 | + |
| 31 | + _logger = LoggingDescriptor() |
| 32 | + |
| 33 | + def __init__(self, parent: LanguageServerProtocol) -> None: |
| 34 | + super().__init__(parent) |
| 35 | + |
| 36 | + def extend_capabilities(self, capabilities: ServerCapabilities) -> None: |
| 37 | + if len(self.collect): |
| 38 | + capabilities.rename_provider = RenameOptions( |
| 39 | + prepare_provider=len(self.collect_prepare) > 0, work_done_progress=True |
| 40 | + ) |
| 41 | + |
| 42 | + @async_tasking_event |
| 43 | + async def collect( |
| 44 | + sender, document: TextDocument, position: Position, new_name: str # NOSONAR |
| 45 | + ) -> Optional[WorkspaceEdit]: |
| 46 | + ... |
| 47 | + |
| 48 | + @async_tasking_event |
| 49 | + async def collect_prepare( |
| 50 | + sender, document: TextDocument, position: Position # NOSONAR |
| 51 | + ) -> Optional[PrepareRenameResult]: |
| 52 | + ... |
| 53 | + |
| 54 | + @rpc_method(name="textDocument/rename", param_type=RenameParams) |
| 55 | + async def _text_document_rename( |
| 56 | + self, |
| 57 | + text_document: TextDocumentIdentifier, |
| 58 | + position: Position, |
| 59 | + new_name: str, |
| 60 | + *args: Any, |
| 61 | + **kwargs: Any, |
| 62 | + ) -> Optional[WorkspaceEdit]: |
| 63 | + |
| 64 | + edits: List[WorkspaceEdit] = [] |
| 65 | + |
| 66 | + document = await self.parent.documents.get(text_document.uri) |
| 67 | + if document is None: |
| 68 | + return None |
| 69 | + |
| 70 | + for result in await self.collect( |
| 71 | + self, document, position, new_name, callback_filter=language_id_filter(document) |
| 72 | + ): |
| 73 | + if isinstance(result, BaseException): |
| 74 | + if not isinstance(result, CancelledError): |
| 75 | + self._logger.exception(result, exc_info=result) |
| 76 | + else: |
| 77 | + if result is not None: |
| 78 | + edits.append(result) |
| 79 | + |
| 80 | + if len(edits) == 0: |
| 81 | + return None |
| 82 | + |
| 83 | + result = WorkspaceEdit() |
| 84 | + for we in edits: |
| 85 | + if we.changes: |
| 86 | + if result.changes is None: |
| 87 | + result.changes = {} |
| 88 | + result.changes.update(we.changes) |
| 89 | + |
| 90 | + if we.document_changes: |
| 91 | + if result.document_changes is None: |
| 92 | + result.document_changes = [] |
| 93 | + result.document_changes.extend(we.document_changes) |
| 94 | + |
| 95 | + if we.change_annotations: |
| 96 | + if result.change_annotations is None: |
| 97 | + result.change_annotations = {} |
| 98 | + result.change_annotations.update(we.change_annotations) |
| 99 | + |
| 100 | + return result |
| 101 | + |
| 102 | + @rpc_method(name="textDocument/prepareRename", param_type=PrepareRenameParams) |
| 103 | + async def _text_document_prepare_rename( |
| 104 | + self, |
| 105 | + text_document: TextDocumentIdentifier, |
| 106 | + position: Position, |
| 107 | + *args: Any, |
| 108 | + **kwargs: Any, |
| 109 | + ) -> Optional[PrepareRenameResult]: |
| 110 | + |
| 111 | + results: List[PrepareRenameResult] = [] |
| 112 | + |
| 113 | + document = await self.parent.documents.get(text_document.uri) |
| 114 | + if document is None: |
| 115 | + return None |
| 116 | + |
| 117 | + for result in await self.collect_prepare( |
| 118 | + self, document, position, callback_filter=language_id_filter(document) |
| 119 | + ): |
| 120 | + if isinstance(result, BaseException): |
| 121 | + if not isinstance(result, CancelledError): |
| 122 | + self._logger.exception(result, exc_info=result) |
| 123 | + else: |
| 124 | + if result is not None: |
| 125 | + results.append(result) |
| 126 | + |
| 127 | + if len(results) == 0: |
| 128 | + return None |
| 129 | + |
| 130 | + return results[-1] |
0 commit comments