|
| 1 | +from concurrent.futures import CancelledError |
| 2 | +from typing import ( |
| 3 | + TYPE_CHECKING, |
| 4 | + Any, |
| 5 | + Callable, |
| 6 | + Final, |
| 7 | + Iterable, |
| 8 | + List, |
| 9 | + Optional, |
| 10 | + Protocol, |
| 11 | + TypeVar, |
| 12 | + Union, |
| 13 | + cast, |
| 14 | + runtime_checkable, |
| 15 | +) |
| 16 | + |
| 17 | +from robotcode.core.event import event |
| 18 | +from robotcode.core.lsp.types import ( |
| 19 | + Location, |
| 20 | + ServerCapabilities, |
| 21 | + SymbolInformation, |
| 22 | + WorkspaceSymbol, |
| 23 | + WorkspaceSymbolClientCapabilitiesResolveSupportType, |
| 24 | + WorkspaceSymbolClientCapabilitiesSymbolKindType, |
| 25 | + WorkspaceSymbolClientCapabilitiesTagSupportType, |
| 26 | + WorkspaceSymbolParams, |
| 27 | +) |
| 28 | +from robotcode.core.utils.logging import LoggingDescriptor |
| 29 | +from robotcode.jsonrpc2.protocol import rpc_method |
| 30 | +from robotcode.language_server.common.parts.protocol_part import ( |
| 31 | + LanguageServerProtocolPart, |
| 32 | +) |
| 33 | + |
| 34 | +if TYPE_CHECKING: |
| 35 | + from robotcode.language_server.common.protocol import LanguageServerProtocol |
| 36 | + |
| 37 | + |
| 38 | +@runtime_checkable |
| 39 | +class HasSymbolInformationLabel(Protocol): |
| 40 | + symbol_information_label: str |
| 41 | + |
| 42 | + |
| 43 | +_F = TypeVar("_F", bound=Callable[..., Any]) |
| 44 | + |
| 45 | + |
| 46 | +def symbol_information_label(label: str) -> Callable[[_F], _F]: |
| 47 | + def decorator(func: _F) -> _F: |
| 48 | + setattr(func, "symbol_information_label", label) |
| 49 | + return func |
| 50 | + |
| 51 | + return decorator |
| 52 | + |
| 53 | + |
| 54 | +class WorkspaceSymbolsProtocolPart(LanguageServerProtocolPart): |
| 55 | + _logger: Final = LoggingDescriptor() |
| 56 | + |
| 57 | + def __init__(self, parent: "LanguageServerProtocol") -> None: |
| 58 | + super().__init__(parent) |
| 59 | + self.symbol_kind: Optional[WorkspaceSymbolClientCapabilitiesSymbolKindType] = None |
| 60 | + self.tag_support: Optional[WorkspaceSymbolClientCapabilitiesTagSupportType] = None |
| 61 | + self.resolve_support: Optional[WorkspaceSymbolClientCapabilitiesResolveSupportType] = None |
| 62 | + |
| 63 | + @event |
| 64 | + def collect( |
| 65 | + sender, |
| 66 | + query: str, |
| 67 | + ) -> Optional[Union[List[WorkspaceSymbol], List[SymbolInformation], None]]: ... |
| 68 | + |
| 69 | + def extend_capabilities(self, capabilities: ServerCapabilities) -> None: |
| 70 | + if ( |
| 71 | + self.parent.client_capabilities |
| 72 | + and self.parent.client_capabilities.workspace |
| 73 | + and self.parent.client_capabilities.workspace.symbol is not None |
| 74 | + ): |
| 75 | + workspace_symbol = self.parent.client_capabilities.workspace.symbol |
| 76 | + |
| 77 | + self.symbol_kind = workspace_symbol.symbol_kind |
| 78 | + self.tag_support = workspace_symbol.tag_support |
| 79 | + self.resolve_support = workspace_symbol.resolve_support |
| 80 | + |
| 81 | + if len(self.collect): |
| 82 | + # TODO: Implement workspace resolve |
| 83 | + capabilities.workspace_symbol_provider = True |
| 84 | + |
| 85 | + @rpc_method(name="workspace/symbol", param_type=WorkspaceSymbolParams, threaded=True) |
| 86 | + def _workspace_symbol( |
| 87 | + self, query: str, *args: Any, **kwargs: Any |
| 88 | + ) -> Optional[Union[List[WorkspaceSymbol], List[SymbolInformation], None]]: |
| 89 | + workspace_symbols: List[WorkspaceSymbol] = [] |
| 90 | + symbol_informations: List[SymbolInformation] = [] |
| 91 | + |
| 92 | + for result in self.collect(self, query): |
| 93 | + if isinstance(result, BaseException): |
| 94 | + if not isinstance(result, CancelledError): |
| 95 | + self._logger.exception(result, exc_info=result) |
| 96 | + else: |
| 97 | + if result is not None: |
| 98 | + if all(isinstance(e, WorkspaceSymbol) for e in result): |
| 99 | + workspace_symbols.extend(cast(Iterable[WorkspaceSymbol], result)) |
| 100 | + elif all(isinstance(e, SymbolInformation) for e in result): |
| 101 | + symbol_informations.extend(cast(Iterable[SymbolInformation], result)) |
| 102 | + else: |
| 103 | + self._logger.warning( |
| 104 | + "Result contains WorkspaceSymbol and SymbolInformation results, result is skipped." |
| 105 | + ) |
| 106 | + |
| 107 | + if workspace_symbols: |
| 108 | + for symbol in workspace_symbols: |
| 109 | + if isinstance(symbol.location, Location): |
| 110 | + doc = self.parent.documents.get(symbol.location.uri) |
| 111 | + if doc is not None: |
| 112 | + symbol.location.range = doc.range_to_utf16(symbol.location.range) |
| 113 | + |
| 114 | + if symbol_informations: |
| 115 | + for symbol_information in symbol_informations: |
| 116 | + doc = self.parent.documents.get(symbol_information.location.uri) |
| 117 | + if doc is not None: |
| 118 | + symbol_information.location.range = doc.range_to_utf16(symbol_information.location.range) |
| 119 | + |
| 120 | + if workspace_symbols and symbol_informations: |
| 121 | + self._logger.warning( |
| 122 | + "Result contains WorksapceSymbol and SymbolInformation results, only WorkspaceSymbols returned." |
| 123 | + ) |
| 124 | + return workspace_symbols |
| 125 | + |
| 126 | + if workspace_symbols: |
| 127 | + return workspace_symbols |
| 128 | + |
| 129 | + if symbol_informations: |
| 130 | + return symbol_informations |
| 131 | + |
| 132 | + return None |
0 commit comments