Skip to content

Commit 7535bfa

Browse files
committed
refactor(langserver): remove async code from inlay hints
1 parent 2f25c78 commit 7535bfa

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from concurrent.futures import CancelledError
42
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union
53

@@ -27,7 +25,7 @@
2725
class ImplementationProtocolPart(LanguageServerProtocolPart):
2826
_logger: Final = LoggingDescriptor()
2927

30-
def __init__(self, parent: LanguageServerProtocol) -> None:
28+
def __init__(self, parent: "LanguageServerProtocol") -> None:
3129
super().__init__(parent)
3230
self.link_support = False
3331

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

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

4-
from robotcode.core.async_tools import async_tasking_event
54
from robotcode.core.concurrent import threaded
5+
from robotcode.core.event import event
66
from robotcode.core.lsp.types import (
77
InlayHint,
88
InlayHintOptions,
@@ -28,12 +28,12 @@ class InlayHintProtocolPart(LanguageServerProtocolPart):
2828
def __init__(self, parent: "LanguageServerProtocol") -> None:
2929
super().__init__(parent)
3030

31-
@async_tasking_event
32-
async def collect(sender, document: TextDocument, range: Range) -> Optional[List[InlayHint]]: # NOSONAR
31+
@event
32+
def collect(sender, document: TextDocument, range: Range) -> Optional[List[InlayHint]]: # NOSONAR
3333
...
3434

35-
@async_tasking_event
36-
async def resolve(sender, hint: InlayHint) -> Optional[InlayHint]: # NOSONAR
35+
@event
36+
def resolve(sender, hint: InlayHint) -> Optional[InlayHint]: # NOSONAR
3737
...
3838

3939
def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
@@ -45,7 +45,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
4545

4646
@rpc_method(name="textDocument/inlayHint", param_type=InlayHintParams)
4747
@threaded
48-
async def _text_document_inlay_hint(
48+
def _text_document_inlay_hint(
4949
self,
5050
text_document: TextDocumentIdentifier,
5151
range: Range,
@@ -58,7 +58,7 @@ async def _text_document_inlay_hint(
5858
if document is None:
5959
return None
6060

61-
for result in await self.collect(
61+
for result in self.collect(
6262
self,
6363
document,
6464
document.range_from_utf16(range),
@@ -72,8 +72,8 @@ async def _text_document_inlay_hint(
7272
results.extend(result)
7373

7474
if results:
75-
for result in results:
76-
result.position = document.position_to_utf16(result.position)
75+
for r in results:
76+
r.position = document.position_to_utf16(r.position)
7777
# TODO: resolve
7878

7979
return results
@@ -82,13 +82,13 @@ async def _text_document_inlay_hint(
8282

8383
@rpc_method(name="inlayHint/resolve", param_type=InlayHint)
8484
@threaded
85-
async def _inlay_hint_resolve(
85+
def _inlay_hint_resolve(
8686
self,
8787
params: InlayHint,
8888
*args: Any,
8989
**kwargs: Any,
9090
) -> Optional[InlayHint]:
91-
for result in await self.resolve(self, params):
91+
for result in self.resolve(self, params):
9292
if isinstance(result, BaseException):
9393
if not isinstance(result, CancelledError):
9494
self._logger.exception(result, exc_info=result)
@@ -98,11 +98,11 @@ async def _inlay_hint_resolve(
9898

9999
return params
100100

101-
async def refresh(self) -> None:
101+
def refresh(self) -> None:
102102
if (
103103
self.parent.client_capabilities is not None
104104
and self.parent.client_capabilities.workspace is not None
105105
and self.parent.client_capabilities.workspace.inlay_hint is not None
106106
and self.parent.client_capabilities.workspace.inlay_hint.refresh_support
107107
):
108-
await self.parent.send_request_async("workspace/inlayHint/refresh")
108+
self.parent.send_request("workspace/inlayHint/refresh").result()

packages/language_server/src/robotcode/language_server/robotframework/parts/inlay_hint.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from __future__ import annotations
2-
31
import ast
4-
import asyncio
5-
from typing import TYPE_CHECKING, Any, Awaitable, Callable, List, Optional, Type, cast
2+
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Type, cast
63

74
from robot.parsing.lexer.tokens import Token
5+
from robotcode.core.concurrent import check_current_thread_canceled
86
from robotcode.core.lsp.types import InlayHint, InlayHintKind, Range
97
from robotcode.core.utils.logging import LoggingDescriptor
108
from robotcode.robot.diagnostics.library_doc import KeywordArgumentKind, KeywordDoc, LibraryDoc
@@ -22,24 +20,24 @@
2220
from .protocol_part import RobotLanguageServerProtocolPart
2321

2422
_HandlerMethod = Callable[
25-
[TextDocument, Range, ast.AST, ast.AST, Namespace, InlayHintsConfig], Awaitable[Optional[List[InlayHint]]]
23+
[TextDocument, Range, ast.AST, ast.AST, Namespace, InlayHintsConfig], Optional[List[InlayHint]]
2624
]
2725

2826

2927
class RobotInlayHintProtocolPart(RobotLanguageServerProtocolPart, ModelHelperMixin):
3028
_logger = LoggingDescriptor()
3129

32-
def __init__(self, parent: RobotLanguageServerProtocol) -> None:
30+
def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
3331
super().__init__(parent)
3432

3533
parent.inlay_hint.collect.add(self.collect)
3634

37-
async def get_config(self, document: TextDocument) -> Optional[InlayHintsConfig]:
35+
def get_config(self, document: TextDocument) -> Optional[InlayHintsConfig]:
3836
folder = self.parent.workspace.get_workspace_folder(document.uri)
3937
if folder is None:
4038
return None
4139

42-
return await self.parent.workspace.get_configuration_async(InlayHintsConfig, folder.uri)
40+
return self.parent.workspace.get_configuration(InlayHintsConfig, folder.uri)
4341

4442
def _find_method(self, cls: Type[Any]) -> Optional[_HandlerMethod]:
4543
if cls is ast.AST:
@@ -58,8 +56,8 @@ def _find_method(self, cls: Type[Any]) -> Optional[_HandlerMethod]:
5856

5957
@language_id("robotframework")
6058
@_logger.call
61-
async def collect(self, sender: Any, document: TextDocument, range: Range) -> Optional[List[InlayHint]]:
62-
config = await self.get_config(document)
59+
def collect(self, sender: Any, document: TextDocument, range: Range) -> Optional[List[InlayHint]]:
60+
config = self.get_config(document)
6361
if config is None or not config.parameter_names and not config.namespaces:
6462
return None
6563

@@ -69,6 +67,8 @@ async def collect(self, sender: Any, document: TextDocument, range: Range) -> Op
6967
result: List[InlayHint] = []
7068

7169
for node in iter_nodes(model):
70+
check_current_thread_canceled()
71+
7272
node_range = range_from_node(node)
7373
if node_range.end < range.start:
7474
continue
@@ -78,13 +78,13 @@ async def collect(self, sender: Any, document: TextDocument, range: Range) -> Op
7878

7979
method = self._find_method(type(node))
8080
if method is not None:
81-
r = await method(document, range, node, model, namespace, config)
81+
r = method(document, range, node, model, namespace, config)
8282
if r is not None:
8383
result.extend(r)
8484

8585
return result
8686

87-
async def _handle_keywordcall_fixture_template(
87+
def _handle_keywordcall_fixture_template(
8888
self,
8989
keyword_token: Token,
9090
arguments: List[Token],
@@ -107,9 +107,9 @@ async def _handle_keywordcall_fixture_template(
107107
if kw_doc is None:
108108
return None
109109

110-
return await self._get_inlay_hint(keyword_token, kw_doc, arguments, namespace, config)
110+
return self._get_inlay_hint(keyword_token, kw_doc, arguments, namespace, config)
111111

112-
async def _get_inlay_hint(
112+
def _get_inlay_hint(
113113
self,
114114
keyword_token: Optional[Token],
115115
kw_doc: KeywordDoc,
@@ -197,7 +197,7 @@ async def _get_inlay_hint(
197197

198198
return result
199199

200-
async def handle_KeywordCall( # noqa: N802
200+
def handle_KeywordCall( # noqa: N802
201201
self,
202202
document: TextDocument,
203203
range: Range,
@@ -215,9 +215,9 @@ async def handle_KeywordCall( # noqa: N802
215215
return None
216216

217217
arguments = keyword_call.get_tokens(RobotToken.ARGUMENT)
218-
return await self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)
218+
return self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)
219219

220-
async def handle_Fixture( # noqa: N802
220+
def handle_Fixture( # noqa: N802
221221
self,
222222
document: TextDocument,
223223
range: Range,
@@ -235,9 +235,9 @@ async def handle_Fixture( # noqa: N802
235235
return None
236236

237237
arguments = fixture.get_tokens(RobotToken.ARGUMENT)
238-
return await self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)
238+
return self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)
239239

240-
async def handle_TestTemplate( # noqa: N802
240+
def handle_TestTemplate( # noqa: N802
241241
self,
242242
document: TextDocument,
243243
range: Range,
@@ -254,9 +254,9 @@ async def handle_TestTemplate( # noqa: N802
254254
if keyword_token is None or not keyword_token.value:
255255
return None
256256

257-
return await self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)
257+
return self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)
258258

259-
async def handle_Template( # noqa: N802
259+
def handle_Template( # noqa: N802
260260
self,
261261
document: TextDocument,
262262
range: Range,
@@ -273,9 +273,9 @@ async def handle_Template( # noqa: N802
273273
if keyword_token is None or not keyword_token.value:
274274
return None
275275

276-
return await self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)
276+
return self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)
277277

278-
async def handle_LibraryImport( # noqa: N802
278+
def handle_LibraryImport( # noqa: N802
279279
self,
280280
document: TextDocument,
281281
range: Range,
@@ -306,19 +306,20 @@ async def handle_LibraryImport( # noqa: N802
306306
variables=namespace.get_resolvable_variables(),
307307
)
308308

309-
except (asyncio.CancelledError, SystemExit, KeyboardInterrupt):
309+
except (SystemExit, KeyboardInterrupt):
310310
raise
311-
except BaseException:
311+
except BaseException as e:
312+
self._logger.exception(e)
312313
return None
313314

314315
arguments = library_node.get_tokens(RobotToken.ARGUMENT)
315316

316317
for kw_doc in lib_doc.inits:
317-
return await self._get_inlay_hint(None, kw_doc, arguments, namespace, config)
318+
return self._get_inlay_hint(None, kw_doc, arguments, namespace, config)
318319

319320
return None
320321

321-
async def handle_VariablesImport( # noqa: N802
322+
def handle_VariablesImport( # noqa: N802
322323
self,
323324
document: TextDocument,
324325
range: Range,
@@ -352,14 +353,15 @@ async def handle_VariablesImport( # noqa: N802
352353
variables=namespace.get_resolvable_variables(),
353354
)
354355

355-
except (asyncio.CancelledError, SystemExit, KeyboardInterrupt):
356+
except (SystemExit, KeyboardInterrupt):
356357
raise
357-
except BaseException:
358+
except BaseException as e:
359+
self._logger.exception(e)
358360
return None
359361

360362
arguments = library_node.get_tokens(RobotToken.ARGUMENT)
361363

362364
for kw_doc in lib_doc.inits:
363-
return await self._get_inlay_hint(None, kw_doc, arguments, namespace, config)
365+
return self._get_inlay_hint(None, kw_doc, arguments, namespace, config)
364366

365367
return None

0 commit comments

Comments
 (0)