Skip to content

Commit 0a08964

Browse files
committed
optimize looking for references only in file wich have the same import
1 parent 98e5441 commit 0a08964

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

robotcode/language_server/robotframework/parts/references.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from ...common.lsp_types import Location, Position, ReferenceContext
2626
from ...common.text_document import TextDocument
2727
from ..configuration import WorkspaceConfig
28-
from ..diagnostics.library_doc import KeywordDoc, KeywordMatcher
28+
from ..diagnostics.library_doc import KeywordDoc, KeywordMatcher, LibraryDoc
2929
from ..utils.ast import (
3030
HasTokens,
3131
Token,
@@ -281,7 +281,7 @@ def _yield_owner_and_kw_names(full_name: str) -> Iterator[Tuple[Optional[str], .
281281
yield ".".join(tokens[:i]), ".".join(tokens[i:])
282282

283283
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
285285
) -> List[Location]:
286286
from robot.parsing.lexer.tokens import Token as RobotToken
287287
from robot.parsing.model.statements import (
@@ -295,11 +295,21 @@ async def _find_keyword_references_from_file(
295295
namespace = await self.parent.documents_cache.get_namespace(doc, cancelation_token=cancel_token)
296296
await namespace.ensure_initialized()
297297

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+
298305
async def _run() -> List[Location]:
299306
kw_matcher = KeywordMatcher(kw_doc.name)
300307

301308
result: List[Location] = []
302309

310+
libraries_matchers = await namespace.get_libraries_matchers()
311+
resources_matchers = await namespace.get_resources_matchers()
312+
303313
for node in ast.walk(namespace.model):
304314
cancel_token.throw_if_canceled()
305315

@@ -308,17 +318,17 @@ async def _run() -> List[Location]:
308318

309319
if isinstance(node, KeywordCall):
310320
kw_token = node.get_token(RobotToken.KEYWORD)
311-
312321
elif isinstance(node, Fixture):
313322
kw_token = node.get_token(RobotToken.NAME)
314323
elif isinstance(node, (Template, TestTemplate)):
315324
kw_token = node.get_token(RobotToken.NAME)
316325

317-
library_names = [KeywordMatcher(v) for v in (await namespace.get_libraries()).keys()]
318326
if kw_token is not None:
319327
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
322332

323333
if kw_matcher == name:
324334
kw = await namespace.find_keyword(str(kw_token.value))
@@ -339,6 +349,30 @@ async def _find_keyword_references(self, document: TextDocument, kw_doc: Keyword
339349
if folder is None:
340350
return []
341351

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+
342376
cancel_token = CancelationToken()
343377

344378
futures: List[Awaitable[List[Location]]] = []
@@ -353,7 +387,9 @@ async def _find_keyword_references(self, document: TextDocument, kw_doc: Keyword
353387
ignore_patterns=config.exclude_patterns or [], # type: ignore
354388
absolute=True,
355389
):
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+
)
357393

358394
for e in await asyncio.gather(*futures, return_exceptions=True):
359395
if isinstance(e, BaseException):

0 commit comments

Comments
 (0)