25
25
from ...common .lsp_types import Location , Position , ReferenceContext
26
26
from ...common .text_document import TextDocument
27
27
from ..configuration import WorkspaceConfig
28
- from ..diagnostics .library_doc import KeywordDoc , KeywordMatcher
28
+ from ..diagnostics .library_doc import KeywordDoc , KeywordMatcher , LibraryDoc
29
29
from ..utils .ast import (
30
30
HasTokens ,
31
31
Token ,
@@ -281,7 +281,7 @@ def _yield_owner_and_kw_names(full_name: str) -> Iterator[Tuple[Optional[str], .
281
281
yield "." .join (tokens [:i ]), "." .join (tokens [i :])
282
282
283
283
async def _find_keyword_references_from_file (
284
- self , kw_doc : KeywordDoc , file : Path , cancel_token : CancelationToken
284
+ self , kw_doc : KeywordDoc , lib_doc : Optional [ LibraryDoc ], file : Path , cancel_token : CancelationToken
285
285
) -> List [Location ]:
286
286
from robot .parsing .lexer .tokens import Token as RobotToken
287
287
from robot .parsing .model .statements import (
@@ -295,11 +295,21 @@ async def _find_keyword_references_from_file(
295
295
namespace = await self .parent .documents_cache .get_namespace (doc , cancelation_token = cancel_token )
296
296
await namespace .ensure_initialized ()
297
297
298
+ if (
299
+ lib_doc is not None
300
+ and lib_doc not in (e .library_doc for e in (await namespace .get_libraries ()).values ())
301
+ and lib_doc not in (e .library_doc for e in (await namespace .get_resources ()).values ())
302
+ ):
303
+ return []
304
+
298
305
async def _run () -> List [Location ]:
299
306
kw_matcher = KeywordMatcher (kw_doc .name )
300
307
301
308
result : List [Location ] = []
302
309
310
+ libraries_matchers = await namespace .get_libraries_matchers ()
311
+ resources_matchers = await namespace .get_resources_matchers ()
312
+
303
313
for node in ast .walk (namespace .model ):
304
314
cancel_token .throw_if_canceled ()
305
315
@@ -308,17 +318,17 @@ async def _run() -> List[Location]:
308
318
309
319
if isinstance (node , KeywordCall ):
310
320
kw_token = node .get_token (RobotToken .KEYWORD )
311
-
312
321
elif isinstance (node , Fixture ):
313
322
kw_token = node .get_token (RobotToken .NAME )
314
323
elif isinstance (node , (Template , TestTemplate )):
315
324
kw_token = node .get_token (RobotToken .NAME )
316
325
317
- library_names = [KeywordMatcher (v ) for v in (await namespace .get_libraries ()).keys ()]
318
326
if kw_token is not None :
319
327
for lib , name in self ._yield_owner_and_kw_names (kw_token .value ):
320
- if lib is not None and KeywordMatcher (lib ) not in library_names :
321
- continue
328
+ if lib is not None :
329
+ lib_matcher = KeywordMatcher (lib )
330
+ if lib_matcher not in libraries_matchers and lib_matcher not in resources_matchers :
331
+ continue
322
332
323
333
if kw_matcher == name :
324
334
kw = await namespace .find_keyword (str (kw_token .value ))
@@ -339,6 +349,30 @@ async def _find_keyword_references(self, document: TextDocument, kw_doc: Keyword
339
349
if folder is None :
340
350
return []
341
351
352
+ namespace = await self .parent .documents_cache .get_namespace (document )
353
+ if namespace is None :
354
+ return None
355
+
356
+ lib_doc = (
357
+ next (
358
+ (
359
+ e .library_doc
360
+ for e in (await namespace .get_libraries ()).values ()
361
+ if kw_doc in e .library_doc .keywords .values ()
362
+ ),
363
+ None ,
364
+ )
365
+ or next (
366
+ (
367
+ e .library_doc
368
+ for e in (await namespace .get_resources ()).values ()
369
+ if kw_doc in e .library_doc .keywords .values ()
370
+ ),
371
+ None ,
372
+ )
373
+ or await namespace .get_library_doc ()
374
+ )
375
+
342
376
cancel_token = CancelationToken ()
343
377
344
378
futures : List [Awaitable [List [Location ]]] = []
@@ -353,7 +387,9 @@ async def _find_keyword_references(self, document: TextDocument, kw_doc: Keyword
353
387
ignore_patterns = config .exclude_patterns or [], # type: ignore
354
388
absolute = True ,
355
389
):
356
- futures .append (asyncio .create_task (self ._find_keyword_references_from_file (kw_doc , f , cancel_token )))
390
+ futures .append (
391
+ asyncio .create_task (self ._find_keyword_references_from_file (kw_doc , lib_doc , f , cancel_token ))
392
+ )
357
393
358
394
for e in await asyncio .gather (* futures , return_exceptions = True ):
359
395
if isinstance (e , BaseException ):
0 commit comments