1
- from __future__ import annotations
2
-
3
- import asyncio
4
- from asyncio import CancelledError
1
+ from concurrent .futures import CancelledError
5
2
from typing import TYPE_CHECKING , Any , Final , List , Optional
6
3
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
9
6
from robotcode .core .lsp .types import (
10
7
CodeLens ,
11
8
CodeLensOptions ,
27
24
class CodeLensProtocolPart (LanguageServerProtocolPart ):
28
25
_logger : Final = LoggingDescriptor ()
29
26
30
- def __init__ (self , parent : LanguageServerProtocol ) -> None :
27
+ def __init__ (self , parent : " LanguageServerProtocol" ) -> None :
31
28
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
33
31
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
36
34
...
37
35
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
40
38
...
41
39
42
40
def extend_capabilities (self , capabilities : ServerCapabilities ) -> None :
@@ -45,15 +43,17 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
45
43
46
44
@rpc_method (name = "textDocument/codeLens" , param_type = CodeLensParams )
47
45
@threaded
48
- async def _text_document_code_lens (
46
+ def _text_document_code_lens (
49
47
self , text_document : TextDocumentIdentifier , * args : Any , ** kwargs : Any
50
48
) -> Optional [List [CodeLens ]]:
51
49
results : List [CodeLens ] = []
52
50
document = self .parent .documents .get (text_document .uri )
53
51
if document is None :
54
52
return None
55
53
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
+
57
57
if isinstance (result , BaseException ):
58
58
if not isinstance (result , CancelledError ):
59
59
self ._logger .exception (result , exc_info = result )
@@ -64,17 +64,19 @@ async def _text_document_code_lens(
64
64
if not results :
65
65
return None
66
66
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 )
69
69
70
70
return results
71
71
72
72
@rpc_method (name = "codeLens/resolve" , param_type = CodeLens )
73
73
@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 :
75
75
results : List [CodeLens ] = []
76
76
77
- for result in await self .resolve (self , params ):
77
+ for result in self .resolve (self , params ):
78
+ check_current_thread_canceled ()
79
+
78
80
if isinstance (result , BaseException ):
79
81
if not isinstance (result , CancelledError ):
80
82
self ._logger .exception (result , exc_info = result )
@@ -88,19 +90,7 @@ async def _code_lens_resolve(self, params: CodeLens, *args: Any, **kwargs: Any)
88
90
89
91
return params
90
92
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 :
104
94
if not (
105
95
self .parent .client_capabilities is not None
106
96
and self .parent .client_capabilities .workspace is not None
@@ -109,4 +99,4 @@ async def __refresh(self) -> None:
109
99
):
110
100
return
111
101
112
- await self .parent .send_request_async ("workspace/codeLens/refresh" )
102
+ self .parent .send_request ("workspace/codeLens/refresh" ). result ( self . _refresh_timeout )
0 commit comments