Skip to content

Commit c8b66c8

Browse files
committed
fix(langserver): hovering over EMPTY builtin variable respects the type and shows correct empty values
1 parent 318d780 commit c8b66c8

File tree

2 files changed

+34
-16
lines changed
  • packages
    • language_server/src/robotcode/language_server/robotframework/parts
    • robot/src/robotcode/robot/diagnostics

2 files changed

+34
-16
lines changed

packages/language_server/src/robotcode/language_server/robotframework/parts/hover.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
)
2828
from robotcode.core.text_document import TextDocument
2929
from robotcode.core.utils.logging import TRACE, LoggingDescriptor
30+
from robotcode.robot.diagnostics.entities import VariableDefinitionType
3031
from robotcode.robot.diagnostics.model_helper import ModelHelper
3132
from robotcode.robot.utils.ast import (
3233
get_nodes_at_position,
@@ -117,29 +118,43 @@ def _hover_default(self, nodes: List[ast.AST], document: TextDocument, position:
117118
if found_range is not None:
118119
highlight_range = found_range
119120
if variable.has_value or variable.resolvable:
120-
try:
121-
value = reprlib.repr(
122-
namespace.imports_manager.resolve_variable(
123-
variable.name,
124-
str(document.uri.to_path().parent),
125-
namespace.get_resolvable_variables(nodes, position),
126-
)
127-
)
128-
except (
129-
asyncio.CancelledError,
130-
SystemExit,
131-
KeyboardInterrupt,
121+
if (
122+
variable.type == VariableDefinitionType.BUILTIN_VARIABLE
123+
and variable.matcher.normalized_name == "empty"
132124
):
133-
raise
134-
except BaseException:
135-
self._logger.exception("Error resolving variable: {e}", level=TRACE)
136-
value = ""
125+
line = document.get_lines()[found_range.start.line]
126+
prefix = line[found_range.start.character - 2 : found_range.start.character - 1]
127+
if prefix == "$":
128+
value = "''"
129+
elif prefix == "&":
130+
value = "{}"
131+
elif prefix == "@":
132+
value = "[]"
133+
else:
134+
try:
135+
value = reprlib.repr(
136+
namespace.imports_manager.resolve_variable(
137+
variable.name,
138+
str(document.uri.to_path().parent),
139+
namespace.get_resolvable_variables(nodes, position),
140+
)
141+
)
142+
except (
143+
asyncio.CancelledError,
144+
SystemExit,
145+
KeyboardInterrupt,
146+
):
147+
raise
148+
except BaseException:
149+
self._logger.exception("Error resolving variable: {e}", level=TRACE)
150+
value = ""
137151
else:
138152
value = ""
139153
if text is None:
140154
text = ""
141155
if text:
142156
text += "\n"
157+
143158
text += f"| ({variable.type.value}) | {variable.name} | {f' `{value}`' if value else ''} |"
144159

145160
if text:

packages/robot/src/robotcode/robot/diagnostics/entities.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ def __eq__(self, o: object) -> bool:
155155
if isinstance(o, str):
156156
match = search_variable(o, "$@&%", ignore_errors=True)
157157
base = match.base
158+
if base is None:
159+
return False
160+
158161
normalized = str(normalize(base))
159162
return self.normalized_name == normalized
160163

0 commit comments

Comments
 (0)