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.
Files changed (32) hide show
  1. robotcode/robot/__version__.py +1 -1
  2. robotcode/robot/config/loader.py +18 -5
  3. robotcode/robot/config/model.py +258 -61
  4. robotcode/robot/diagnostics/analyzer_result.py +1 -0
  5. robotcode/robot/diagnostics/data_cache.py +12 -1
  6. robotcode/robot/diagnostics/diagnostic_rules.py +75 -0
  7. robotcode/robot/diagnostics/diagnostics_modifier.py +83 -43
  8. robotcode/robot/diagnostics/document_cache_helper.py +158 -17
  9. robotcode/robot/diagnostics/errors.py +7 -0
  10. robotcode/robot/diagnostics/import_resolver.py +178 -12
  11. robotcode/robot/diagnostics/imports_manager.py +556 -220
  12. robotcode/robot/diagnostics/library_doc.py +874 -162
  13. robotcode/robot/diagnostics/namespace.py +84 -18
  14. robotcode/robot/diagnostics/namespace_analyzer.py +59 -16
  15. robotcode/robot/diagnostics/project_index.py +88 -13
  16. robotcode/robot/diagnostics/scope_tree.py +2 -2
  17. robotcode/robot/diagnostics/semantic_analyzer/analyzer.py +790 -201
  18. robotcode/robot/diagnostics/semantic_analyzer/enums.py +39 -2
  19. robotcode/robot/diagnostics/semantic_analyzer/json_dump.py +364 -0
  20. robotcode/robot/diagnostics/semantic_analyzer/model.py +72 -11
  21. robotcode/robot/diagnostics/semantic_analyzer/nodes.py +7 -1
  22. robotcode/robot/diagnostics/semantic_analyzer/variable_tokenizer.py +218 -112
  23. robotcode/robot/diagnostics/workspace_config.py +3 -0
  24. robotcode/robot/utils/markdown_docs.py +733 -0
  25. robotcode/robot/utils/markdownformatter.py +57 -12
  26. robotcode/robot/utils/match.py +13 -0
  27. robotcode/robot/utils/robot_patching.py +4 -0
  28. robotcode/robot/utils/variables.py +3 -0
  29. {robotcode_robot-2.6.2.dist-info → robotcode_robot-2.8.0.dev548.dist-info}/METADATA +4 -3
  30. robotcode_robot-2.8.0.dev548.dist-info/RECORD +48 -0
  31. {robotcode_robot-2.6.2.dist-info → robotcode_robot-2.8.0.dev548.dist-info}/WHEEL +1 -1
  32. robotcode_robot-2.6.2.dist-info/RECORD +0 -46
@@ -26,7 +26,7 @@ from robotcode.core.lsp.types import (
26
26
  from robotcode.core.text_document import TextDocument
27
27
  from robotcode.core.uri import Uri
28
28
  from robotcode.core.utils.logging import LoggingDescriptor
29
- from robotcode.core.utils.path import FileId, file_id
29
+ from robotcode.core.utils.path import DiskInfo, FileId, file_id, normalized_path
30
30
 
31
31
  from ..utils.stubs import Languages
32
32
  from ..utils.variables import (
@@ -46,8 +46,8 @@ from .entities import (
46
46
  VariablesImport,
47
47
  )
48
48
  from .errors import DIAGNOSTICS_SOURCE_NAME
49
- from .import_resolver import ImportResolver
50
- from .imports_manager import ImportsManager
49
+ from .import_resolver import FailedImport, ImportResolver
50
+ from .imports_manager import ImportsManager, RobotFileMeta
51
51
  from .keyword_finder import KeywordFinder
52
52
  from .library_doc import (
53
53
  KeywordDoc,
@@ -127,6 +127,7 @@ class NamespaceData:
127
127
  keyword_tag_references: Dict[str, Set[Location]] = field(default_factory=dict)
128
128
  testcase_tag_references: Dict[str, Set[Location]] = field(default_factory=dict)
129
129
  metadata_references: Dict[str, Set[Location]] = field(default_factory=dict)
130
+ testcase_metadata_references: Dict[str, Set[Location]] = field(default_factory=dict)
130
131
 
131
132
  # --- ScopeTree (local scopes only, file_scope is reconstructed) ---
132
133
  local_scopes: List[LocalScope] = field(default_factory=list)
@@ -146,6 +147,24 @@ class NamespaceData:
146
147
  variable_definitions: Dict[str, VariableDefinition] = field(default_factory=dict)
147
148
 
148
149
 
150
+ def _namespace_reference_key(entry: LibraryEntry) -> str:
151
+ """The key of a `namespace_references` entry in `NamespaceData`.
152
+
153
+ Like `LibraryEntry.__hash__`, it includes the import position, so it tells
154
+ apart the entries of one library: the implicitly imported BuiltIn and an
155
+ explicit import of it, or a library imported by a resource file and the
156
+ same library imported directly.
157
+ """
158
+
159
+ def position(r: Range) -> str:
160
+ return f"{r.start.line}.{r.start.character}-{r.end.line}.{r.end.character}"
161
+
162
+ return (
163
+ f"{type(entry).__name__}:{entry.name}:{entry.import_name}:{entry.args!r}:{entry.alias or ''}"
164
+ f":{entry.import_source or ''}:{position(entry.import_range)}:{position(entry.alias_range)}"
165
+ )
166
+
167
+
149
168
  class Namespace:
150
169
  """Data container holding all results of a namespace build.
151
170
 
@@ -180,10 +199,13 @@ class Namespace:
180
199
  keyword_tag_references: Dict[str, Set[Location]],
181
200
  testcase_tag_references: Dict[str, Set[Location]],
182
201
  metadata_references: Dict[str, Set[Location]],
202
+ testcase_metadata_references: Dict[str, Set[Location]],
183
203
  scope_tree: ScopeTree,
184
204
  finder: KeywordFinder,
185
205
  sentinel: object,
186
206
  semantic_model: Optional["SemanticModel"] = None,
207
+ dependency_metas: Optional[Dict[str, Optional[Any]]] = None,
208
+ failed_imports: Optional[List[FailedImport]] = None,
187
209
  ) -> None:
188
210
  self.imports_manager = imports_manager
189
211
  self.source = source
@@ -207,10 +229,13 @@ class Namespace:
207
229
  self._keyword_tag_references = keyword_tag_references
208
230
  self._testcase_tag_references = testcase_tag_references
209
231
  self._metadata_references = metadata_references
232
+ self._testcase_metadata_references = testcase_metadata_references
210
233
  self._scope_tree = scope_tree
211
234
  self._finder: KeywordFinder = finder
212
235
  self._sentinel = sentinel # prevent GC — ref-counted by imports_manager
213
236
  self._semantic_model = semantic_model
237
+ self._dependency_metas = dependency_metas
238
+ self._failed_imports = failed_imports or []
214
239
 
215
240
  # Lazy-computed caches
216
241
  self._namespaces: Optional[Dict[KeywordMatcher, List[LibraryEntry]]] = None
@@ -222,6 +247,20 @@ class Namespace:
222
247
  imports_manager.resources_changed.add(self._on_resources_changed)
223
248
  imports_manager.variables_changed.add(self._on_variables_changed)
224
249
 
250
+ @property
251
+ def dependency_metas(self) -> Optional[Dict[str, Optional[Any]]]:
252
+ """Metadata of the dependencies captured while this namespace was resolved.
253
+
254
+ ``None`` when the namespace was built by a path that did not record
255
+ them — such a namespace must never be persisted to the disk cache.
256
+ """
257
+ return self._dependency_metas
258
+
259
+ @property
260
+ def failed_imports(self) -> List[FailedImport]:
261
+ """The imports whose resolution raised or whose library or variables documentation has errors."""
262
+ return self._failed_imports
263
+
225
264
  @property
226
265
  def document(self) -> Optional[TextDocument]:
227
266
  return self._document() if self._document is not None else None
@@ -268,6 +307,10 @@ class Namespace:
268
307
  def metadata_references(self) -> Dict[str, Set[Location]]:
269
308
  return self._metadata_references
270
309
 
310
+ @property
311
+ def testcase_metadata_references(self) -> Dict[str, Set[Location]]:
312
+ return self._testcase_metadata_references
313
+
271
314
  @property
272
315
  def import_entries(self) -> Dict[Import, LibraryEntry]:
273
316
  return self._import_entries
@@ -414,7 +457,9 @@ class Namespace:
414
457
  def invalidated(sender) -> None: ...
415
458
 
416
459
  def _on_imports_changed(self, sender: Any, uri: Any) -> None:
417
- self.invalidated(self)
460
+ # a created file can only resolve an import that failed
461
+ if self._failed_imports:
462
+ self.invalidated(self)
418
463
 
419
464
  def _on_libraries_changed(self, sender: Any, libraries: Any) -> None:
420
465
  for p in libraries:
@@ -470,10 +515,9 @@ class Namespace:
470
515
  (diagnostics, tag references, etc.) is copied directly.
471
516
  """
472
517
  # Build namespace_references key from LibraryEntry identity
473
- ns_refs: Dict[str, Set[Location]] = {}
474
- for entry, locs in self._namespace_references.items():
475
- key = f"{type(entry).__name__}:{entry.import_name}:{entry.args!r}:{entry.alias or ''}"
476
- ns_refs[key] = locs
518
+ ns_refs: Dict[str, Set[Location]] = {
519
+ _namespace_reference_key(entry): locs for entry, locs in self._namespace_references.items()
520
+ }
477
521
 
478
522
  # Collect all referenced variable definitions for stable_id → object lookup
479
523
  all_var_defs: Dict[str, VariableDefinition] = {}
@@ -534,6 +578,7 @@ class Namespace:
534
578
  keyword_tag_references={k: set(v) for k, v in self._keyword_tag_references.items()},
535
579
  testcase_tag_references={k: set(v) for k, v in self._testcase_tag_references.items()},
536
580
  metadata_references={k: set(v) for k, v in self._metadata_references.items()},
581
+ testcase_metadata_references={k: set(v) for k, v in self._testcase_metadata_references.items()},
537
582
  local_scopes=list(self._scope_tree.local_scopes),
538
583
  resolved_resource_sources=resolved_res_sources,
539
584
  variable_definitions=all_var_defs,
@@ -635,15 +680,19 @@ class Namespace:
635
680
  if sid in var_by_id:
636
681
  local_variable_assignments[var_by_id[sid]] = set(ranges)
637
682
 
638
- # Reconstruct namespace_references: key format "ClassName:import_name:args:alias"
639
- all_entries: Dict[str, LibraryEntry] = {}
640
- for entry in itertools.chain(
641
- resolved.libraries.values(),
642
- resolved.resources.values(),
643
- resolved.variables_imports.values(),
644
- ):
645
- key = f"{type(entry).__name__}:{entry.import_name}:{entry.args!r}:{entry.alias or ''}"
646
- all_entries[key] = entry
683
+ # Reconstruct namespace_references. import_entries has the entry of every
684
+ # import statement, also of one that imports something already imported;
685
+ # the other maps add the implicit default libraries and the imports of
686
+ # resource files.
687
+ all_entries: Dict[str, LibraryEntry] = {
688
+ _namespace_reference_key(entry): entry
689
+ for entry in itertools.chain(
690
+ resolved.libraries.values(),
691
+ resolved.resources.values(),
692
+ resolved.variables_imports.values(),
693
+ resolved.import_entries.values(),
694
+ )
695
+ }
647
696
 
648
697
  namespace_references: Dict[LibraryEntry, Set[Location]] = {}
649
698
  for key, locs in data.namespace_references.items():
@@ -698,10 +747,16 @@ class Namespace:
698
747
  keyword_tag_references={k: set(v) for k, v in data.keyword_tag_references.items()},
699
748
  testcase_tag_references={k: set(v) for k, v in data.testcase_tag_references.items()},
700
749
  metadata_references={k: set(v) for k, v in data.metadata_references.items()},
750
+ # Cache entries pickled before this field existed don't have the attribute.
751
+ testcase_metadata_references={
752
+ k: set(v) for k, v in getattr(data, "testcase_metadata_references", {}).items()
753
+ },
701
754
  scope_tree=scope_tree,
702
755
  finder=finder,
703
756
  sentinel=sentinel,
704
757
  semantic_model=semantic_model,
758
+ dependency_metas=resolved.dependency_metas,
759
+ failed_imports=resolved.failed_imports,
705
760
  )
706
761
 
707
762
 
@@ -723,6 +778,8 @@ class NamespaceBuilder:
723
778
  document_type: Optional[DocumentType] = None,
724
779
  languages: Optional[Languages] = None,
725
780
  workspace_languages: Optional[Languages] = None,
781
+ *,
782
+ disk_info: Optional[DiskInfo] = None,
726
783
  ) -> None:
727
784
  self._imports_manager = imports_manager
728
785
  self._model = model
@@ -732,6 +789,7 @@ class NamespaceBuilder:
732
789
  self._document_type = document_type
733
790
  self._languages = languages
734
791
  self._workspace_languages = workspace_languages
792
+ self._disk_info = disk_info
735
793
  self._enable_semantic_model = False
736
794
 
737
795
  def set_semantic_model_enabled(self, enabled: bool) -> None:
@@ -747,7 +805,12 @@ class NamespaceBuilder:
747
805
  from .semantic_analyzer.analyzer import SemanticAnalyzer
748
806
 
749
807
  with self._logger.measure_time(lambda: f"Build Namespace for {self._source}", context_name="import"):
750
- library_doc = self._imports_manager.get_libdoc_from_model(self._model, self._source)
808
+ source_meta = (
809
+ RobotFileMeta(str(normalized_path(self._source)), self._disk_info)
810
+ if self._disk_info is not None
811
+ else None
812
+ )
813
+ library_doc = self._imports_manager.get_libdoc_from_model(self._model, self._source, source_meta)
751
814
  document_uri = (
752
815
  self._document.document_uri if self._document is not None else str(Uri.from_path(self._source))
753
816
  )
@@ -835,8 +898,11 @@ class NamespaceBuilder:
835
898
  keyword_tag_references=analyzer_result.keyword_tag_references,
836
899
  testcase_tag_references=analyzer_result.testcase_tag_references,
837
900
  metadata_references=analyzer_result.metadata_references,
901
+ testcase_metadata_references=analyzer_result.testcase_metadata_references,
838
902
  scope_tree=analyzer_result.scope_tree,
839
903
  finder=finder,
840
904
  sentinel=sentinel,
841
905
  semantic_model=analyzer_result.semantic_model,
906
+ dependency_metas=resolved.dependency_metas,
907
+ failed_imports=resolved.failed_imports,
842
908
  )
@@ -14,6 +14,7 @@ from robot.parsing.lexer.tokens import Token
14
14
  from robot.parsing.model.blocks import File, Keyword, TestCase, VariableSection
15
15
  from robot.parsing.model.statements import (
16
16
  Arguments,
17
+ Documentation,
17
18
  Fixture,
18
19
  KeywordCall,
19
20
  KeywordName,
@@ -53,7 +54,7 @@ from ..utils.ast import (
53
54
  strip_variable_token,
54
55
  tokenize_variables,
55
56
  )
56
- from ..utils.match import normalize
57
+ from ..utils.match import normalize, normalize_metadata_name
57
58
  from ..utils.stubs import Languages
58
59
  from ..utils.variables import (
59
60
  BUILTIN_VARIABLES,
@@ -68,6 +69,7 @@ from ..utils.variables import (
68
69
  )
69
70
  from ..utils.visitor import Visitor
70
71
  from .analyzer_result import AnalyzerResult
72
+ from .diagnostic_rules import private_keyword_call_message, tags_row_without_empty_row
71
73
  from .entities import (
72
74
  ArgumentDefinition,
73
75
  BuiltInVariableDefinition,
@@ -128,6 +130,8 @@ class NamespaceAnalyzer(Visitor):
128
130
 
129
131
  self._current_testcase_or_keyword_name: Optional[str] = None
130
132
  self._current_keyword_doc: Optional[KeywordDoc] = None
133
+ self._in_keyword = False
134
+ self._in_testcase = False
131
135
  self._test_template: Optional[TestTemplate] = None
132
136
  self._template: Optional[Template] = None
133
137
  self._node_stack: List[ast.AST] = []
@@ -140,6 +144,7 @@ class NamespaceAnalyzer(Visitor):
140
144
  self._keyword_tag_references: Dict[str, Set[Location]] = defaultdict(set)
141
145
  self._testcase_tag_references: Dict[str, Set[Location]] = defaultdict(set)
142
146
  self._metadata_references: Dict[str, Set[Location]] = defaultdict(set)
147
+ self._testcase_metadata_references: Dict[str, Set[Location]] = defaultdict(set)
143
148
 
144
149
  # Phase 1+2 results (set by resolve())
145
150
  self._library_doc: ResourceDoc = cast(ResourceDoc, None) # set by resolve()
@@ -238,6 +243,7 @@ class NamespaceAnalyzer(Visitor):
238
243
  self._keyword_tag_references,
239
244
  self._testcase_tag_references,
240
245
  self._metadata_references,
246
+ self._testcase_metadata_references,
241
247
  self._scope_builder.build(self._variable_scope),
242
248
  )
243
249
 
@@ -904,15 +910,14 @@ class NamespaceAnalyzer(Visitor):
904
910
  code=Error.RESERVED_KEYWORD,
905
911
  )
906
912
 
907
- if result.is_resource_keyword and result.is_private:
908
- if self._source != result.source:
909
- self._append_diagnostics(
910
- range=kw_range,
911
- message=f"Keyword '{result.longname}' is private and should only be called by"
912
- f" keywords in the same file.",
913
- severity=DiagnosticSeverity.WARNING,
914
- code=Error.PRIVATE_KEYWORD,
915
- )
913
+ private_message = private_keyword_call_message(result, self._source)
914
+ if private_message is not None:
915
+ self._append_diagnostics(
916
+ range=kw_range,
917
+ message=private_message,
918
+ severity=DiagnosticSeverity.WARNING,
919
+ code=Error.PRIVATE_KEYWORD,
920
+ )
916
921
 
917
922
  if not isinstance(node, (Template, TestTemplate)):
918
923
  try:
@@ -1228,6 +1233,7 @@ class NamespaceAnalyzer(Visitor):
1228
1233
  )
1229
1234
 
1230
1235
  self._current_testcase_or_keyword_name = node.name
1236
+ self._in_testcase = True
1231
1237
  old_variables = self._variables
1232
1238
  self._variables = self._variables.copy()
1233
1239
  self._end_block_handlers = []
@@ -1243,6 +1249,7 @@ class NamespaceAnalyzer(Visitor):
1243
1249
  self._end_block_handlers = None
1244
1250
  self._variables = old_variables
1245
1251
  self._current_testcase_or_keyword_name = None
1252
+ self._in_testcase = False
1246
1253
  self._template = None
1247
1254
 
1248
1255
  def visit_TestCaseName(self, node: TestCaseName) -> None: # noqa: N802
@@ -1293,6 +1300,7 @@ class NamespaceAnalyzer(Visitor):
1293
1300
  )
1294
1301
 
1295
1302
  self._current_testcase_or_keyword_name = node.name
1303
+ self._in_keyword = True
1296
1304
  old_variables = self._variables
1297
1305
  self._variables = self._variables.copy()
1298
1306
  self._end_block_handlers = []
@@ -1315,6 +1323,7 @@ class NamespaceAnalyzer(Visitor):
1315
1323
  self._variables = old_variables
1316
1324
  self._current_testcase_or_keyword_name = None
1317
1325
  self._current_keyword_doc = None
1326
+ self._in_keyword = False
1318
1327
 
1319
1328
  EMBEDDED_ARGUMENTS_MATCHER = re.compile("([^:]+): ([^:]+)(:(.*))?")
1320
1329
 
@@ -1746,7 +1755,25 @@ class NamespaceAnalyzer(Visitor):
1746
1755
  if hasattr(node, "name") and node.name:
1747
1756
  name_token = node.get_token(Token.NAME)
1748
1757
  if name_token is not None:
1749
- self._metadata_references[node.name].add(Location(self._document_uri, range_from_token(name_token)))
1758
+ if self._in_testcase:
1759
+ refs = self._testcase_metadata_references
1760
+ else:
1761
+ refs = self._metadata_references
1762
+ # metadata names are case, space and underscore insensitive in Robot Framework
1763
+ refs[normalize_metadata_name(node.name)].add(Location(self._document_uri, range_from_token(name_token)))
1764
+
1765
+ # Robot Framework 7.5 warns when a `Tags:` section of a keyword's documentation is not preceded by an empty row
1766
+ if RF_VERSION >= (7, 5) and self._in_keyword and isinstance(node, Documentation):
1767
+ tags_row = tags_row_without_empty_row(node)
1768
+ if tags_row:
1769
+ self._append_diagnostics(
1770
+ range=Range(start=range_from_token(tags_row[0]).start, end=range_from_token(tags_row[-1]).end),
1771
+ message=f"Invalid documentation in '{self._current_testcase_or_keyword_name}': "
1772
+ "Not having an empty row before 'Tags:' is deprecated.",
1773
+ severity=DiagnosticSeverity.WARNING,
1774
+ tags=[DiagnosticTag.DEPRECATED],
1775
+ code=Error.TAGS_WITHOUT_EMPTY_ROW,
1776
+ )
1750
1777
 
1751
1778
  def visit_Timeout(self, node: Statement) -> None: # noqa: N802
1752
1779
  self._visit_block_settings_statement(node)
@@ -1759,7 +1786,7 @@ class NamespaceAnalyzer(Visitor):
1759
1786
 
1760
1787
  def visit_Tags(self, node: Statement) -> None: # noqa: N802
1761
1788
  self._visit_settings_statement(node, DiagnosticSeverity.HINT)
1762
- if any(isinstance(n, Keyword) for n in self._node_stack):
1789
+ if self._in_keyword:
1763
1790
  self._collect_tag_references(node, self._keyword_tag_references)
1764
1791
  else:
1765
1792
  self._collect_tag_references(node, self._testcase_tag_references)
@@ -1855,7 +1882,12 @@ class NamespaceAnalyzer(Visitor):
1855
1882
  for v in entries.values():
1856
1883
  if v.import_source == self._source and v.import_range == range_from_token(name_token):
1857
1884
  for k in self._namespace_references:
1858
- if type(k) is type(v) and k.library_doc.source_or_origin == v.library_doc.source_or_origin:
1885
+ # the same library imported again, also with other arguments; two classes of one module differ
1886
+ if (
1887
+ type(k) is type(v)
1888
+ and k.library_doc.source_or_origin == v.library_doc.source_or_origin
1889
+ and k.library_doc.name == v.library_doc.name
1890
+ ):
1859
1891
  self._namespace_references[k].add(Location(self._document_uri, v.import_range))
1860
1892
  break
1861
1893
  else:
@@ -1893,7 +1925,7 @@ class NamespaceAnalyzer(Visitor):
1893
1925
 
1894
1926
  def _find_variable(self, name: str) -> Optional[VariableDefinition]:
1895
1927
  if name[:2] == "%{" and name[-1] == "}":
1896
- var_name, _, default_value = name[2:-1].partition("=")
1928
+ var_name, sep, default_value = name[2:-1].partition("=")
1897
1929
  return EnvironmentVariableDefinition(
1898
1930
  0,
1899
1931
  0,
@@ -1902,7 +1934,7 @@ class NamespaceAnalyzer(Visitor):
1902
1934
  "",
1903
1935
  f"%{{{var_name}}}",
1904
1936
  None,
1905
- default_value=default_value or None,
1937
+ default_value=default_value if sep else None,
1906
1938
  )
1907
1939
 
1908
1940
  vars = (
@@ -1959,7 +1991,9 @@ class NamespaceAnalyzer(Visitor):
1959
1991
  - ``${scalar}`` → resolved value converted to string, multi-values joined with space
1960
1992
  - ``@{list}`` → ``str(list_value)`` e.g. ``"['a', 'b']"
1961
1993
  - ``&{dict}`` → ``str(dict_value)`` e.g. ``"{'a': '1'}"
1962
- - ``%{ENV}`` → environment variable value or default
1994
+ - ``%{ENV}`` → environment variable value or default, ``False`` if a variable in its
1995
+ name or in the default of an unset variable would have to be resolved (a default
1996
+ without variables behind a name with a variable is still returned)
1963
1997
  - ``${{expr}}`` → cannot evaluate statically → ``False``
1964
1998
 
1965
1999
  Returns:
@@ -1979,11 +2013,20 @@ class NamespaceAnalyzer(Visitor):
1979
2013
  # Environment variable %{VAR} or %{VAR=default}
1980
2014
  if sub_id == "%":
1981
2015
  env_inner = var_ref[2:-1]
2016
+ # A variable in the name means the environment variable RF looks up is not known here.
2017
+ name, name_default = split_from_equals(env_inner)
2018
+ if contains_variable(name, "$@&%"):
2019
+ if name_default is not None and not contains_variable(name_default, "$@&%"):
2020
+ return name_default
2021
+ return False
1982
2022
  env_name, sep, default = env_inner.partition("=")
1983
2023
  env_val = os.environ.get(env_name)
1984
2024
  if env_val is not None:
1985
2025
  return env_val
1986
2026
  if sep:
2027
+ # A variable in the default is not resolved here.
2028
+ if contains_variable(default, "$@&%"):
2029
+ return False
1987
2030
  return default
1988
2031
  return None
1989
2032
 
@@ -5,13 +5,18 @@ from collections import defaultdict
5
5
  from dataclasses import dataclass, field
6
6
  from typing import (
7
7
  TYPE_CHECKING,
8
+ Callable,
8
9
  Dict,
10
+ List,
11
+ Optional,
9
12
  Set,
13
+ Tuple,
10
14
  TypeVar,
11
15
  )
12
16
 
13
17
  from robotcode.core.lsp.types import Location
14
18
 
19
+ from ..utils.match import normalize_metadata_name
15
20
  from .entities import LibraryEntry, VariableDefinition
16
21
  from .library_doc import KeywordDoc
17
22
 
@@ -26,12 +31,14 @@ _K = TypeVar("_K")
26
31
  class _FileRefs:
27
32
  """Tracks which references a single file contributed to the global index."""
28
33
 
34
+ uris: Set[str] = field(default_factory=set)
29
35
  keyword_references: Dict[KeywordDoc, Set[Location]] = field(default_factory=dict)
30
36
  variable_references: Dict[VariableDefinition, Set[Location]] = field(default_factory=dict)
31
37
  namespace_references: Dict[LibraryEntry, Set[Location]] = field(default_factory=dict)
32
38
  keyword_tag_references: Dict[str, Set[Location]] = field(default_factory=dict)
33
39
  testcase_tag_references: Dict[str, Set[Location]] = field(default_factory=dict)
34
40
  metadata_references: Dict[str, Set[Location]] = field(default_factory=dict)
41
+ testcase_metadata_references: Dict[str, Set[Location]] = field(default_factory=dict)
35
42
 
36
43
 
37
44
  class ProjectIndex:
@@ -40,13 +47,20 @@ class ProjectIndex:
40
47
  Incrementally maintained: on file change only the affected file is
41
48
  removed and re-inserted. All lookups are O(1).
42
49
 
50
+ Lookups count only the files that `is_known_source` accepts. They leave
51
+ out the locations of the other files and remove these files, so that a
52
+ file that is gone leaves nothing behind, also when its analysis ended
53
+ after it went away. The check runs without the lock, and a file that is
54
+ analyzed again meanwhile keeps its new references.
55
+
43
56
  Thread-safety: An RLock protects all mutation operations (update_file,
44
57
  remove_file). Reads use the same lock — since writes are rare (only on
45
58
  file changes) and short, they block reads minimally.
46
59
  """
47
60
 
48
- def __init__(self) -> None:
61
+ def __init__(self, is_known_source: Optional[Callable[[str], bool]] = None) -> None:
49
62
  self._lock = threading.RLock()
63
+ self._is_known_source = is_known_source
50
64
 
51
65
  self._keyword_references: Dict[KeywordDoc, Set[Location]] = defaultdict(set)
52
66
  self._variable_references: Dict[VariableDefinition, Set[Location]] = defaultdict(set)
@@ -54,8 +68,11 @@ class ProjectIndex:
54
68
  self._keyword_tag_references: Dict[str, Set[Location]] = defaultdict(set)
55
69
  self._testcase_tag_references: Dict[str, Set[Location]] = defaultdict(set)
56
70
  self._metadata_references: Dict[str, Set[Location]] = defaultdict(set)
71
+ self._testcase_metadata_references: Dict[str, Set[Location]] = defaultdict(set)
57
72
 
58
73
  self._refs_by_file: Dict[str, _FileRefs] = {}
74
+ # the file of a location, without converting its URI to a path
75
+ self._source_by_uri: Dict[str, str] = {}
59
76
 
60
77
  def update_file(self, source: str, namespace: Namespace) -> None:
61
78
  """After NamespaceBuilder.build(): update this file's references."""
@@ -68,34 +85,48 @@ class ProjectIndex:
68
85
  namespace.keyword_references,
69
86
  self._keyword_references,
70
87
  file_refs.keyword_references,
88
+ file_refs.uris,
71
89
  )
72
90
  self._merge_refs(
73
91
  namespace.variable_references,
74
92
  self._variable_references,
75
93
  file_refs.variable_references,
94
+ file_refs.uris,
76
95
  )
77
96
  self._merge_refs(
78
97
  namespace.namespace_references,
79
98
  self._namespace_references,
80
99
  file_refs.namespace_references,
100
+ file_refs.uris,
81
101
  )
82
102
  self._merge_refs(
83
103
  namespace.keyword_tag_references,
84
104
  self._keyword_tag_references,
85
105
  file_refs.keyword_tag_references,
106
+ file_refs.uris,
86
107
  )
87
108
  self._merge_refs(
88
109
  namespace.testcase_tag_references,
89
110
  self._testcase_tag_references,
90
111
  file_refs.testcase_tag_references,
112
+ file_refs.uris,
91
113
  )
92
114
  self._merge_refs(
93
115
  namespace.metadata_references,
94
116
  self._metadata_references,
95
117
  file_refs.metadata_references,
118
+ file_refs.uris,
119
+ )
120
+ self._merge_refs(
121
+ namespace.testcase_metadata_references,
122
+ self._testcase_metadata_references,
123
+ file_refs.testcase_metadata_references,
124
+ file_refs.uris,
96
125
  )
97
126
 
98
127
  self._refs_by_file[source] = file_refs
128
+ for uri in file_refs.uris:
129
+ self._source_by_uri[uri] = source
99
130
 
100
131
  def remove_file(self, source: str) -> None:
101
132
  """File deleted or invalidated: remove all its references."""
@@ -107,24 +138,31 @@ class ProjectIndex:
107
138
  if file_refs is None:
108
139
  return
109
140
 
141
+ for uri in file_refs.uris:
142
+ if self._source_by_uri.get(uri) == source:
143
+ del self._source_by_uri[uri]
144
+
110
145
  self._subtract_refs(file_refs.keyword_references, self._keyword_references)
111
146
  self._subtract_refs(file_refs.variable_references, self._variable_references)
112
147
  self._subtract_refs(file_refs.namespace_references, self._namespace_references)
113
148
  self._subtract_refs(file_refs.keyword_tag_references, self._keyword_tag_references)
114
149
  self._subtract_refs(file_refs.testcase_tag_references, self._testcase_tag_references)
115
150
  self._subtract_refs(file_refs.metadata_references, self._metadata_references)
151
+ self._subtract_refs(file_refs.testcase_metadata_references, self._testcase_metadata_references)
116
152
 
117
153
  @staticmethod
118
154
  def _merge_refs(
119
155
  source_refs: Dict[_K, Set[Location]],
120
156
  global_refs: Dict[_K, Set[Location]],
121
157
  file_refs: Dict[_K, Set[Location]],
158
+ uris: Set[str],
122
159
  ) -> None:
123
160
  for key, locations in source_refs.items():
124
161
  if locations:
125
162
  copied = set(locations)
126
163
  global_refs[key].update(copied)
127
164
  file_refs[key] = copied
165
+ uris.update(location.uri for location in copied)
128
166
 
129
167
  @staticmethod
130
168
  def _subtract_refs(
@@ -138,32 +176,67 @@ class ProjectIndex:
138
176
  if not bucket:
139
177
  del global_refs[key]
140
178
 
179
+ def _find(self, refs: Dict[_K, Set[Location]], key: _K) -> Set[Location]:
180
+ """The locations of `key` in the files that still count; removes the files that do not."""
181
+ with self._lock:
182
+ locations = set(refs.get(key, ()))
183
+ if self._is_known_source is None:
184
+ return locations
185
+
186
+ result: Set[Location] = set()
187
+ # the locations of each file, with the entries the file had at the lookup
188
+ files: Dict[str, Tuple[_FileRefs, List[Location]]] = {}
189
+ for location in locations:
190
+ source = self._source_by_uri.get(location.uri)
191
+ file_refs = self._refs_by_file.get(source) if source is not None else None
192
+ if source is None or file_refs is None:
193
+ result.add(location)
194
+ continue
195
+ files.setdefault(source, (file_refs, []))[1].append(location)
196
+
197
+ # without the lock: the documents manager notifies didOpen under its own lock, and the diagnostics of a
198
+ # document that waits there can wait for a lookup
199
+ unknown: List[Tuple[str, _FileRefs]] = []
200
+ for source, (file_refs, file_locations) in files.items():
201
+ if self._is_known_source(source):
202
+ result.update(file_locations)
203
+ else:
204
+ unknown.append((source, file_refs))
205
+
206
+ if unknown:
207
+ with self._lock:
208
+ for source, file_refs in unknown:
209
+ # a file that was analyzed again since the lookup keeps its new references
210
+ if self._refs_by_file.get(source) is file_refs:
211
+ self._remove_file_unlocked(source)
212
+
213
+ return result
214
+
141
215
  def find_keyword_references(self, kw: KeywordDoc) -> Set[Location]:
142
216
  """O(1) lookup instead of O(N) workspace scan."""
143
- with self._lock:
144
- return set(self._keyword_references.get(kw, ()))
217
+ return self._find(self._keyword_references, kw)
145
218
 
146
219
  def find_variable_references(self, var: VariableDefinition) -> Set[Location]:
147
220
  """O(1) lookup instead of O(N) workspace scan."""
148
- with self._lock:
149
- return set(self._variable_references.get(var, ()))
221
+ return self._find(self._variable_references, var)
150
222
 
151
223
  def find_namespace_references(self, entry: LibraryEntry) -> Set[Location]:
152
224
  """O(1) lookup instead of O(N) workspace scan."""
153
- with self._lock:
154
- return set(self._namespace_references.get(entry, ()))
225
+ return self._find(self._namespace_references, entry)
155
226
 
156
227
  def find_keyword_tag_references(self, tag: str) -> Set[Location]:
157
- with self._lock:
158
- return set(self._keyword_tag_references.get(tag, ()))
228
+ return self._find(self._keyword_tag_references, tag)
159
229
 
160
230
  def find_testcase_tag_references(self, tag: str) -> Set[Location]:
161
- with self._lock:
162
- return set(self._testcase_tag_references.get(tag, ()))
231
+ return self._find(self._testcase_tag_references, tag)
163
232
 
164
233
  def find_metadata_references(self, key: str) -> Set[Location]:
165
- with self._lock:
166
- return set(self._metadata_references.get(key, ()))
234
+ """References to a suite metadata name, in any spelling Robot Framework treats as the same name."""
235
+ return self._find(self._metadata_references, normalize_metadata_name(key))
236
+
237
+ def find_testcase_metadata_references(self, key: str) -> Set[Location]:
238
+ """References to a test metadata name, in any spelling Robot Framework treats as the same name."""
239
+ return self._find(self._testcase_metadata_references, normalize_metadata_name(key))
167
240
 
168
241
  @property
169
242
  def keyword_references(self) -> Dict[KeywordDoc, Set[Location]]:
@@ -188,4 +261,6 @@ class ProjectIndex:
188
261
  self._keyword_tag_references.clear()
189
262
  self._testcase_tag_references.clear()
190
263
  self._metadata_references.clear()
264
+ self._testcase_metadata_references.clear()
191
265
  self._refs_by_file.clear()
266
+ self._source_by_uri.clear()