1
- from __future__ import annotations
2
-
3
- import asyncio
4
- from asyncio import CancelledError
1
+ from concurrent .futures import CancelledError
5
2
from itertools import chain
6
3
from typing import TYPE_CHECKING , Any , Final , List , Optional , Union , cast
7
4
8
- from robotcode .core .async_tools import async_tasking_event
5
+ from robotcode .core .event import event
9
6
from robotcode .core .lsp .types import (
10
7
CompletionContext ,
11
8
CompletionItem ,
21
18
TextEdit ,
22
19
)
23
20
from robotcode .core .utils .logging import LoggingDescriptor
24
- from robotcode .core .utils .threading import threaded
21
+ from robotcode .core .utils .threading import check_thread_canceled , threaded
25
22
from robotcode .jsonrpc2 .protocol import rpc_method
26
23
from robotcode .language_server .common .decorators import (
27
24
ALL_COMMIT_CHARACTERS_ATTR ,
40
37
class CompletionProtocolPart (LanguageServerProtocolPart ):
41
38
_logger : Final = LoggingDescriptor ()
42
39
43
- def __init__ (self , parent : LanguageServerProtocol ) -> None :
40
+ def __init__ (self , parent : " LanguageServerProtocol" ) -> None :
44
41
super ().__init__ (parent )
45
42
46
- @async_tasking_event
47
- async def collect (
43
+ @event
44
+ def collect (
48
45
sender , document : TextDocument , position : Position , context : Optional [CompletionContext ] # NOSONAR
49
46
) -> Union [List [CompletionItem ], CompletionList , None ]:
50
47
...
51
48
52
- @async_tasking_event
53
- async def resolve (sender , completion_item : CompletionItem ) -> Optional [CompletionItem ]: # NOSONAR
49
+ @event
50
+ def resolve (sender , completion_item : CompletionItem ) -> Optional [CompletionItem ]: # NOSONAR
54
51
...
55
52
56
53
def extend_capabilities (self , capabilities : ServerCapabilities ) -> None :
@@ -84,7 +81,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
84
81
85
82
@rpc_method (name = "textDocument/completion" , param_type = CompletionParams )
86
83
@threaded
87
- async def _text_document_completion (
84
+ def _text_document_completion (
88
85
self ,
89
86
text_document : TextDocumentIdentifier ,
90
87
position : Position ,
@@ -95,21 +92,17 @@ async def _text_document_completion(
95
92
results : List [Union [List [CompletionItem ], CompletionList ]] = []
96
93
97
94
if context is not None and context .trigger_kind == CompletionTriggerKind .TRIGGER_CHARACTER :
98
- await asyncio . sleep (0.25 )
95
+ check_thread_canceled (0.25 )
99
96
100
97
document = self .parent .documents .get (text_document .uri )
101
98
if document is None :
102
99
return None
103
100
104
101
p = document .position_from_utf16 (position )
105
102
106
- for result in await self .collect (
107
- self ,
108
- document ,
109
- p ,
110
- context ,
111
- callback_filter = language_id_filter (document ),
112
- ):
103
+ for result in self .collect (self , document , p , context , callback_filter = language_id_filter (document )):
104
+ check_thread_canceled ()
105
+
113
106
if isinstance (result , BaseException ):
114
107
if not isinstance (result , CancelledError ):
115
108
self ._logger .exception (result , exc_info = result )
@@ -121,6 +114,8 @@ async def _text_document_completion(
121
114
return None
122
115
123
116
for result in results :
117
+ check_thread_canceled ()
118
+
124
119
if isinstance (result , CompletionList ):
125
120
for item in result .items :
126
121
if item .text_edit is not None :
@@ -155,15 +150,15 @@ def update_completion_item_to_utf16(self, document: TextDocument, item: Completi
155
150
156
151
@rpc_method (name = "completionItem/resolve" , param_type = CompletionItem )
157
152
@threaded
158
- async def _completion_item_resolve (
153
+ def _completion_item_resolve (
159
154
self ,
160
155
params : CompletionItem ,
161
156
* args : Any ,
162
157
** kwargs : Any ,
163
158
) -> CompletionItem :
164
159
results : List [CompletionItem ] = []
165
160
166
- for result in await self .resolve (self , params ):
161
+ for result in self .resolve (self , params ):
167
162
if isinstance (result , BaseException ):
168
163
if not isinstance (result , CancelledError ):
169
164
self ._logger .exception (result , exc_info = result )
0 commit comments