Skip to content

Commit d75eb9a

Browse files
committed
refactor(langserver): rename core.concurrent.FutureEx to Task
1 parent e98a865 commit d75eb9a

32 files changed

+141
-155
lines changed

packages/core/src/robotcode/core/concurrent.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
__THREADED_MARKER = "__robotcode_threaded"
2323

2424

25-
class FutureEx(Future, Generic[_TResult]): # type: ignore[type-arg]
25+
class Task(Future, Generic[_TResult]): # type: ignore[type-arg]
2626
def __init__(self) -> None:
2727
super().__init__()
2828
self.cancelation_requested_event = Event()
@@ -38,7 +38,7 @@ def cancel(self) -> bool:
3838
def result(self, timeout: Optional[float] = None) -> _TResult:
3939
return cast(_TResult, super().result(timeout))
4040

41-
def add_done_callback(self, fn: Callable[["FutureEx[Any]"], Any]) -> None:
41+
def add_done_callback(self, fn: Callable[["Task[Any]"], Any]) -> None:
4242
super().add_done_callback(fn) # type: ignore[arg-type]
4343

4444

@@ -74,14 +74,14 @@ def is_threaded_callable(callable: Callable[..., Any]) -> bool:
7474
class _Local(local):
7575
def __init__(self) -> None:
7676
super().__init__()
77-
self._local_future: Optional[FutureEx[Any]] = None
77+
self._local_future: Optional[Task[Any]] = None
7878

7979

8080
_local_storage = _Local()
8181

8282

83-
def _run_callable_in_thread_handler(
84-
future: FutureEx[_TResult],
83+
def _run_task_in_thread_handler(
84+
future: Task[_TResult],
8585
callable: Callable[..., _TResult],
8686
args: Tuple[Any, ...],
8787
kwargs: Dict[str, Any],
@@ -102,12 +102,12 @@ def _run_callable_in_thread_handler(
102102
_local_storage._local_future = None
103103

104104

105-
def is_current_thread_cancelled() -> bool:
105+
def is_current_task_cancelled() -> bool:
106106
local_future = _local_storage._local_future
107107
return local_future is not None and local_future.cancelation_requested
108108

109109

110-
def check_current_thread_canceled(at_least_seconds: Optional[float] = None, raise_exception: bool = True) -> bool:
110+
def check_current_task_canceled(at_least_seconds: Optional[float] = None, raise_exception: bool = True) -> bool:
111111
local_future = _local_storage._local_future
112112
if local_future is None:
113113
return False
@@ -125,39 +125,39 @@ def check_current_thread_canceled(at_least_seconds: Optional[float] = None, rais
125125
return True
126126

127127

128-
_running_callables_lock = RLock()
129-
_running_callables: Dict[FutureEx[Any], Thread] = {}
128+
_running_tasks_lock = RLock()
129+
_running_tasks: Dict[Task[Any], Thread] = {}
130130

131131

132-
def _remove_future_from_running_callables(future: FutureEx[Any]) -> None:
133-
with _running_callables_lock:
134-
_running_callables.pop(future, None)
132+
def _remove_future_from_running_tasks(future: Task[Any]) -> None:
133+
with _running_tasks_lock:
134+
_running_tasks.pop(future, None)
135135

136136

137137
_P = ParamSpec("_P")
138138

139139

140-
def run_in_thread(callable: Callable[_P, _TResult], *args: _P.args, **kwargs: _P.kwargs) -> FutureEx[_TResult]:
141-
future: FutureEx[_TResult] = FutureEx()
142-
with _running_callables_lock:
140+
def run_as_task(callable: Callable[_P, _TResult], *args: _P.args, **kwargs: _P.kwargs) -> Task[_TResult]:
141+
future: Task[_TResult] = Task()
142+
with _running_tasks_lock:
143143
thread = Thread(
144-
target=_run_callable_in_thread_handler,
144+
target=_run_task_in_thread_handler,
145145
args=(future, callable, args, kwargs),
146146
name=str(callable),
147147
)
148-
_running_callables[future] = thread
149-
future.add_done_callback(_remove_future_from_running_callables)
148+
_running_tasks[future] = thread
149+
future.add_done_callback(_remove_future_from_running_tasks)
150150
# TODO: don't set daemon=True because it can be deprecated in future pyhton versions
151151
thread.daemon = True
152152
thread.start()
153153

154154
return future
155155

156156

157-
def cancel_running_callables(timeout: Optional[float] = None) -> None:
157+
def _cancel_all_running_tasks(timeout: Optional[float] = None) -> None:
158158
threads: List[Thread] = []
159-
with _running_callables_lock:
160-
for future, thread in _running_callables.items():
159+
with _running_tasks_lock:
160+
for future, thread in _running_tasks.items():
161161
if not future.cancelation_requested:
162162
future.cancel()
163163
threads.append(thread)

packages/debugger/src/robotcode/debugger/protocol.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Union,
2121
)
2222

23-
from robotcode.core.concurrent import FutureEx
23+
from robotcode.core.concurrent import Task
2424
from robotcode.core.utils.dataclasses import as_dict, as_json, from_dict
2525
from robotcode.core.utils.inspect import ensure_coroutine
2626
from robotcode.core.utils.logging import LoggingDescriptor
@@ -308,8 +308,8 @@ def send_response(
308308
)
309309

310310
@_logger.call
311-
def send_request(self, request: Request, return_type: Optional[Type[TResult]] = None) -> FutureEx[TResult]:
312-
result: FutureEx[TResult] = FutureEx()
311+
def send_request(self, request: Request, return_type: Optional[Type[TResult]] = None) -> Task[TResult]:
312+
result: Task[TResult] = Task()
313313

314314
with self._sended_request_lock:
315315
self._sended_request[request.seq] = SendedRequestEntry(result, return_type)

packages/jsonrpc2/src/robotcode/jsonrpc2/protocol.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
)
3434

3535
from robotcode.core.async_tools import run_coroutine_in_thread
36-
from robotcode.core.concurrent import FutureEx, run_in_thread
36+
from robotcode.core.concurrent import Task, run_as_task
3737
from robotcode.core.event import event
3838
from robotcode.core.utils.dataclasses import as_json, from_dict
3939
from robotcode.core.utils.inspect import ensure_coroutine, iter_methods
@@ -352,7 +352,7 @@ def get_param_type(self, name: str) -> Optional[Type[Any]]:
352352

353353

354354
class SendedRequestEntry:
355-
def __init__(self, future: FutureEx[Any], result_type: Optional[Type[Any]]) -> None:
355+
def __init__(self, future: Task[Any], result_type: Optional[Type[Any]]) -> None:
356356
self.future = future
357357
self.result_type = result_type
358358

@@ -567,8 +567,8 @@ def send_request(
567567
method: str,
568568
params: Optional[Any] = None,
569569
return_type: Optional[Type[_TResult]] = None,
570-
) -> FutureEx[_TResult]:
571-
result: FutureEx[_TResult] = FutureEx()
570+
) -> Task[_TResult]:
571+
result: Task[_TResult] = Task()
572572

573573
with self._sended_request_lock:
574574
self._sended_request_count += 1
@@ -753,7 +753,7 @@ async def handle_request(self, message: JsonRPCRequest) -> None:
753753
**params[1],
754754
)
755755
else:
756-
task = asyncio.wrap_future(run_in_thread(e.method, *params[0], **params[1]))
756+
task = asyncio.wrap_future(run_as_task(e.method, *params[0], **params[1]))
757757
else:
758758
task = asyncio.create_task(e.method(*params[0], **params[1]), name=message.method)
759759

@@ -850,7 +850,7 @@ async def handle_notification(self, message: JsonRPCNotification) -> None:
850850
ensure_coroutine(cast(Callable[..., Any], e.method)), *params[0], **params[1]
851851
)
852852
else:
853-
task = asyncio.wrap_future(run_in_thread(e.method, *params[0], **params[1]))
853+
task = asyncio.wrap_future(run_as_task(e.method, *params[0], **params[1]))
854854
else:
855855
task = asyncio.create_task(e.method(*params[0], **params[1]), name=message.method)
856856

packages/language_server/src/robotcode/language_server/common/parts/code_action.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from itertools import chain
33
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union, cast
44

5-
from robotcode.core.concurrent import check_current_thread_canceled
5+
from robotcode.core.concurrent import check_current_task_canceled
66
from robotcode.core.event import event
77
from robotcode.core.lsp.types import (
88
CodeAction,
@@ -94,7 +94,7 @@ def _text_document_code_action(
9494
context,
9595
callback_filter=language_id_filter(document),
9696
):
97-
check_current_thread_canceled()
97+
check_current_task_canceled()
9898

9999
if isinstance(result, BaseException):
100100
if not isinstance(result, CancelledError):
@@ -113,7 +113,7 @@ def _text_document_code_action_resolve(self, params: CodeAction, *args: Any, **k
113113
results: List[CodeAction] = []
114114

115115
for result in self.resolve(self, params):
116-
check_current_thread_canceled()
116+
check_current_task_canceled()
117117

118118
if isinstance(result, BaseException):
119119
if not isinstance(result, CancelledError):

packages/language_server/src/robotcode/language_server/common/parts/code_lens.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from concurrent.futures import CancelledError
22
from typing import TYPE_CHECKING, Any, Final, List, Optional
33

4-
from robotcode.core.concurrent import FutureEx, check_current_thread_canceled, run_in_thread
4+
from robotcode.core.concurrent import Task, check_current_task_canceled, run_as_task
55
from robotcode.core.event import event
66
from robotcode.core.lsp.types import (
77
CodeLens,
@@ -26,7 +26,7 @@ class CodeLensProtocolPart(LanguageServerProtocolPart):
2626

2727
def __init__(self, parent: "LanguageServerProtocol") -> None:
2828
super().__init__(parent)
29-
self.refresh_task: Optional[FutureEx[Any]] = None
29+
self.refresh_task: Optional[Task[Any]] = None
3030
self._refresh_timeout = 5
3131

3232
@event
@@ -51,7 +51,7 @@ def _text_document_code_lens(
5151
return None
5252

5353
for result in self.collect(self, document, callback_filter=language_id_filter(document)):
54-
check_current_thread_canceled()
54+
check_current_task_canceled()
5555

5656
if isinstance(result, BaseException):
5757
if not isinstance(result, CancelledError):
@@ -73,7 +73,7 @@ def _code_lens_resolve(self, params: CodeLens, *args: Any, **kwargs: Any) -> Cod
7373
results: List[CodeLens] = []
7474

7575
for result in self.resolve(self, params):
76-
check_current_thread_canceled()
76+
check_current_task_canceled()
7777

7878
if isinstance(result, BaseException):
7979
if not isinstance(result, CancelledError):
@@ -92,7 +92,7 @@ def refresh(self, now: bool = True) -> None:
9292
if self.refresh_task is not None and not self.refresh_task.done():
9393
self.refresh_task.cancel()
9494

95-
self.refresh_task = run_in_thread(self._refresh, now)
95+
self.refresh_task = run_as_task(self._refresh, now)
9696

9797
def _refresh(self, now: bool = True) -> None:
9898
if (
@@ -102,6 +102,6 @@ def _refresh(self, now: bool = True) -> None:
102102
and self.parent.client_capabilities.workspace.code_lens.refresh_support
103103
):
104104
if not now:
105-
check_current_thread_canceled(1)
105+
check_current_task_canceled(1)
106106

107107
self.parent.send_request("workspace/codeLens/refresh").result(self._refresh_timeout)

packages/language_server/src/robotcode/language_server/common/parts/completion.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from itertools import chain
33
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union, cast
44

5-
from robotcode.core.concurrent import check_current_thread_canceled
5+
from robotcode.core.concurrent import check_current_task_canceled
66
from robotcode.core.event import event
77
from robotcode.core.lsp.types import (
88
CompletionContext,
@@ -96,7 +96,7 @@ def _text_document_completion(
9696
results: List[Union[List[CompletionItem], CompletionList]] = []
9797

9898
if context is not None and context.trigger_kind == CompletionTriggerKind.TRIGGER_CHARACTER:
99-
check_current_thread_canceled(0.25)
99+
check_current_task_canceled(0.25)
100100

101101
document = self.parent.documents.get(text_document.uri)
102102
if document is None:
@@ -111,7 +111,7 @@ def _text_document_completion(
111111
context,
112112
callback_filter=language_id_filter(document),
113113
):
114-
check_current_thread_canceled()
114+
check_current_task_canceled()
115115

116116
if isinstance(result, BaseException):
117117
if not isinstance(result, CancelledError):
@@ -124,7 +124,7 @@ def _text_document_completion(
124124
return None
125125

126126
for result in results:
127-
check_current_thread_canceled()
127+
check_current_task_canceled()
128128

129129
if isinstance(result, CompletionList):
130130
for item in result.items:

packages/language_server/src/robotcode/language_server/common/parts/declaration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from asyncio import CancelledError
22
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union
33

4-
from robotcode.core.concurrent import check_current_thread_canceled
4+
from robotcode.core.concurrent import check_current_task_canceled
55
from robotcode.core.event import event
66
from robotcode.core.lsp.types import (
77
DeclarationParams,
@@ -70,7 +70,7 @@ def _text_document_declaration(
7070
position,
7171
callback_filter=language_id_filter(document),
7272
):
73-
check_current_thread_canceled()
73+
check_current_task_canceled()
7474

7575
if isinstance(result, BaseException):
7676
if not isinstance(result, CancelledError):

packages/language_server/src/robotcode/language_server/common/parts/definition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from concurrent.futures import CancelledError
22
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union
33

4-
from robotcode.core.concurrent import check_current_thread_canceled
4+
from robotcode.core.concurrent import check_current_task_canceled
55
from robotcode.core.event import event
66
from robotcode.core.lsp.types import (
77
DefinitionParams,
@@ -70,7 +70,7 @@ def _text_document_definition(
7070
document.position_from_utf16(position),
7171
callback_filter=language_id_filter(document),
7272
):
73-
check_current_thread_canceled()
73+
check_current_task_canceled()
7474

7575
if isinstance(result, BaseException):
7676
if not isinstance(result, CancelledError):

0 commit comments

Comments
 (0)