Skip to content

Commit 73cda66

Browse files
committed
feat(robot): unify variable handling and prepare for RF 7.3 type parsing
- Added support for parse_type parameter in Robot Framework 7.3+ - Unified variable processing across different RF versions
1 parent d8eac38 commit 73cda66

File tree

2,151 files changed

+19296
-8556
lines changed

Some content is hidden

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

2,151 files changed

+19296
-8556
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class VariableDefinition(SourceEntity):
187187

188188
value: Any = field(default=None, compare=False)
189189
value_is_native: bool = field(default=False, compare=False)
190+
value_type: Optional[str] = field(default=None, compare=False)
190191

191192
@functools.cached_property
192193
def matcher(self) -> VariableMatcher:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
from ..utils import get_robot_version
6868
from ..utils.ast import (
6969
cached_isinstance,
70-
get_variable_token,
70+
get_first_variable_token,
7171
range_from_token,
7272
strip_variable_token,
7373
)
@@ -2785,7 +2785,7 @@ def _get_argument_definitions_from_line(
27852785

27862786
for argument_token in (cast(RobotToken, e) for e in arguments):
27872787
try:
2788-
argument = get_variable_token(argument_token)
2788+
argument = get_first_variable_token(argument_token)
27892789

27902790
if argument is not None and argument.value != "@{}":
27912791
if argument.value not in args:

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

Lines changed: 223 additions & 192 deletions
Large diffs are not rendered by default.

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

Lines changed: 148 additions & 152 deletions
Large diffs are not rendered by default.

packages/robot/src/robotcode/robot/utils/ast.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,12 @@ def iter_over_keyword_names_and_owners(
326326
yield ".".join(tokens[:i]), ".".join(tokens[i:])
327327

328328

329-
def strip_variable_token(token: Token, identifiers: str = "$@&%*", matcher: Optional[VariableMatcher] = None) -> Token:
330-
if token.type == Token.VARIABLE:
329+
def strip_variable_token(
330+
token: Token, identifiers: str = "$@&%*", parse_type: bool = False, matcher: Optional[VariableMatcher] = None
331+
) -> Token:
332+
if token.type in [Token.VARIABLE, Token.ASSIGN]:
331333
if matcher is None:
332-
matcher = search_variable(token.value, identifiers, ignore_errors=True)
334+
matcher = search_variable(token.value, identifiers, parse_type=parse_type, ignore_errors=True)
333335

334336
if matcher.is_variable():
335337
value = matcher.base
@@ -346,13 +348,13 @@ def strip_variable_token(token: Token, identifiers: str = "$@&%*", matcher: Opti
346348
return token
347349

348350

349-
def get_variable_token(token: Token) -> Optional[Token]:
351+
def get_first_variable_token(token: Token, extra_types: Optional[Set[str]] = None) -> Optional[Token]:
350352
return next(
351353
(
352354
v
353355
for v in itertools.dropwhile(
354356
lambda t: t.type in Token.NON_DATA_TOKENS,
355-
tokenize_variables(token, ignore_errors=True),
357+
tokenize_variables(token, ignore_errors=True, extra_types=extra_types),
356358
)
357359
if v.type == Token.VARIABLE
358360
),

packages/robot/src/robotcode/robot/utils/variables.py

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,63 @@
88
from robot.variables.search import search_variable as robot_search_variable
99
from robotcode.robot.utils.match import normalize
1010

11+
from . import get_robot_version
12+
1113

1214
class InvalidVariableError(Exception):
1315
pass
1416

1517

1618
class VariableMatcher:
17-
def __init__(self, string: str, identifiers: str = "$@&%", ignore_errors: bool = True) -> None:
18-
self.string = string
19+
if get_robot_version() >= (7, 3):
20+
21+
def __init__(
22+
self, string: str, identifiers: str = "$@&%", parse_type: bool = False, ignore_errors: bool = True
23+
) -> None:
24+
self.string = string
25+
26+
self.match = robot_search_variable(
27+
string, identifiers=identifiers, parse_type=parse_type, ignore_errors=ignore_errors
28+
)
29+
30+
if not ignore_errors and self.match.base is None:
31+
raise InvalidVariableError(f"Invalid variable '{string}'")
32+
33+
self.base = self.match.base
34+
self.identifier = self.match.identifier
35+
self.name = "%s{%s}" % (self.identifier, self.base.strip()) if self.base else None
36+
self.type = self.match.type
37+
self.items = self.match.items
38+
self.start = self.match.start
39+
self.end = self.match.end
40+
self.after = self.match.after
41+
self.before = self.match.before
42+
43+
self.normalized_name = normalize(self.base) if self.base else None
44+
45+
else:
46+
47+
def __init__(
48+
self, string: str, identifiers: str = "$@&%", parse_type: bool = False, ignore_errors: bool = True
49+
) -> None:
50+
self.string = string
1951

20-
self.match = robot_search_variable(string, identifiers=identifiers, ignore_errors=ignore_errors)
52+
self.match = robot_search_variable(string, identifiers=identifiers, ignore_errors=ignore_errors)
2153

22-
if not ignore_errors and self.match.base is None:
23-
raise InvalidVariableError(f"Invalid variable '{string}'")
54+
if not ignore_errors and self.match.base is None:
55+
raise InvalidVariableError(f"Invalid variable '{string}'")
2456

25-
self.base = self.match.base
26-
self.identifier = self.match.identifier
27-
self.name = "%s{%s}" % (self.identifier, self.base.strip()) if self.base else None
28-
self.items = self.match.items
29-
self.start = self.match.start
30-
self.end = self.match.end
31-
self.after = self.match.after
32-
self.before = self.match.before
57+
self.base = self.match.base
58+
self.identifier = self.match.identifier
59+
self.name = "%s{%s}" % (self.identifier, self.base.strip()) if self.base else None
60+
self.type = None
61+
self.items = self.match.items
62+
self.start = self.match.start
63+
self.end = self.match.end
64+
self.after = self.match.after
65+
self.before = self.match.before
3366

34-
self.normalized_name = normalize(self.base) if self.base else None
67+
self.normalized_name = normalize(self.base) if self.base else None
3568

3669
def __eq__(self, o: object) -> bool:
3770
if self.normalized_name is None:
@@ -75,17 +108,57 @@ def is_list_variable(self) -> bool:
75108
def is_dict_variable(self) -> bool:
76109
return bool(self.match.is_dict_variable())
77110

78-
def is_assign(self, allow_assign_mark: bool = False) -> bool:
79-
return bool(self.match.is_assign(allow_assign_mark))
111+
if get_robot_version() >= (6, 1):
112+
113+
def is_assign(
114+
self, allow_assign_mark: bool = False, allow_nested: bool = False, allow_items: bool = False
115+
) -> bool:
116+
return bool(
117+
self.match.is_assign(
118+
allow_assign_mark=allow_assign_mark, allow_nested=allow_nested, allow_items=allow_items
119+
)
120+
)
121+
else:
122+
123+
def is_assign(
124+
self, allow_assign_mark: bool = False, allow_nested: bool = False, allow_items: bool = False
125+
) -> bool:
126+
return bool(self.match.is_assign(allow_assign_mark=allow_assign_mark))
127+
128+
if get_robot_version() >= (6, 1):
129+
130+
def is_scalar_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool:
131+
return bool(self.match.is_scalar_assign(allow_assign_mark=allow_assign_mark, allow_nested=allow_nested))
132+
else:
133+
134+
def is_scalar_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool:
135+
return bool(self.match.is_scalar_assign(allow_assign_mark=allow_assign_mark))
136+
137+
if get_robot_version() >= (6, 1):
138+
139+
def is_list_assign(
140+
self,
141+
allow_assign_mark: bool = False,
142+
allow_nested: bool = False,
143+
) -> bool:
144+
return bool(self.match.is_list_assign(allow_assign_mark=allow_assign_mark, allow_nested=allow_nested))
145+
else:
146+
147+
def is_list_assign(
148+
self,
149+
allow_assign_mark: bool = False,
150+
allow_nested: bool = False,
151+
) -> bool:
152+
return bool(self.match.is_list_assign(allow_assign_mark=allow_assign_mark))
80153

81-
def is_scalar_assign(self, allow_assign_mark: bool = False) -> bool:
82-
return bool(self.match.is_scalar_assign(allow_assign_mark))
154+
if get_robot_version() >= (6, 1):
83155

84-
def is_list_assign(self, allow_assign_mark: bool = False) -> bool:
85-
return bool(self.match.is_list_assign(allow_assign_mark))
156+
def is_dict_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool:
157+
return bool(self.match.is_dict_assign(allow_assign_mark=allow_assign_mark, allow_nested=allow_nested))
158+
else:
86159

87-
def is_dict_assign(self, allow_assign_mark: bool = False) -> bool:
88-
return bool(self.match.is_dict_assign(allow_assign_mark))
160+
def is_dict_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool:
161+
return bool(self.match.is_dict_assign(allow_assign_mark=allow_assign_mark))
89162

90163

91164
BUILTIN_VARIABLES = [
@@ -143,8 +216,10 @@ def is_variable(string: str, identifiers: str = "$@&") -> bool:
143216

144217

145218
@functools.lru_cache(maxsize=8192)
146-
def search_variable(string: str, identifiers: str = "$@&%*", ignore_errors: bool = False) -> VariableMatcher:
147-
return VariableMatcher(string, identifiers, ignore_errors)
219+
def search_variable(
220+
string: str, identifiers: str = "$@&%*", parse_type: bool = False, ignore_errors: bool = False
221+
) -> VariableMatcher:
222+
return VariableMatcher(string, identifiers, parse_type, ignore_errors)
148223

149224

150225
@functools.lru_cache(maxsize=8192)

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-007-020-var_in_variables_import].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 7
1414
target_range:
1515
end:
16-
character: 22
16+
character: 8
1717
line: 20
1818
start:
19-
character: 0
19+
character: 2
2020
line: 20
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-007-023-var_in_variables_import].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 7
1414
target_range:
1515
end:
16-
character: 22
16+
character: 8
1717
line: 20
1818
start:
19-
character: 0
19+
character: 2
2020
line: 20
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-007-025-var_in_variables_import].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 7
1414
target_range:
1515
end:
16-
character: 22
16+
character: 8
1717
line: 20
1818
start:
19-
character: 0
19+
character: 2
2020
line: 20
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-010-020-var_in_resource_import].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 10
1414
target_range:
1515
end:
16-
character: 22
16+
character: 8
1717
line: 20
1818
start:
19-
character: 0
19+
character: 2
2020
line: 20
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-010-023-var_in_resource_import].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 10
1414
target_range:
1515
end:
16-
character: 22
16+
character: 8
1717
line: 20
1818
start:
19-
character: 0
19+
character: 2
2020
line: 20
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-010-025-var_in_resource_import].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 10
1414
target_range:
1515
end:
16-
character: 22
16+
character: 8
1717
line: 20
1818
start:
19-
character: 0
19+
character: 2
2020
line: 20
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-014-020-var_in_Libary_import_path].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 14
1414
target_range:
1515
end:
16-
character: 11
16+
character: 5
1717
line: 25
1818
start:
19-
character: 0
19+
character: 2
2020
line: 25
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-014-021-var_in_Libary_import_path].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 14
1414
target_range:
1515
end:
16-
character: 11
16+
character: 5
1717
line: 25
1818
start:
19-
character: 0
19+
character: 2
2020
line: 25
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-014-022-var_in_Libary_import_path].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 14
1414
target_range:
1515
end:
16-
character: 11
16+
character: 5
1717
line: 25
1818
start:
19-
character: 0
19+
character: 2
2020
line: 25
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-014-057-var_in_library_parameters].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 14
1414
target_range:
1515
end:
16-
character: 22
16+
character: 9
1717
line: 23
1818
start:
19-
character: 0
19+
character: 2
2020
line: 23
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-014-060-var_in_library_parameters].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 14
1414
target_range:
1515
end:
16-
character: 22
16+
character: 9
1717
line: 23
1818
start:
19-
character: 0
19+
character: 2
2020
line: 23
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-014-063-var_in_library_parameters].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 14
1414
target_range:
1515
end:
16-
character: 22
16+
character: 9
1717
line: 23
1818
start:
19-
character: 0
19+
character: 2
2020
line: 23
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-023-002-Var_declaration].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 23
1414
target_range:
1515
end:
16-
character: 22
16+
character: 9
1717
line: 23
1818
start:
19-
character: 0
19+
character: 2
2020
line: 23
2121
target_selection_range:
2222
end:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_goto_definition.test_definition[goto.robot-023-005-Var_declaration].out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ result:
1313
line: 23
1414
target_range:
1515
end:
16-
character: 22
16+
character: 9
1717
line: 23
1818
start:
19-
character: 0
19+
character: 2
2020
line: 23
2121
target_selection_range:
2222
end:

0 commit comments

Comments
 (0)