|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import TYPE_CHECKING, Any, List, Optional |
| 3 | + |
| 4 | +from robotcode.core.lsp.types import TextDocumentIdentifier |
| 5 | +from robotcode.core.utils.dataclasses import CamelSnakeMixin |
| 6 | +from robotcode.core.utils.logging import LoggingDescriptor |
| 7 | +from robotcode.jsonrpc2.protocol import rpc_method |
| 8 | +from robotcode.robot.diagnostics.model_helper import ModelHelper |
| 9 | + |
| 10 | +from .protocol_part import RobotLanguageServerProtocolPart |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from ..protocol import RobotLanguageServerProtocol |
| 14 | + |
| 15 | + |
| 16 | +@dataclass(repr=False) |
| 17 | +class GetDocumentImportsParams(CamelSnakeMixin): |
| 18 | + text_document: TextDocumentIdentifier |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(repr=False) |
| 22 | +class Keyword(CamelSnakeMixin): |
| 23 | + name: str |
| 24 | + id: Optional[str] |
| 25 | + signature: Optional[str] = None |
| 26 | + documentation: Optional[str] = None |
| 27 | + |
| 28 | + |
| 29 | +@dataclass(repr=False) |
| 30 | +class DocumentImport(CamelSnakeMixin): |
| 31 | + name: str |
| 32 | + alias: Optional[str] |
| 33 | + id: Optional[str] |
| 34 | + type: Optional[str] |
| 35 | + documentation: Optional[str] = None |
| 36 | + keywords: Optional[List[Keyword]] = None |
| 37 | + |
| 38 | + |
| 39 | +@dataclass(repr=False) |
| 40 | +class GetDocumentationUrl(CamelSnakeMixin): |
| 41 | + text_document: TextDocumentIdentifier |
| 42 | + import_id: Optional[str] = None |
| 43 | + keyword_id: Optional[str] = None |
| 44 | + |
| 45 | + |
| 46 | +class RobotKeywordsTreeViewPart(RobotLanguageServerProtocolPart, ModelHelper): |
| 47 | + _logger = LoggingDescriptor() |
| 48 | + |
| 49 | + def __init__(self, parent: "RobotLanguageServerProtocol") -> None: |
| 50 | + super().__init__(parent) |
| 51 | + |
| 52 | + @rpc_method(name="robot/keywordsview/getDocumentImports", param_type=GetDocumentImportsParams, threaded=True) |
| 53 | + @_logger.call |
| 54 | + async def _get_document_imports( |
| 55 | + self, |
| 56 | + text_document: TextDocumentIdentifier, |
| 57 | + *args: Any, |
| 58 | + **kwargs: Any, |
| 59 | + ) -> Optional[List[DocumentImport]]: |
| 60 | + document = self.parent.documents.get(text_document.uri) |
| 61 | + if document is None: |
| 62 | + return None |
| 63 | + |
| 64 | + namespace = self.parent.documents_cache.get_namespace(document) |
| 65 | + |
| 66 | + result = [] |
| 67 | + |
| 68 | + for _k, v in namespace.get_libraries().items(): |
| 69 | + result.append( |
| 70 | + DocumentImport( |
| 71 | + name=v.name, |
| 72 | + alias=v.alias, |
| 73 | + id=str(hash(v)), |
| 74 | + type="library", |
| 75 | + documentation=v.library_doc.to_markdown(add_signature=False), |
| 76 | + keywords=[ |
| 77 | + Keyword( |
| 78 | + l.name, |
| 79 | + str(hash(l)), |
| 80 | + l.parameter_signature(), |
| 81 | + l.to_markdown(add_signature=False), |
| 82 | + ) |
| 83 | + for l in v.library_doc.keywords.values() |
| 84 | + ], |
| 85 | + ) |
| 86 | + ) |
| 87 | + for _k, v in namespace.get_resources().items(): |
| 88 | + result.append( |
| 89 | + DocumentImport( |
| 90 | + name=v.name, |
| 91 | + alias=None, |
| 92 | + id=str(hash(v)), |
| 93 | + type="resource", |
| 94 | + documentation=v.library_doc.to_markdown(add_signature=False), |
| 95 | + keywords=[ |
| 96 | + Keyword(l.name, str(hash(l)), l.parameter_signature(), l.to_markdown(add_signature=False)) |
| 97 | + for l in v.library_doc.keywords.values() |
| 98 | + ], |
| 99 | + ) |
| 100 | + ) |
| 101 | + |
| 102 | + return result |
| 103 | + |
| 104 | + @rpc_method(name="robot/keywordsview/getDocumentKeywords", param_type=GetDocumentImportsParams, threaded=True) |
| 105 | + @_logger.call |
| 106 | + async def _get_document_keywords( |
| 107 | + self, |
| 108 | + text_document: TextDocumentIdentifier, |
| 109 | + *args: Any, |
| 110 | + **kwargs: Any, |
| 111 | + ) -> Optional[List[Keyword]]: |
| 112 | + document = self.parent.documents.get(text_document.uri) |
| 113 | + if document is None: |
| 114 | + return None |
| 115 | + |
| 116 | + namespace = self.parent.documents_cache.get_namespace(document) |
| 117 | + |
| 118 | + return [ |
| 119 | + Keyword(l.name, str(hash(l)), l.parameter_signature(), l.to_markdown(add_signature=False)) |
| 120 | + for l in namespace.get_library_doc().keywords.values() |
| 121 | + ] |
| 122 | + |
| 123 | + @rpc_method(name="robot/keywordsview/getDocumentationUrl", param_type=GetDocumentationUrl, threaded=True) |
| 124 | + @_logger.call |
| 125 | + async def _get_documentation_url( |
| 126 | + self, |
| 127 | + text_document: TextDocumentIdentifier, |
| 128 | + import_id: Optional[str] = None, |
| 129 | + keyword_id: Optional[str] = None, |
| 130 | + *args: Any, |
| 131 | + **kwargs: Any, |
| 132 | + ) -> Optional[str]: |
| 133 | + document = self.parent.documents.get(text_document.uri) |
| 134 | + if document is None: |
| 135 | + return None |
| 136 | + |
| 137 | + namespace = self.parent.documents_cache.get_namespace(document) |
| 138 | + |
| 139 | + keyword_name = None |
| 140 | + |
| 141 | + if import_id is None: |
| 142 | + if keyword_id is not None: |
| 143 | + keyword = next( |
| 144 | + (l for l in namespace.get_library_doc().keywords.values() if str(hash(l)) == keyword_id), None |
| 145 | + ) |
| 146 | + if keyword is not None: |
| 147 | + keyword_name = keyword.name |
| 148 | + |
| 149 | + return self.parent.robot_code_action_documentation.build_url( |
| 150 | + str(document.uri.to_path().name), (), document, namespace, keyword_name |
| 151 | + ) |
| 152 | + |
| 153 | + entry = next((l for l in namespace.get_libraries().values() if str(hash(l)) == import_id), None) |
| 154 | + if entry is None: |
| 155 | + entry = next((l for l in namespace.get_resources().values() if str(hash(l)) == import_id), None) |
| 156 | + |
| 157 | + if keyword_id and entry is not None: |
| 158 | + keyword = next((l for l in entry.library_doc.keywords.values() if str(hash(l)) == keyword_id), None) |
| 159 | + if keyword is not None: |
| 160 | + keyword_name = keyword.name |
| 161 | + |
| 162 | + if entry is not None: |
| 163 | + return self.parent.robot_code_action_documentation.build_url( |
| 164 | + entry.name, entry.args, document, namespace, keyword_name |
| 165 | + ) |
| 166 | + |
| 167 | + return None |
0 commit comments