robotcode-robot 2.6.2__py3-none-any.whl → 2.8.0.dev548__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- robotcode/robot/__version__.py +1 -1
- robotcode/robot/config/loader.py +18 -5
- robotcode/robot/config/model.py +258 -61
- robotcode/robot/diagnostics/analyzer_result.py +1 -0
- robotcode/robot/diagnostics/data_cache.py +12 -1
- robotcode/robot/diagnostics/diagnostic_rules.py +75 -0
- robotcode/robot/diagnostics/diagnostics_modifier.py +83 -43
- robotcode/robot/diagnostics/document_cache_helper.py +158 -17
- robotcode/robot/diagnostics/errors.py +7 -0
- robotcode/robot/diagnostics/import_resolver.py +178 -12
- robotcode/robot/diagnostics/imports_manager.py +556 -220
- robotcode/robot/diagnostics/library_doc.py +874 -162
- robotcode/robot/diagnostics/namespace.py +84 -18
- robotcode/robot/diagnostics/namespace_analyzer.py +59 -16
- robotcode/robot/diagnostics/project_index.py +88 -13
- robotcode/robot/diagnostics/scope_tree.py +2 -2
- robotcode/robot/diagnostics/semantic_analyzer/analyzer.py +790 -201
- robotcode/robot/diagnostics/semantic_analyzer/enums.py +39 -2
- robotcode/robot/diagnostics/semantic_analyzer/json_dump.py +364 -0
- robotcode/robot/diagnostics/semantic_analyzer/model.py +72 -11
- robotcode/robot/diagnostics/semantic_analyzer/nodes.py +7 -1
- robotcode/robot/diagnostics/semantic_analyzer/variable_tokenizer.py +218 -112
- robotcode/robot/diagnostics/workspace_config.py +3 -0
- robotcode/robot/utils/markdown_docs.py +733 -0
- robotcode/robot/utils/markdownformatter.py +57 -12
- robotcode/robot/utils/match.py +13 -0
- robotcode/robot/utils/robot_patching.py +4 -0
- robotcode/robot/utils/variables.py +3 -0
- {robotcode_robot-2.6.2.dist-info → robotcode_robot-2.8.0.dev548.dist-info}/METADATA +4 -3
- robotcode_robot-2.8.0.dev548.dist-info/RECORD +48 -0
- {robotcode_robot-2.6.2.dist-info → robotcode_robot-2.8.0.dev548.dist-info}/WHEEL +1 -1
- robotcode_robot-2.6.2.dist-info/RECORD +0 -46
|
@@ -77,7 +77,7 @@ class NodeKind(Enum):
|
|
|
77
77
|
SETTING_FORCE_TAGS = "setting_force_tags" # Force Tags (deprecated)
|
|
78
78
|
SETTING_TEST_TAGS = "setting_test_tags" # Test Tags
|
|
79
79
|
SETTING_DOCUMENTATION = "setting_documentation" # [Documentation] / Documentation
|
|
80
|
-
SETTING_METADATA = "setting_metadata" # Metadata
|
|
80
|
+
SETTING_METADATA = "setting_metadata" # Metadata (suite-level) / [Metadata] (test-level, RF 7.5+)
|
|
81
81
|
SETTING_TIMEOUT = "setting_timeout" # [Timeout] / Test Timeout
|
|
82
82
|
SETTING_ARGUMENTS = "setting_arguments" # [Arguments]
|
|
83
83
|
SETTING_SUITE_NAME = "setting_suite_name" # Name (RF 7.0+)
|
|
@@ -93,10 +93,16 @@ class NodeKind(Enum):
|
|
|
93
93
|
|
|
94
94
|
|
|
95
95
|
class TokenKind(Enum):
|
|
96
|
-
"""What this token represents — already resolved.
|
|
96
|
+
"""What this token represents — already resolved.
|
|
97
|
+
|
|
98
|
+
Kinds are fine-grained enough that rendering is a static table lookup:
|
|
99
|
+
consumers never need Robot Framework version checks, token-value parsing,
|
|
100
|
+
or statement-type inspection to decide what a token is.
|
|
101
|
+
"""
|
|
97
102
|
|
|
98
103
|
# Keyword-related
|
|
99
104
|
KEYWORD = "keyword"
|
|
105
|
+
KEYWORD_INNER = "keyword_inner" # inner keyword name of a Run Keyword variant
|
|
100
106
|
BDD_PREFIX = "bdd_prefix"
|
|
101
107
|
NAMESPACE = "namespace"
|
|
102
108
|
|
|
@@ -137,10 +143,18 @@ class TokenKind(Enum):
|
|
|
137
143
|
ARGUMENT = "argument"
|
|
138
144
|
NAMED_ARGUMENT_NAME = "named_argument_name"
|
|
139
145
|
NAMED_ARGUMENT_VALUE = "named_argument_value"
|
|
146
|
+
PARAMETER = "parameter" # [Arguments] definition with a default value
|
|
140
147
|
|
|
141
148
|
# Control flow
|
|
142
149
|
CONTROL_FLOW = "control_flow"
|
|
143
150
|
CONDITION = "condition"
|
|
151
|
+
FOR_SEPARATOR = "for_separator" # IN / IN RANGE / IN ENUMERATE / IN ZIP
|
|
152
|
+
VAR_MARKER = "var_marker" # the VAR word (RF 7.0+)
|
|
153
|
+
|
|
154
|
+
# Options (name=value cells on VAR / FOR / WHILE / EXCEPT)
|
|
155
|
+
OPTION = "option" # whole name=value option (VAR / FOR)
|
|
156
|
+
OPTION_NAME = "option_name" # option name half (WHILE / EXCEPT)
|
|
157
|
+
OPTION_VALUE = "option_value" # option value half (WHILE / EXCEPT)
|
|
144
158
|
|
|
145
159
|
# Definitions
|
|
146
160
|
TEST_NAME = "test_name"
|
|
@@ -149,16 +163,39 @@ class TokenKind(Enum):
|
|
|
149
163
|
|
|
150
164
|
# Structure
|
|
151
165
|
SETTING_NAME = "setting_name"
|
|
166
|
+
SETTING_IMPORT = "setting_import" # Library / Resource / Variables, WITH NAME / AS
|
|
152
167
|
IMPORT_NAME = "import_name"
|
|
168
|
+
OPERATOR = "operator" # "." in Namespace.Keyword, "=" in named args, "[" / "]"
|
|
153
169
|
HEADER = "header"
|
|
170
|
+
HEADER_SETTINGS = "header_settings"
|
|
171
|
+
HEADER_VARIABLE = "header_variable"
|
|
172
|
+
HEADER_TESTCASE = "header_testcase"
|
|
173
|
+
HEADER_TASK = "header_task"
|
|
174
|
+
HEADER_KEYWORD = "header_keyword"
|
|
175
|
+
HEADER_COMMENT = "header_comment"
|
|
154
176
|
SEPARATOR = "separator"
|
|
155
177
|
CONTINUATION = "continuation"
|
|
178
|
+
EOL = "eol" # line break incl. preceding trailing whitespace — layout only, never rendered
|
|
156
179
|
COMMENT = "comment"
|
|
157
180
|
TAG = "tag"
|
|
158
181
|
CONFIG = "config"
|
|
159
182
|
ERROR = "error"
|
|
160
183
|
|
|
161
184
|
|
|
185
|
+
class TokenModifier(Enum):
|
|
186
|
+
"""Pre-computed semantic modifiers carried on a SemanticToken.
|
|
187
|
+
|
|
188
|
+
Filled by the SemanticAnalyzer from data it already holds (keyword docs,
|
|
189
|
+
embedded-argument matches, statement kind) — consumers map them to their
|
|
190
|
+
output format without any re-resolution.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
BUILTIN = "builtin"
|
|
194
|
+
EMBEDDED = "embedded"
|
|
195
|
+
DECLARATION = "declaration"
|
|
196
|
+
DOCUMENTATION = "documentation"
|
|
197
|
+
|
|
198
|
+
|
|
162
199
|
class ForFlavor(Enum):
|
|
163
200
|
"""FOR loop variant."""
|
|
164
201
|
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
"""Read-only JSON serialization of a built `SemanticModel`.
|
|
2
|
+
|
|
3
|
+
Developer/diagnostic surface: `model_to_dict` converts the complete model —
|
|
4
|
+
block tree, flat statement list with full token trees, file scope, and
|
|
5
|
+
per-definition local scopes — into a JSON-compatible dict for manual
|
|
6
|
+
inspection and diffing. The output is deterministic (stable ordering,
|
|
7
|
+
workspace-relative sources, no object identities) and is NOT designed to be
|
|
8
|
+
deserialized back into a model. The format carries no stability guarantee.
|
|
9
|
+
|
|
10
|
+
The walkers are explicit per class instead of `dataclasses.asdict` because
|
|
11
|
+
`parent` back-pointers create cycles, resolved references must become compact
|
|
12
|
+
stubs, and enums serialize as names. `_SERIALIZED_FIELDS` / `_SKIPPED_FIELDS`
|
|
13
|
+
document that contract per class; a drift-guard test compares them against
|
|
14
|
+
`dataclasses.fields()` so new node fields cannot be forgotten silently.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import TYPE_CHECKING, Any, Callable, Dict, FrozenSet, List, Optional, Tuple
|
|
19
|
+
|
|
20
|
+
from .model import SemanticModel
|
|
21
|
+
from .nodes import (
|
|
22
|
+
DefinitionBlock,
|
|
23
|
+
DefinitionStatement,
|
|
24
|
+
ExceptStatement,
|
|
25
|
+
ForBlock,
|
|
26
|
+
ForStatement,
|
|
27
|
+
GroupBlock,
|
|
28
|
+
IfBlock,
|
|
29
|
+
IfStatement,
|
|
30
|
+
ImportStatement,
|
|
31
|
+
InlineIfStatement,
|
|
32
|
+
KeywordCallStatement,
|
|
33
|
+
ReturnStatement,
|
|
34
|
+
RunKeywordCallStatement,
|
|
35
|
+
SemanticBlock,
|
|
36
|
+
SemanticNode,
|
|
37
|
+
SemanticStatement,
|
|
38
|
+
SemanticToken,
|
|
39
|
+
SettingStatement,
|
|
40
|
+
TemplateDataStatement,
|
|
41
|
+
TryBlock,
|
|
42
|
+
VarStatement,
|
|
43
|
+
WhileBlock,
|
|
44
|
+
WhileStatement,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if TYPE_CHECKING:
|
|
48
|
+
from ..entities import LibraryEntry, VariableDefinition
|
|
49
|
+
from ..library_doc import ArgumentSpec, KeywordDoc
|
|
50
|
+
|
|
51
|
+
_RelFn = Callable[[Optional[str]], Optional[str]]
|
|
52
|
+
|
|
53
|
+
# Per-class contract of the dump format: which dataclass fields (declared on
|
|
54
|
+
# that class itself, not inherited) appear in the output and which are
|
|
55
|
+
# intentionally left out. Checked against `dataclasses.fields()` by the
|
|
56
|
+
# drift-guard test.
|
|
57
|
+
_SERIALIZED_FIELDS: Dict[type, FrozenSet[str]] = {
|
|
58
|
+
SemanticToken: frozenset({"kind", "value", "line", "col_offset", "length", "sub_tokens", "modifiers"}),
|
|
59
|
+
SemanticNode: frozenset({"kind", "line_start", "line_end"}),
|
|
60
|
+
SemanticStatement: frozenset({"tokens"}),
|
|
61
|
+
KeywordCallStatement: frozenset({"keyword_doc", "lib_entry", "assign_variables"}),
|
|
62
|
+
RunKeywordCallStatement: frozenset({"inner_calls"}),
|
|
63
|
+
ForStatement: frozenset(),
|
|
64
|
+
WhileStatement: frozenset(),
|
|
65
|
+
IfStatement: frozenset(),
|
|
66
|
+
InlineIfStatement: frozenset({"condition", "assign_variable"}),
|
|
67
|
+
ExceptStatement: frozenset(),
|
|
68
|
+
VarStatement: frozenset({"variable_name", "scope", "separator", "values"}),
|
|
69
|
+
ReturnStatement: frozenset({"return_values"}),
|
|
70
|
+
ImportStatement: frozenset({"import_type", "import_name", "alias", "arguments", "lib_entry", "init_keyword_doc"}),
|
|
71
|
+
SettingStatement: frozenset({"setting_name", "argument_definitions", "tag_values"}),
|
|
72
|
+
DefinitionStatement: frozenset({"name", "arguments_spec", "return_type", "tags", "local_variables"}),
|
|
73
|
+
TemplateDataStatement: frozenset({"template_keyword_doc"}),
|
|
74
|
+
SemanticBlock: frozenset({"header", "body"}),
|
|
75
|
+
# `local_variables` of DefinitionBlock appears in the top-level
|
|
76
|
+
# `local_scopes` section instead of inline in the tree.
|
|
77
|
+
DefinitionBlock: frozenset({"name", "arguments_spec", "return_type", "tags", "local_variables"}),
|
|
78
|
+
ForBlock: frozenset({"flavor", "loop_variables", "start", "mode", "fill"}),
|
|
79
|
+
WhileBlock: frozenset({"condition", "limit", "on_limit", "on_limit_message"}),
|
|
80
|
+
IfBlock: frozenset({"condition"}),
|
|
81
|
+
TryBlock: frozenset(),
|
|
82
|
+
GroupBlock: frozenset(),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
_SKIPPED_FIELDS: Dict[type, FrozenSet[str]] = {
|
|
86
|
+
# `range` is derived from line/col_offset/length in __post_init__.
|
|
87
|
+
SemanticToken: frozenset({"range"}),
|
|
88
|
+
# `parent` back-pointers create cycles; structure is implicit in nesting.
|
|
89
|
+
SemanticNode: frozenset({"parent"}),
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def model_to_dict(
|
|
94
|
+
model: SemanticModel,
|
|
95
|
+
workspace_root: Optional[Path] = None,
|
|
96
|
+
source: Optional[str] = None,
|
|
97
|
+
) -> Dict[str, Any]:
|
|
98
|
+
"""Serialize a built `SemanticModel` into a JSON-compatible dict.
|
|
99
|
+
|
|
100
|
+
Read-only: the model is not mutated. `workspace_root` controls source-path
|
|
101
|
+
relativization; sources outside it are rendered as `<external>/<basename>`.
|
|
102
|
+
`source` is the dumped file itself (relativized the same way).
|
|
103
|
+
"""
|
|
104
|
+
rel = _make_rel(workspace_root)
|
|
105
|
+
return {
|
|
106
|
+
"source": rel(source),
|
|
107
|
+
"tree": _block(model.root, rel) if model.root is not None else None,
|
|
108
|
+
"statements": [_statement(stmt, rel) for stmt in model.statements],
|
|
109
|
+
"file_scope": _file_scope(model, rel),
|
|
110
|
+
"local_scopes": _local_scopes(model, rel),
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _make_rel(workspace_root: Optional[Path]) -> _RelFn:
|
|
115
|
+
resolved_root = workspace_root.resolve() if workspace_root is not None else None
|
|
116
|
+
|
|
117
|
+
def rel(source: Optional[str]) -> Optional[str]:
|
|
118
|
+
if source is None:
|
|
119
|
+
return None
|
|
120
|
+
path = Path(source)
|
|
121
|
+
if resolved_root is not None:
|
|
122
|
+
try:
|
|
123
|
+
return path.resolve().relative_to(resolved_root).as_posix()
|
|
124
|
+
except ValueError:
|
|
125
|
+
pass
|
|
126
|
+
return f"<external>/{path.name}"
|
|
127
|
+
|
|
128
|
+
return rel
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# --- Tokens ---
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _token(token: SemanticToken) -> Dict[str, Any]:
|
|
135
|
+
result: Dict[str, Any] = {
|
|
136
|
+
"kind": token.kind.name,
|
|
137
|
+
"value": token.value,
|
|
138
|
+
"line": token.line,
|
|
139
|
+
"col_offset": token.col_offset,
|
|
140
|
+
"length": token.length,
|
|
141
|
+
}
|
|
142
|
+
if token.modifiers:
|
|
143
|
+
result["modifiers"] = sorted(modifier.name for modifier in token.modifiers)
|
|
144
|
+
if token.sub_tokens:
|
|
145
|
+
result["sub_tokens"] = [_token(sub) for sub in token.sub_tokens]
|
|
146
|
+
return result
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _tokens(tokens: List[SemanticToken]) -> List[Dict[str, Any]]:
|
|
150
|
+
return [_token(token) for token in tokens]
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
# --- Reference stubs ---
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _range_str(range_: Any) -> str:
|
|
157
|
+
return f"{range_.start.line}:{range_.start.character}-{range_.end.line}:{range_.end.character}"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _variable_stub(var: "VariableDefinition", rel: _RelFn) -> Dict[str, Any]:
|
|
161
|
+
return {
|
|
162
|
+
"class": type(var).__name__,
|
|
163
|
+
"name": var.name,
|
|
164
|
+
"type": var.type.name,
|
|
165
|
+
"range": _range_str(var.range),
|
|
166
|
+
"source": rel(var.source),
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _keyword_stub(keyword_doc: Optional["KeywordDoc"], rel: _RelFn) -> Optional[Dict[str, Any]]:
|
|
171
|
+
if keyword_doc is None:
|
|
172
|
+
return None
|
|
173
|
+
return {
|
|
174
|
+
"name": keyword_doc.name,
|
|
175
|
+
"source": rel(keyword_doc.source),
|
|
176
|
+
"line": keyword_doc.line_no,
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _lib_entry_stub(entry: Optional["LibraryEntry"], rel: _RelFn) -> Optional[Dict[str, Any]]:
|
|
181
|
+
if entry is None:
|
|
182
|
+
return None
|
|
183
|
+
return {
|
|
184
|
+
"name": entry.name,
|
|
185
|
+
"alias": entry.alias,
|
|
186
|
+
"source": rel(entry.library_doc.source),
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _jsonable(value: Any) -> Any:
|
|
191
|
+
if value is None or isinstance(value, (bool, int, float, str)):
|
|
192
|
+
return value
|
|
193
|
+
if isinstance(value, (list, tuple)):
|
|
194
|
+
return [_jsonable(item) for item in value]
|
|
195
|
+
if isinstance(value, dict):
|
|
196
|
+
return {str(key): _jsonable(val) for key, val in value.items()}
|
|
197
|
+
return str(value)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _argument_spec(spec: Optional["ArgumentSpec"]) -> Optional[Dict[str, Any]]:
|
|
201
|
+
if spec is None:
|
|
202
|
+
return None
|
|
203
|
+
return {
|
|
204
|
+
"name": spec.name,
|
|
205
|
+
"positional_only": list(spec.positional_only),
|
|
206
|
+
"positional_or_named": list(spec.positional_or_named),
|
|
207
|
+
"var_positional": _jsonable(spec.var_positional),
|
|
208
|
+
"named_only": _jsonable(spec.named_only),
|
|
209
|
+
"var_named": _jsonable(spec.var_named),
|
|
210
|
+
"embedded": _jsonable(spec.embedded),
|
|
211
|
+
"defaults": _jsonable(spec.defaults),
|
|
212
|
+
"types": spec.types,
|
|
213
|
+
"return_type": spec.return_type,
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _local_variables(local_variables: List[Tuple["VariableDefinition", int]], rel: _RelFn) -> List[Dict[str, Any]]:
|
|
218
|
+
return [
|
|
219
|
+
{"variable": _variable_stub(var, rel), "visible_from_line": visible_from_line}
|
|
220
|
+
for var, visible_from_line in local_variables
|
|
221
|
+
]
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
# --- Statements ---
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _statement(stmt: SemanticStatement, rel: _RelFn) -> Dict[str, Any]:
|
|
228
|
+
result: Dict[str, Any] = {
|
|
229
|
+
"kind": stmt.kind.name,
|
|
230
|
+
"class": type(stmt).__name__,
|
|
231
|
+
"line_start": stmt.line_start,
|
|
232
|
+
"line_end": stmt.line_end,
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if isinstance(stmt, KeywordCallStatement):
|
|
236
|
+
result["keyword_doc"] = _keyword_stub(stmt.keyword_doc, rel)
|
|
237
|
+
result["lib_entry"] = _lib_entry_stub(stmt.lib_entry, rel)
|
|
238
|
+
if stmt.assign_variables:
|
|
239
|
+
result["assign_variables"] = _tokens(stmt.assign_variables)
|
|
240
|
+
if isinstance(stmt, RunKeywordCallStatement):
|
|
241
|
+
result["inner_calls"] = [_statement(inner, rel) for inner in stmt.inner_calls]
|
|
242
|
+
elif isinstance(stmt, InlineIfStatement):
|
|
243
|
+
result["condition"] = stmt.condition
|
|
244
|
+
if stmt.assign_variable is not None:
|
|
245
|
+
result["assign_variable"] = _token(stmt.assign_variable)
|
|
246
|
+
elif isinstance(stmt, VarStatement):
|
|
247
|
+
result["variable_name"] = _token(stmt.variable_name) if stmt.variable_name is not None else None
|
|
248
|
+
result["scope"] = stmt.scope.name if stmt.scope is not None else None
|
|
249
|
+
result["separator"] = stmt.separator
|
|
250
|
+
if stmt.values:
|
|
251
|
+
result["values"] = _tokens(stmt.values)
|
|
252
|
+
elif isinstance(stmt, ReturnStatement):
|
|
253
|
+
if stmt.return_values:
|
|
254
|
+
result["return_values"] = _tokens(stmt.return_values)
|
|
255
|
+
elif isinstance(stmt, ImportStatement):
|
|
256
|
+
result["import_type"] = stmt.import_type.name if stmt.import_type is not None else None
|
|
257
|
+
result["import_name"] = stmt.import_name
|
|
258
|
+
result["alias"] = stmt.alias
|
|
259
|
+
if stmt.arguments:
|
|
260
|
+
result["arguments"] = _tokens(stmt.arguments)
|
|
261
|
+
result["lib_entry"] = _lib_entry_stub(stmt.lib_entry, rel)
|
|
262
|
+
result["init_keyword_doc"] = _keyword_stub(stmt.init_keyword_doc, rel)
|
|
263
|
+
elif isinstance(stmt, SettingStatement):
|
|
264
|
+
result["setting_name"] = stmt.setting_name
|
|
265
|
+
if stmt.argument_definitions:
|
|
266
|
+
result["argument_definitions"] = _tokens(stmt.argument_definitions)
|
|
267
|
+
if stmt.tag_values:
|
|
268
|
+
result["tag_values"] = list(stmt.tag_values)
|
|
269
|
+
elif isinstance(stmt, DefinitionStatement):
|
|
270
|
+
result["name"] = stmt.name
|
|
271
|
+
result["arguments_spec"] = _argument_spec(stmt.arguments_spec)
|
|
272
|
+
result["return_type"] = stmt.return_type
|
|
273
|
+
if stmt.tags:
|
|
274
|
+
result["tags"] = list(stmt.tags)
|
|
275
|
+
# Populated only in legacy flat mode (no tree); tree mode keeps local
|
|
276
|
+
# variables on the DefinitionBlock, dumped under `local_scopes`.
|
|
277
|
+
if stmt.local_variables:
|
|
278
|
+
result["local_variables"] = _local_variables(stmt.local_variables, rel)
|
|
279
|
+
elif isinstance(stmt, TemplateDataStatement):
|
|
280
|
+
result["template_keyword_doc"] = _keyword_stub(stmt.template_keyword_doc, rel)
|
|
281
|
+
|
|
282
|
+
result["tokens"] = _tokens(stmt.tokens)
|
|
283
|
+
return result
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
# --- Blocks ---
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _block(block: SemanticBlock, rel: _RelFn) -> Dict[str, Any]:
|
|
290
|
+
result: Dict[str, Any] = {
|
|
291
|
+
"kind": block.kind.name,
|
|
292
|
+
"class": type(block).__name__,
|
|
293
|
+
"line_start": block.line_start,
|
|
294
|
+
"line_end": block.line_end,
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if isinstance(block, DefinitionBlock):
|
|
298
|
+
result["name"] = block.name
|
|
299
|
+
result["arguments_spec"] = _argument_spec(block.arguments_spec)
|
|
300
|
+
result["return_type"] = block.return_type
|
|
301
|
+
if block.tags:
|
|
302
|
+
result["tags"] = list(block.tags)
|
|
303
|
+
elif isinstance(block, ForBlock):
|
|
304
|
+
result["flavor"] = block.flavor.name if block.flavor is not None else None
|
|
305
|
+
result["loop_variables"] = _tokens(block.loop_variables)
|
|
306
|
+
result["start"] = block.start
|
|
307
|
+
result["mode"] = block.mode.name if block.mode is not None else None
|
|
308
|
+
result["fill"] = block.fill
|
|
309
|
+
elif isinstance(block, WhileBlock):
|
|
310
|
+
result["condition"] = block.condition
|
|
311
|
+
result["limit"] = block.limit
|
|
312
|
+
result["on_limit"] = block.on_limit.name if block.on_limit is not None else None
|
|
313
|
+
result["on_limit_message"] = block.on_limit_message
|
|
314
|
+
elif isinstance(block, IfBlock):
|
|
315
|
+
result["condition"] = block.condition
|
|
316
|
+
|
|
317
|
+
result["header"] = _statement(block.header, rel) if block.header is not None else None
|
|
318
|
+
body: List[Dict[str, Any]] = []
|
|
319
|
+
for child in block.body:
|
|
320
|
+
if isinstance(child, SemanticBlock):
|
|
321
|
+
body.append(_block(child, rel))
|
|
322
|
+
elif isinstance(child, SemanticStatement):
|
|
323
|
+
body.append(_statement(child, rel))
|
|
324
|
+
result["body"] = body
|
|
325
|
+
return result
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
# --- Scopes ---
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def _file_scope(model: SemanticModel, rel: _RelFn) -> Optional[Dict[str, Any]]:
|
|
332
|
+
scope = model.file_scope
|
|
333
|
+
if scope is None:
|
|
334
|
+
return None
|
|
335
|
+
return {
|
|
336
|
+
"command_line": [_variable_stub(var, rel) for var in scope.command_line_variables],
|
|
337
|
+
"own": [_variable_stub(var, rel) for var in scope.own_variables],
|
|
338
|
+
"imported": [_variable_stub(var, rel) for var in scope.imported_variables],
|
|
339
|
+
"builtin": [_variable_stub(var, rel) for var in scope.builtin_variables],
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def _iter_definition_blocks(block: SemanticBlock) -> List[DefinitionBlock]:
|
|
344
|
+
result: List[DefinitionBlock] = []
|
|
345
|
+
for child in block.body:
|
|
346
|
+
if isinstance(child, DefinitionBlock):
|
|
347
|
+
result.append(child)
|
|
348
|
+
if isinstance(child, SemanticBlock):
|
|
349
|
+
result.extend(_iter_definition_blocks(child))
|
|
350
|
+
return result
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _local_scopes(model: SemanticModel, rel: _RelFn) -> List[Dict[str, Any]]:
|
|
354
|
+
if model.root is None:
|
|
355
|
+
return []
|
|
356
|
+
return [
|
|
357
|
+
{
|
|
358
|
+
"name": definition.name,
|
|
359
|
+
"line_start": definition.line_start,
|
|
360
|
+
"line_end": definition.line_end,
|
|
361
|
+
"local_variables": _local_variables(definition.local_variables, rel),
|
|
362
|
+
}
|
|
363
|
+
for definition in _iter_definition_blocks(model.root)
|
|
364
|
+
]
|
|
@@ -10,6 +10,7 @@ from .nodes import (
|
|
|
10
10
|
SemanticStatement,
|
|
11
11
|
SemanticToken,
|
|
12
12
|
)
|
|
13
|
+
from .variable_tokenizer import _MATCH_EXTENDED
|
|
13
14
|
|
|
14
15
|
_SECTION_KINDS: FrozenSet[NodeKind] = frozenset(
|
|
15
16
|
{
|
|
@@ -287,8 +288,10 @@ class SemanticModel:
|
|
|
287
288
|
self,
|
|
288
289
|
name: str,
|
|
289
290
|
line: int,
|
|
291
|
+
col: Optional[int] = None,
|
|
290
292
|
skip_commandline_variables: bool = False,
|
|
291
293
|
skip_local_variables: bool = False,
|
|
294
|
+
extended: bool = True,
|
|
292
295
|
) -> Optional["VariableDefinition"]:
|
|
293
296
|
"""Position-aware variable lookup. Replaces ScopeTree.find_variable().
|
|
294
297
|
|
|
@@ -298,11 +301,39 @@ class SemanticModel:
|
|
|
298
301
|
- ``${SPACE * 5}`` → strips `` * 5`` → looks up ``${SPACE}``
|
|
299
302
|
- ``${{expr}}`` → inline Python expression, no variable lookup
|
|
300
303
|
|
|
304
|
+
``extended=False`` disables the extended-syntax stripping — the raw
|
|
305
|
+
lookup the legacy resolution performs before its extended fallback.
|
|
306
|
+
|
|
307
|
+
When ``col`` is given, visibility on the *defining* line is
|
|
308
|
+
column-aware like the legacy scope tree: arguments become visible
|
|
309
|
+
after their defining cell, assign targets from the start of their
|
|
310
|
+
defining name.
|
|
311
|
+
|
|
301
312
|
Search order:
|
|
302
|
-
1.
|
|
303
|
-
2.
|
|
313
|
+
1. Environment variables (``%{NAME}``, ``%{NAME=default}``)
|
|
314
|
+
2. Block-local variables (visible_from_line <= line) in enclosing definition
|
|
315
|
+
3. File-scope variables (command-line, own, imported, builtin)
|
|
304
316
|
"""
|
|
305
|
-
|
|
317
|
+
from ..entities import (
|
|
318
|
+
ArgumentDefinition,
|
|
319
|
+
EmbeddedArgumentDefinition,
|
|
320
|
+
EnvironmentVariableDefinition,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
if name[:2] == "%{" and name[-1:] == "}":
|
|
324
|
+
var_name, sep, default_value = name[2:-1].partition("=")
|
|
325
|
+
return EnvironmentVariableDefinition(
|
|
326
|
+
0,
|
|
327
|
+
0,
|
|
328
|
+
0,
|
|
329
|
+
0,
|
|
330
|
+
"",
|
|
331
|
+
f"%{{{var_name}}}",
|
|
332
|
+
None,
|
|
333
|
+
default_value=default_value if sep else None,
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
base_name = self._normalize_variable_name(name, extended=extended)
|
|
306
337
|
if base_name is None:
|
|
307
338
|
return None
|
|
308
339
|
|
|
@@ -310,7 +341,22 @@ class SemanticModel:
|
|
|
310
341
|
definition = self.enclosing_definition(line)
|
|
311
342
|
if definition is not None:
|
|
312
343
|
for var_def, visible_from in reversed(definition.local_variables):
|
|
313
|
-
if visible_from
|
|
344
|
+
if visible_from > line:
|
|
345
|
+
continue
|
|
346
|
+
if col is not None and visible_from == line and var_def.line_no == line:
|
|
347
|
+
if isinstance(var_def, ArgumentDefinition) and not isinstance(
|
|
348
|
+
var_def, EmbeddedArgumentDefinition
|
|
349
|
+
):
|
|
350
|
+
# Legacy registers `[Arguments]` definitions
|
|
351
|
+
# visible from the end of their defining cell;
|
|
352
|
+
# everything else (assigns, embedded arguments)
|
|
353
|
+
# from the start of the defining name.
|
|
354
|
+
boundary = self._defining_cell_end(var_def, line)
|
|
355
|
+
else:
|
|
356
|
+
boundary = var_def.col_offset or 0
|
|
357
|
+
if col < boundary:
|
|
358
|
+
continue
|
|
359
|
+
if var_def.matcher == base_name:
|
|
314
360
|
return var_def
|
|
315
361
|
|
|
316
362
|
if self.file_scope is not None:
|
|
@@ -322,8 +368,20 @@ class SemanticModel:
|
|
|
322
368
|
|
|
323
369
|
return None
|
|
324
370
|
|
|
371
|
+
def _defining_cell_end(self, var_def: "VariableDefinition", line: int) -> int:
|
|
372
|
+
"""End column of the statement cell containing a definition's name —
|
|
373
|
+
the column from which legacy makes an argument visible on its own
|
|
374
|
+
defining line."""
|
|
375
|
+
stmt = self.statement_at(line)
|
|
376
|
+
if stmt is not None and var_def.col_offset is not None:
|
|
377
|
+
for token in stmt.tokens:
|
|
378
|
+
if token.line == line and token.col_offset <= var_def.col_offset < token.col_offset + token.length:
|
|
379
|
+
return token.col_offset + token.length
|
|
380
|
+
# Fallback: end of the stripped name plus its closing brace.
|
|
381
|
+
return (var_def.end_col_offset or 0) + 1
|
|
382
|
+
|
|
325
383
|
@staticmethod
|
|
326
|
-
def _normalize_variable_name(name: str) -> Optional[str]:
|
|
384
|
+
def _normalize_variable_name(name: str, extended: bool = True) -> Optional[str]:
|
|
327
385
|
"""Strip Extended Syntax, Index Access, and Expression syntax
|
|
328
386
|
to recover the base variable name for lookup.
|
|
329
387
|
|
|
@@ -344,13 +402,16 @@ class SemanticModel:
|
|
|
344
402
|
break
|
|
345
403
|
base = base[:bracket_start]
|
|
346
404
|
|
|
347
|
-
# Strip extended syntax inside braces: ${obj.attr} → ${obj}
|
|
348
|
-
|
|
405
|
+
# Strip extended syntax inside braces: ${obj.attr} → ${obj}. Like the
|
|
406
|
+
# legacy matcher, extended syntax starts at the first character that is
|
|
407
|
+
# neither a word character nor whitespace — a space alone is part of
|
|
408
|
+
# the name (`${name with space}`).
|
|
409
|
+
if extended and base.startswith(("${", "@{", "&{", "%{")):
|
|
349
410
|
inner = base[2:-1]
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
411
|
+
extended_match = _MATCH_EXTENDED.match(inner)
|
|
412
|
+
if extended_match is not None:
|
|
413
|
+
prefix = base[:2]
|
|
414
|
+
return f"{prefix}{extended_match.group(1).strip()}}}"
|
|
354
415
|
return base
|
|
355
416
|
|
|
356
417
|
def get_variables_at(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from dataclasses import dataclass, field
|
|
2
|
-
from typing import TYPE_CHECKING, List, Optional, Tuple
|
|
2
|
+
from typing import TYPE_CHECKING, FrozenSet, List, Optional, Tuple
|
|
3
3
|
|
|
4
4
|
from robotcode.core.lsp.types import Position, Range
|
|
5
5
|
|
|
@@ -10,6 +10,7 @@ from .enums import (
|
|
|
10
10
|
NodeKind,
|
|
11
11
|
OnLimitAction,
|
|
12
12
|
TokenKind,
|
|
13
|
+
TokenModifier,
|
|
13
14
|
VarScope,
|
|
14
15
|
)
|
|
15
16
|
|
|
@@ -38,6 +39,11 @@ class SemanticToken:
|
|
|
38
39
|
|
|
39
40
|
sub_tokens: Optional[List["SemanticToken"]] = None
|
|
40
41
|
|
|
42
|
+
# Pre-computed semantic modifiers (builtin, embedded, declaration,
|
|
43
|
+
# documentation). Set by the analyzer from already-resolved data —
|
|
44
|
+
# consumers map these without re-resolution. None means "no modifiers".
|
|
45
|
+
modifiers: Optional[FrozenSet[TokenModifier]] = None
|
|
46
|
+
|
|
41
47
|
# Pre-computed LSP `Range` covering this token. Built once in
|
|
42
48
|
# `__post_init__` so consumers get the canonical (0-indexed) Range
|
|
43
49
|
# without re-deriving it from `line - 1` / `col_offset` / `length`.
|