Skip to content

Commit 204624c

Browse files
committed
refactor(langserver): remove async code from code lens
1 parent faee359 commit 204624c

File tree

8 files changed

+105
-123
lines changed

8 files changed

+105
-123
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import asyncio
4-
import concurrent.futures
54
import inspect
65
import json
76
import threading
@@ -21,6 +20,7 @@
2120
Union,
2221
)
2322

23+
from robotcode.core.concurrent import FutureEx
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
@@ -293,12 +293,8 @@ def send_response(
293293
)
294294

295295
@_logger.call
296-
def send_request(
297-
self,
298-
request: Request,
299-
return_type: Optional[Type[TResult]] = None,
300-
) -> concurrent.futures.Future[TResult]:
301-
result: concurrent.futures.Future[TResult] = concurrent.futures.Future()
296+
def send_request(self, request: Request, return_type: Optional[Type[TResult]] = None) -> FutureEx[TResult]:
297+
result: FutureEx[TResult] = FutureEx()
302298

303299
with self._sended_request_lock:
304300
self._sended_request[request.seq] = SendedRequestEntry(result, return_type)
@@ -309,9 +305,7 @@ def send_request(
309305

310306
@_logger.call
311307
def send_request_async(
312-
self,
313-
request: Request,
314-
return_type: Optional[Type[TResult]] = None,
308+
self, request: Request, return_type: Optional[Type[TResult]] = None
315309
) -> asyncio.Future[TResult]:
316310
return asyncio.wrap_future(self.send_request(request, return_type))
317311

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import asyncio
4-
import concurrent.futures
54
import functools
65
import inspect
76
import json
@@ -38,7 +37,7 @@
3837
create_sub_task,
3938
run_coroutine_in_thread,
4039
)
41-
from robotcode.core.concurrent import is_threaded_callable, run_in_thread
40+
from robotcode.core.concurrent import FutureEx, is_threaded_callable, run_in_thread
4241
from robotcode.core.utils.dataclasses import as_json, from_dict
4342
from robotcode.core.utils.inspect import ensure_coroutine, iter_methods
4443
from robotcode.core.utils.logging import LoggingDescriptor
@@ -344,7 +343,7 @@ def get_param_type(self, name: str) -> Optional[Type[Any]]:
344343

345344

346345
class SendedRequestEntry:
347-
def __init__(self, future: concurrent.futures.Future[Any], result_type: Optional[Type[Any]]) -> None:
346+
def __init__(self, future: FutureEx[Any], result_type: Optional[Type[Any]]) -> None:
348347
self.future = future
349348
self.result_type = result_type
350349

@@ -563,8 +562,8 @@ def send_request(
563562
method: str,
564563
params: Optional[Any] = None,
565564
return_type_or_converter: Optional[Type[_TResult]] = None,
566-
) -> concurrent.futures.Future[_TResult]:
567-
result: concurrent.futures.Future[_TResult] = concurrent.futures.Future()
565+
) -> FutureEx[_TResult]:
566+
result: FutureEx[_TResult] = FutureEx()
568567

569568
with self._sended_request_lock:
570569
self._sended_request_count += 1
Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from __future__ import annotations
2-
3-
import asyncio
4-
from asyncio import CancelledError
1+
from concurrent.futures import CancelledError
52
from typing import TYPE_CHECKING, Any, Final, List, Optional
63

7-
from robotcode.core.async_tools import async_tasking_event, create_sub_task
8-
from robotcode.core.concurrent import threaded
4+
from robotcode.core.concurrent import FutureEx, check_current_thread_canceled, threaded
5+
from robotcode.core.event import event
96
from robotcode.core.lsp.types import (
107
CodeLens,
118
CodeLensOptions,
@@ -27,16 +24,17 @@
2724
class CodeLensProtocolPart(LanguageServerProtocolPart):
2825
_logger: Final = LoggingDescriptor()
2926

30-
def __init__(self, parent: LanguageServerProtocol) -> None:
27+
def __init__(self, parent: "LanguageServerProtocol") -> None:
3128
super().__init__(parent)
32-
self.refresh_task: Optional[asyncio.Task[Any]] = None
29+
self.refresh_task: Optional[FutureEx[Any]] = None
30+
self._refresh_timeout = 5
3331

34-
@async_tasking_event
35-
async def collect(sender, document: TextDocument) -> Optional[List[CodeLens]]: # NOSONAR
32+
@event
33+
def collect(sender, document: TextDocument) -> Optional[List[CodeLens]]: # NOSONAR
3634
...
3735

38-
@async_tasking_event
39-
async def resolve(sender, code_lens: CodeLens) -> Optional[CodeLens]: # NOSONAR
36+
@event
37+
def resolve(sender, code_lens: CodeLens) -> Optional[CodeLens]: # NOSONAR
4038
...
4139

4240
def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
@@ -45,15 +43,17 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
4543

4644
@rpc_method(name="textDocument/codeLens", param_type=CodeLensParams)
4745
@threaded
48-
async def _text_document_code_lens(
46+
def _text_document_code_lens(
4947
self, text_document: TextDocumentIdentifier, *args: Any, **kwargs: Any
5048
) -> Optional[List[CodeLens]]:
5149
results: List[CodeLens] = []
5250
document = self.parent.documents.get(text_document.uri)
5351
if document is None:
5452
return None
5553

56-
for result in await self.collect(self, document, callback_filter=language_id_filter(document)):
54+
for result in self.collect(self, document, callback_filter=language_id_filter(document)):
55+
check_current_thread_canceled()
56+
5757
if isinstance(result, BaseException):
5858
if not isinstance(result, CancelledError):
5959
self._logger.exception(result, exc_info=result)
@@ -64,17 +64,19 @@ async def _text_document_code_lens(
6464
if not results:
6565
return None
6666

67-
for result in results:
68-
result.range = document.range_to_utf16(result.range)
67+
for r in results:
68+
r.range = document.range_to_utf16(r.range)
6969

7070
return results
7171

7272
@rpc_method(name="codeLens/resolve", param_type=CodeLens)
7373
@threaded
74-
async def _code_lens_resolve(self, params: CodeLens, *args: Any, **kwargs: Any) -> CodeLens:
74+
def _code_lens_resolve(self, params: CodeLens, *args: Any, **kwargs: Any) -> CodeLens:
7575
results: List[CodeLens] = []
7676

77-
for result in await self.resolve(self, params):
77+
for result in self.resolve(self, params):
78+
check_current_thread_canceled()
79+
7880
if isinstance(result, BaseException):
7981
if not isinstance(result, CancelledError):
8082
self._logger.exception(result, exc_info=result)
@@ -88,19 +90,7 @@ async def _code_lens_resolve(self, params: CodeLens, *args: Any, **kwargs: Any)
8890

8991
return params
9092

91-
async def __do_refresh(self, now: bool = False) -> None:
92-
if not now:
93-
await asyncio.sleep(1)
94-
95-
await self.__refresh()
96-
97-
async def refresh(self, now: bool = False) -> None:
98-
if self.refresh_task is not None and not self.refresh_task.done():
99-
self.refresh_task.get_loop().call_soon_threadsafe(self.refresh_task.cancel)
100-
101-
self.refresh_task = create_sub_task(self.__do_refresh(now), loop=self.parent.diagnostics.diagnostics_loop)
102-
103-
async def __refresh(self) -> None:
93+
def refresh(self) -> None:
10494
if not (
10595
self.parent.client_capabilities is not None
10696
and self.parent.client_capabilities.workspace is not None
@@ -109,4 +99,4 @@ async def __refresh(self) -> None:
10999
):
110100
return
111101

112-
await self.parent.send_request_async("workspace/codeLens/refresh")
102+
self.parent.send_request("workspace/codeLens/refresh").result(self._refresh_timeout)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
create_sub_task,
1919
)
2020
from robotcode.core.concurrent import threaded
21+
from robotcode.core.event import event
2122
from robotcode.core.lsp.types import (
2223
Diagnostic,
2324
DiagnosticOptions,
@@ -241,8 +242,8 @@ async def collect(sender, document: TextDocument) -> Optional[DiagnosticsResult]
241242
async def load_workspace_documents(sender) -> Optional[List[WorkspaceDocumentsResult]]: # NOSONAR
242243
...
243244

244-
@async_tasking_event
245-
async def on_workspace_loaded(sender) -> None: # NOSONAR
245+
@event
246+
def on_workspace_loaded(sender) -> None: # NOSONAR
246247
...
247248

248249
@async_event
@@ -262,7 +263,7 @@ async def ensure_workspace_loaded(self) -> None:
262263
finally:
263264
self._workspace_loaded = True
264265
self.workspace_loaded_event.set()
265-
await self.on_workspace_loaded(self)
266+
self.on_workspace_loaded(self)
266267
await self.force_refresh_all()
267268

268269
async def force_refresh_all(self, refresh: bool = True) -> None:

0 commit comments

Comments
 (0)