From 1bad58fa16e10f0fe5b5f947b7fbea93b5497d8d Mon Sep 17 00:00:00 2001 From: Daniel Biehl Date: Wed, 9 Oct 2024 11:50:40 +0200 Subject: [PATCH] fix(langserver): stabilized resolving of variables in test case and keyword documentation --- .../robotframework/parts/hover.py | 19 +++++++++++----- .../robot/diagnostics/imports_manager.py | 2 ++ .../robot/diagnostics/library_doc.py | 22 ++++++++++++++----- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/packages/language_server/src/robotcode/language_server/robotframework/parts/hover.py b/packages/language_server/src/robotcode/language_server/robotframework/parts/hover.py index 4083b55f..eb044f86 100644 --- a/packages/language_server/src/robotcode/language_server/robotframework/parts/hover.py +++ b/packages/language_server/src/robotcode/language_server/robotframework/parts/hover.py @@ -184,13 +184,19 @@ def _hover_default(self, nodes: List[ast.AST], document: TextDocument, position: ) if found_range is not None: - txt = kw.to_markdown() + if kw.libtype == "RESOURCE": - txt = namespace.imports_manager.replace_variables_scalar( - txt, - str(document.uri.to_path().parent), - namespace.get_resolvable_variables(nodes, position), + txt = kw.to_markdown( + modify_doc_handler=lambda t: namespace.imports_manager.replace_variables_scalar( + t, + str(document.uri.to_path().parent), + namespace.get_resolvable_variables(), + ignore_errors=True, + ) ) + else: + txt = kw.to_markdown() + result.append((found_range, txt)) if result: r = result[0][0] @@ -271,7 +277,8 @@ def hover_TestCase( # noqa: N802 txt = namespace.imports_manager.replace_variables_scalar( txt, str(document.uri.to_path().parent), - namespace.get_resolvable_variables(nodes, position), + namespace.get_resolvable_variables(), + ignore_errors=True, ) return Hover( contents=MarkupContent(kind=MarkupKind.MARKDOWN, value=MarkDownFormatter().format(txt)), diff --git a/packages/robot/src/robotcode/robot/diagnostics/imports_manager.py b/packages/robot/src/robotcode/robot/diagnostics/imports_manager.py index 4ae6bfda..8f45d7db 100644 --- a/packages/robot/src/robotcode/robot/diagnostics/imports_manager.py +++ b/packages/robot/src/robotcode/robot/diagnostics/imports_manager.py @@ -1579,6 +1579,7 @@ def replace_variables_scalar( scalar: str, base_dir: str = ".", variables: Optional[Dict[str, Any]] = None, + ignore_errors: bool = False, ) -> Any: return replace_variables_scalar( scalar, @@ -1586,4 +1587,5 @@ def replace_variables_scalar( base_dir, self.get_resolvable_command_line_variables(), variables, + ignore_errors=ignore_errors, ) diff --git a/packages/robot/src/robotcode/robot/diagnostics/library_doc.py b/packages/robot/src/robotcode/robot/diagnostics/library_doc.py index 36a1f8fc..5cc177a5 100644 --- a/packages/robot/src/robotcode/robot/diagnostics/library_doc.py +++ b/packages/robot/src/robotcode/robot/diagnostics/library_doc.py @@ -711,6 +711,7 @@ def to_markdown( add_signature: bool = True, header_level: int = 2, add_type: bool = True, + modify_doc_handler: Optional[Callable[[str], str]] = None, ) -> str: result = "" @@ -723,12 +724,18 @@ def to_markdown( result += f"##{'#' * header_level} Documentation:\n" + doc: Optional[str] = None if self.doc_format == ROBOT_DOC_FORMAT: - result += MarkDownFormatter().format(self.doc) + doc = MarkDownFormatter().format(self.doc) elif self.doc_format == REST_DOC_FORMAT: - result += convert_from_rest(self.doc) + doc = convert_from_rest(self.doc) else: - result += self.doc + doc = self.doc + + if doc is not None: + if modify_doc_handler is not None: + doc = modify_doc_handler(doc) + result += doc return result @@ -1588,6 +1595,7 @@ def replace_variables_scalar( base_dir: str = ".", command_line_variables: Optional[Dict[str, Optional[Any]]] = None, variables: Optional[Dict[str, Optional[Any]]] = None, + ignore_errors: bool = False, ) -> Any: _update_env(working_dir) @@ -1595,9 +1603,13 @@ def replace_variables_scalar( if contains_variable(scalar, "$@&%"): robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables) if get_robot_version() >= (6, 1): - return VariableReplacer(robot_variables).replace_scalar(scalar.replace("\\", "\\\\")) + return VariableReplacer(robot_variables).replace_scalar( + scalar.replace("\\", "\\\\"), ignore_errors=ignore_errors + ) - return VariableReplacer(robot_variables.store).replace_scalar(scalar.replace("\\", "\\\\")) + return VariableReplacer(robot_variables.store).replace_scalar( + scalar.replace("\\", "\\\\"), ignore_errors=ignore_errors + ) return scalar.replace("\\", "\\\\")