11import ast
22from concurrent .futures import CancelledError
3- from typing import TYPE_CHECKING , Any , Callable , List , Optional , Type , cast
3+ from typing import TYPE_CHECKING , Any , Callable , Iterable , List , Optional , Type , cast
44
55from robot .parsing .lexer .tokens import Token as RobotToken
66from robot .parsing .model import statements
@@ -150,7 +150,7 @@ def _find_references_in_workspace(
150150 self ,
151151 document : TextDocument ,
152152 stop_at_first : bool ,
153- func : Callable [..., List [Location ]],
153+ func : Callable [..., Iterable [Location ]],
154154 * args : Any ,
155155 ** kwargs : Any ,
156156 ) -> List [Location ]:
@@ -249,73 +249,44 @@ def find_variable_references_in_file(
249249 doc : TextDocument ,
250250 variable : VariableDefinition ,
251251 include_declaration : bool = True ,
252- ) -> List [Location ]:
252+ ) -> Iterable [Location ]:
253253 try :
254254 namespace = self .parent .documents_cache .get_namespace (doc )
255255
256- if (
257- variable .source
258- and variable .source != str (doc .uri .to_path ())
259- and not any (e for e in (namespace .get_resources ()).values () if e .library_doc .source == variable .source )
260- and not any (
261- e for e in namespace .get_variables_imports ().values () if e .library_doc .source == variable .source
262- )
263- and not any (e for e in namespace .get_command_line_variables () if e .source == variable .source )
264- ):
265- return []
266-
267- result = set ()
268- if include_declaration and variable .source :
269- result .add (Location (str (Uri .from_path (variable .source )), variable .name_range ))
270-
271256 refs = namespace .get_variable_references ()
272257 if variable in refs :
273- result |= refs [variable ]
258+ if include_declaration and variable .source == namespace .source :
259+ yield Location (str (Uri .from_path (variable .source )), variable .name_range )
260+
261+ yield from refs [variable ]
274262
275- return list (result )
276263 except (SystemExit , KeyboardInterrupt , CancelledError ):
277264 raise
278265 except BaseException as e :
279266 self ._logger .exception (e )
280267
281- return []
282-
283268 @_logger .call
284269 def find_keyword_references_in_file (
285270 self ,
286271 doc : TextDocument ,
287272 kw_doc : KeywordDoc ,
288- lib_doc : Optional [LibraryDoc ] = None ,
289273 include_declaration : bool = True ,
290- ) -> List [Location ]:
274+ ) -> Iterable [Location ]:
291275 try :
292276 namespace = self .parent .documents_cache .get_namespace (doc )
293277
294- if (
295- lib_doc is not None
296- and lib_doc .source is not None
297- and lib_doc .source != str (doc .uri .to_path ())
298- and lib_doc not in (e .library_doc for e in (namespace .get_libraries ()).values ())
299- and lib_doc not in (e .library_doc for e in (namespace .get_resources ()).values ())
300- ):
301- return []
302-
303- result = set ()
304- if include_declaration and kw_doc .source :
305- result .add (Location (str (Uri .from_path (kw_doc .source )), kw_doc .range ))
306-
307278 refs = namespace .get_keyword_references ()
308279 if kw_doc in refs :
309- result |= refs [kw_doc ]
280+ if include_declaration and kw_doc .source == namespace .source :
281+ yield Location (str (Uri .from_path (kw_doc .source )), kw_doc .range )
282+
283+ yield from refs [kw_doc ]
310284
311- return list (result )
312285 except (SystemExit , KeyboardInterrupt , CancelledError ):
313286 raise
314287 except BaseException as e :
315288 self ._logger .exception (e )
316289
317- return []
318-
319290 def has_cached_keyword_references (
320291 self ,
321292 document : TextDocument ,
@@ -346,28 +317,6 @@ def _find_keyword_references(
346317 include_declaration : bool = True ,
347318 stop_at_first : bool = False ,
348319 ) -> List [Location ]:
349- namespace = self .parent .documents_cache .get_namespace (document )
350-
351- lib_doc = (
352- next (
353- (
354- e .library_doc
355- for e in (namespace .get_libraries ()).values ()
356- if kw_doc in e .library_doc .keywords .values ()
357- ),
358- None ,
359- )
360- or next (
361- (
362- e .library_doc
363- for e in (namespace .get_resources ()).values ()
364- if kw_doc in e .library_doc .keywords .values ()
365- ),
366- None ,
367- )
368- or namespace .get_library_doc ()
369- )
370-
371320 result = []
372321
373322 if include_declaration and kw_doc .source :
@@ -379,7 +328,6 @@ def _find_keyword_references(
379328 stop_at_first ,
380329 self .find_keyword_references_in_file ,
381330 kw_doc ,
382- lib_doc ,
383331 False ,
384332 )
385333 )
0 commit comments