Skip to content

Commit 008644d

Browse files
committed
more speedups in collecting/finding references
1 parent 129ef7f commit 008644d

File tree

5 files changed

+30
-47
lines changed

5 files changed

+30
-47
lines changed

robotcode/language_server/common/parts/documents.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def __init__(self, parent: LanguageServerProtocol) -> None:
6969
self.parent.on_initialized.add(self._protocol_initialized)
7070
self._lock = Lock()
7171

72+
@property
73+
def documents(self) -> List[TextDocument]:
74+
return list(self._documents.values())
75+
7276
async def _protocol_initialized(self, sender: Any) -> None:
7377
await self._update_filewatchers()
7478

robotcode/language_server/robotframework/parts/codelens.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async def resolve(self, sender: Any, code_lens: CodeLens) -> Optional[CodeLens]:
105105
name = code_lens.data["name"]
106106
line = code_lens.data["line"]
107107

108-
if self.parent.robot_workspace.workspace_loaded:
108+
if self.parent.robot_workspace.workspace_loaded.is_set():
109109
kw_doc = await self.get_keyword_definition_at_line(namespace, name, line)
110110

111111
if kw_doc is not None and not kw_doc.is_error_handler:
@@ -128,6 +128,7 @@ async def find_refs() -> None:
128128
kw_doc,
129129
include_declaration=False,
130130
)
131+
131132
# await self.parent.robot_references.find_keyword_references(
132133
# document, kw_doc, include_declaration=False
133134
# )

robotcode/language_server/robotframework/parts/references.py

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,14 @@
1515
)
1616

1717
from ....utils.async_cache import AsyncSimpleLRUCache
18-
from ....utils.async_tools import run_coroutine_in_thread, threaded
19-
from ....utils.glob_path import iter_files
18+
from ....utils.async_tools import threaded
2019
from ....utils.logging import LoggingDescriptor
2120
from ....utils.uri import Uri
2221
from ...common.decorators import language_id
2322
from ...common.lsp_types import Location, Position, ReferenceContext
2423
from ...common.text_document import TextDocument
25-
from ..configuration import WorkspaceConfig
2624
from ..diagnostics.entities import LocalVariableDefinition, VariableDefinition
27-
from ..diagnostics.library_doc import (
28-
RESOURCE_FILE_EXTENSION,
29-
ROBOT_FILE_EXTENSION,
30-
KeywordDoc,
31-
LibraryDoc,
32-
)
25+
from ..diagnostics.library_doc import KeywordDoc, LibraryDoc
3326
from ..utils.ast_utils import (
3427
HasTokens,
3528
get_nodes_at_position,
@@ -108,36 +101,13 @@ async def _find_references_in_workspace(
108101
*args: Any,
109102
**kwargs: Any,
110103
) -> List[Location]:
111-
futures: List[Awaitable[List[Location]]] = []
104+
await self.parent.robot_workspace.workspace_loaded.wait()
112105

113106
result: List[Location] = []
114107

115-
for folder in self.parent.workspace.workspace_folders:
116-
117-
config = await self.parent.workspace.get_configuration(WorkspaceConfig, folder.uri) or WorkspaceConfig()
118-
119-
async for f in iter_files(
120-
folder.uri.to_path(),
121-
(f"**/*.{{{ROBOT_FILE_EXTENSION[1:]},{RESOURCE_FILE_EXTENSION[1:]}}}"),
122-
ignore_patterns=config.exclude_patterns or [], # type: ignore
123-
absolute=True,
124-
):
125-
try:
126-
doc = await self.parent.documents.get_or_open_document(f, "robotframework")
127-
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
128-
raise
129-
except BaseException as ex:
130-
self._logger.exception(ex)
131-
else:
132-
futures.append(run_coroutine_in_thread(func, doc, *args, **kwargs))
133-
134-
for e in await asyncio.gather(*futures, return_exceptions=True):
135-
if isinstance(e, BaseException):
136-
if not isinstance(result, asyncio.CancelledError):
137-
self._logger.exception(e)
138-
continue
139-
if e:
140-
result.extend(e)
108+
for doc in self.parent.documents.documents:
109+
if doc.language_id == "robotframework":
110+
result.extend(await func(doc, *args, **kwargs))
141111

142112
return result
143113

robotcode/language_server/robotframework/parts/robot_workspace.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from typing import TYPE_CHECKING, Any, List, Optional
55

6-
from ....utils.async_tools import threaded
6+
from ....utils.async_tools import Event, threaded
77
from ....utils.glob_path import iter_files
88
from ....utils.logging import LoggingDescriptor
99
from ....utils.uri import Uri
@@ -30,7 +30,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
3030
self.parent.documents.on_read_document_text.add(self._on_read_document_text)
3131
self.parent.diagnostics.collect_workspace_documents.add(self._collect_workspace_documents)
3232
self.parent.diagnostics.on_get_diagnostics_mode.add(self.on_get_diagnostics_mode)
33-
self.workspace_loaded = False
33+
self.workspace_loaded = Event()
3434

3535
@language_id("robotframework")
3636
async def _on_read_document_text(self, sender: Any, uri: Uri) -> Optional[str]:
@@ -101,7 +101,7 @@ async def _collect_workspace_documents(self, sender: Any) -> List[WorkspaceDocum
101101
except BaseException as e:
102102
self._logger.exception(e)
103103

104-
self.workspace_loaded = True
104+
self.workspace_loaded.set()
105105

106106
if config.analysis.references_code_lens:
107107
await self.parent.code_lens.refresh()

robotcode/utils/async_tools.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ async def create_inner_task() -> _T:
475475
inner_task.cancel()
476476

477477
return await inner_task
478+
478479
finally:
479480
threading.current_thread().setName(old_name)
480481

@@ -585,6 +586,8 @@ async def __inner_lock(self) -> AsyncGenerator[Any, None]:
585586
yield None
586587
finally:
587588
self._lock.release()
589+
# with self._lock:
590+
# yield None
588591

589592
async def acquire(self) -> bool:
590593
async with self.__inner_lock():
@@ -603,7 +606,8 @@ async def acquire(self) -> bool:
603606
await fut
604607
finally:
605608
async with self.__inner_lock():
606-
self._waiters.remove(fut)
609+
if fut in self._waiters:
610+
self._waiters.remove(fut)
607611
self._locked = True
608612
except asyncio.CancelledError:
609613
await self._wake_up_next()
@@ -621,16 +625,19 @@ async def release(self) -> None:
621625
else:
622626
warnings.warn(f"Lock is not acquired ({len(self._waiters) if self._waiters else 0} waiters).")
623627

624-
await self._wake_up_next()
628+
await self._wake_up_next()
625629

626630
async def _wake_up_next(self) -> None:
627631
if not self._waiters:
628632
return
629633

630-
try:
631-
fut = next(iter(self._waiters))
632-
except StopIteration:
633-
return
634+
async with self.__inner_lock():
635+
try:
636+
fut = next(iter(self._waiters))
637+
except StopIteration:
638+
return
639+
if fut in self._waiters:
640+
self._waiters.remove(fut)
634641

635642
def s() -> None:
636643
if not fut.done():
@@ -640,7 +647,8 @@ def s() -> None:
640647
if not fut.done():
641648
fut.set_result(True)
642649
else:
643-
fut._loop.call_soon_threadsafe(s)
650+
if fut._loop.is_running():
651+
fut._loop.call_soon_threadsafe(s)
644652

645653

646654
class FutureInfo:

0 commit comments

Comments
 (0)