Skip to content

Commit 0ca6170

Browse files
author
Leo
committed
feat(language-server): add python variable line for robot goto
Add parsing of the variables from python files to include their line for navigation from robot.
1 parent 2413bc3 commit 0ca6170

File tree

43 files changed

+241
-196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+241
-196
lines changed

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

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,42 @@ def find_variables(
22042204
)
22052205

22062206

2207+
def _extract_variable_line_numbers_from_python_source(source_path: str) -> Dict[str, Tuple[int, int, int]]:
2208+
variable_info: Dict[str, Tuple[int, int, int]] = {}
2209+
2210+
try:
2211+
with open(source_path, "r", encoding="utf-8") as f:
2212+
source_code = f.read()
2213+
2214+
tree = ast.parse(source_code, filename=source_path)
2215+
2216+
# Find all module-level assignments
2217+
for node in ast.walk(tree):
2218+
if isinstance(node, ast.Assign):
2219+
# Only consider module-level assignments (col_offset == 0)
2220+
if node.col_offset == 0 and node.lineno:
2221+
for target in node.targets:
2222+
if isinstance(target, ast.Name):
2223+
# Simple assignment: VAR = value
2224+
variable_info[target.id] = (node.lineno, target.col_offset, len(target.id))
2225+
elif isinstance(target, ast.Tuple):
2226+
# Tuple unpacking: a, b = values
2227+
for elt in target.elts:
2228+
if isinstance(elt, ast.Name):
2229+
variable_info[elt.id] = (node.lineno, elt.col_offset, len(elt.id))
2230+
elif isinstance(node, ast.AnnAssign):
2231+
# Annotated assignment: VAR: int = value
2232+
if node.col_offset == 0 and node.lineno:
2233+
if isinstance(node.target, ast.Name):
2234+
variable_info[node.target.id] = (node.lineno, node.target.col_offset, len(node.target.id))
2235+
except (OSError, SyntaxError):
2236+
# If we can't read or parse the file, return empty dict
2237+
# Variables will fall back to default line_no=1, col_offset=0
2238+
pass
2239+
2240+
return variable_info
2241+
2242+
22072243
def get_variables_doc(
22082244
name: str,
22092245
args: Optional[Tuple[Any, ...]] = None,
@@ -2385,24 +2421,33 @@ def _get_initial_handler(self, library: Any, name: Any, method: Any) -> Any:
23852421
]
23862422
)
23872423
try:
2424+
# Extract line numbers and column offsets from Python source for better go-to-definition
2425+
variable_info: Dict[str, Tuple[int, int, int]] = {}
2426+
if python_import and source:
2427+
variable_info = _extract_variable_line_numbers_from_python_source(source)
2428+
23882429
# TODO: add type information of the value including dict key names and member names
2389-
libdoc.variables = [
2390-
ImportedVariableDefinition(
2391-
line_no=1,
2392-
col_offset=0,
2393-
end_line_no=1,
2394-
end_col_offset=0,
2395-
source=source or (module_spec.origin if module_spec is not None else None) or "",
2396-
name=name if get_robot_version() < (7, 0) else f"${{{name}}}",
2397-
name_token=None,
2398-
value=(
2399-
NativeValue(value) if value is None or isinstance(value, (int, float, bool, str)) else None
2400-
),
2401-
has_value=value is None or isinstance(value, (int, float, bool, str)),
2402-
value_is_native=value is None or isinstance(value, (int, float, bool, str)),
2430+
libdoc.variables = []
2431+
for name, value in importer.import_variables(import_name, args):
2432+
var_info = variable_info.get(name, (1, 0, len(name)))
2433+
libdoc.variables.append(
2434+
ImportedVariableDefinition(
2435+
line_no=var_info[0],
2436+
col_offset=var_info[1],
2437+
end_line_no=var_info[0],
2438+
end_col_offset=var_info[1] + var_info[2],
2439+
source=source or (module_spec.origin if module_spec is not None else None) or "",
2440+
name=(name if get_robot_version() < (7, 0) else f"${{{name}}}"),
2441+
name_token=None,
2442+
value=(
2443+
NativeValue(value)
2444+
if value is None or isinstance(value, (int, float, bool, str))
2445+
else None
2446+
),
2447+
has_value=value is None or isinstance(value, (int, float, bool, str)),
2448+
value_is_native=value is None or isinstance(value, (int, float, bool, str)),
2449+
)
24032450
)
2404-
for name, value in importer.import_variables(import_name, args)
2405-
]
24062451
except (SystemExit, KeyboardInterrupt):
24072452
raise
24082453
except BaseException as e:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf50/test_goto_definition.test_definition[goto.robot-075-013-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf50/test_goto_definition.test_definition[goto.robot-075-020-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf50/test_goto_definition.test_definition[goto.robot-075-026-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf50/test_goto_implementation.test_implementation[goto.robot-075-013-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf50/test_goto_implementation.test_implementation[goto.robot-075-020-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf50/test_goto_implementation.test_implementation[goto.robot-075-026-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf60/test_goto_definition.test_definition[goto.robot-075-013-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf60/test_goto_definition.test_definition[goto.robot-075-020-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf60/test_goto_definition.test_definition[goto.robot-075-026-Imported_Variable].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ result:
1313
line: 75
1414
target_range:
1515
end:
16-
character: 0
16+
character: 17
1717
line: 0
1818
start:
1919
character: 0
2020
line: 0
2121
target_selection_range:
2222
end:
23-
character: 0
23+
character: 17
2424
line: 0
2525
start:
2626
character: 0

0 commit comments

Comments
 (0)