Skip to content

Commit 87cb416

Browse files
committed
refactor(langserver): remove async code from code actions
1 parent 135b0d4 commit 87cb416

File tree

7 files changed

+69
-104
lines changed

7 files changed

+69
-104
lines changed

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from __future__ import annotations
2-
3-
from asyncio import CancelledError
1+
from concurrent.futures import CancelledError
42
from itertools import chain
53
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union, cast
64

7-
from robotcode.core.async_tools import async_tasking_event
5+
from robotcode.core.event import event
86
from robotcode.core.lsp.types import (
97
CodeAction,
108
CodeActionContext,
@@ -16,7 +14,7 @@
1614
TextDocumentIdentifier,
1715
)
1816
from robotcode.core.utils.logging import LoggingDescriptor
19-
from robotcode.core.utils.threading import threaded
17+
from robotcode.core.utils.threading import check_thread_canceled, threaded
2018
from robotcode.jsonrpc2.protocol import rpc_method
2119
from robotcode.language_server.common.decorators import CODE_ACTION_KINDS_ATTR, HasCodeActionKinds, language_id_filter
2220
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
@@ -29,17 +27,17 @@
2927
class CodeActionProtocolPart(LanguageServerProtocolPart):
3028
_logger: Final = LoggingDescriptor()
3129

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

35-
@async_tasking_event
36-
async def collect(
33+
@event
34+
def collect(
3735
sender, document: TextDocument, range: Range, context: CodeActionContext # NOSONAR
3836
) -> Optional[List[Union[Command, CodeAction]]]:
3937
...
4038

41-
@async_tasking_event
42-
async def resolve(sender, code_action: CodeAction) -> Optional[CodeAction]: # NOSONAR
39+
@event
40+
def resolve(sender, code_action: CodeAction) -> Optional[CodeAction]: # NOSONAR
4341
...
4442

4543
def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
@@ -61,7 +59,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
6159

6260
@rpc_method(name="textDocument/codeAction", param_type=CodeActionParams)
6361
@threaded
64-
async def _text_document_code_action(
62+
def _text_document_code_action(
6563
self,
6664
text_document: TextDocumentIdentifier,
6765
range: Range,
@@ -81,13 +79,11 @@ async def _text_document_code_action(
8179
for r in c.related_information:
8280
r.location.range = document.range_from_utf16(r.location.range)
8381

84-
for result in await self.collect(
85-
self,
86-
document,
87-
document.range_from_utf16(range),
88-
context,
89-
callback_filter=language_id_filter(document),
82+
for result in self.collect(
83+
self, document, document.range_from_utf16(range), context, callback_filter=language_id_filter(document)
9084
):
85+
check_thread_canceled()
86+
9187
if isinstance(result, BaseException):
9288
if not isinstance(result, CancelledError):
9389
self._logger.exception(result, exc_info=result)
@@ -102,15 +98,17 @@ async def _text_document_code_action(
10298

10399
@rpc_method(name="codeAction/resolve", param_type=CodeAction)
104100
@threaded
105-
async def _text_document_code_action_resolve(
101+
def _text_document_code_action_resolve(
106102
self,
107103
params: CodeAction,
108104
*args: Any,
109105
**kwargs: Any,
110106
) -> CodeAction:
111107
results: List[CodeAction] = []
112108

113-
for result in await self.resolve(self, params):
109+
for result in self.resolve(self, params):
110+
check_thread_canceled()
111+
114112
if isinstance(result, BaseException):
115113
if not isinstance(result, CancelledError):
116114
self._logger.exception(result, exc_info=result)

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def _ensure_http_server_started(self) -> None:
243243
]
244244
)
245245
@_logger.call
246-
async def collect(
246+
def collect(
247247
self,
248248
sender: Any,
249249
document: TextDocument,
@@ -270,11 +270,8 @@ async def collect(
270270
if CodeActionKind.SOURCE.value in context.only and range in range_from_token(
271271
node.get_token(RobotToken.NAME)
272272
):
273-
url = await self.build_url(
274-
node.name,
275-
node.args if isinstance(node, LibraryImport) else (),
276-
document,
277-
namespace,
273+
url = self.build_url(
274+
node.name, node.args if isinstance(node, LibraryImport) else (), document, namespace
278275
)
279276

280277
return [self.open_documentation_code_action(url)]
@@ -338,26 +335,14 @@ async def collect(
338335
if entry is None:
339336
return None
340337

341-
url = await self.build_url(
342-
entry.import_name,
343-
entry.args,
344-
document,
345-
namespace,
346-
kw_doc.name,
347-
)
338+
url = self.build_url(entry.import_name, entry.args, document, namespace, kw_doc.name)
348339

349340
return [self.open_documentation_code_action(url)]
350341

351342
if isinstance(node, KeywordName):
352343
name_token = node.get_token(RobotToken.KEYWORD_NAME)
353344
if name_token is not None and range in range_from_token(name_token):
354-
url = await self.build_url(
355-
str(document.uri.to_path().name),
356-
(),
357-
document,
358-
namespace,
359-
name_token.value,
360-
)
345+
url = self.build_url(str(document.uri.to_path().name), (), document, namespace, name_token.value)
361346

362347
return [self.open_documentation_code_action(url)]
363348

@@ -374,7 +359,7 @@ def open_documentation_code_action(self, url: str) -> CodeAction:
374359
),
375360
)
376361

377-
async def build_url(
362+
def build_url(
378363
self,
379364
name: str,
380365
args: Tuple[Any, ...],
@@ -423,7 +408,7 @@ async def build_url(
423408

424409
@rpc_method(name="robot/documentationServer/convertUri", param_type=ConvertUriParams)
425410
@threaded
426-
async def _convert_uri(self, uri: str, *args: Any, **kwargs: Any) -> Optional[str]:
411+
def _convert_uri(self, uri: str, *args: Any, **kwargs: Any) -> Optional[str]:
427412
real_uri = Uri(uri)
428413

429414
folder = self.parent.workspace.get_workspace_folder(real_uri)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def find_keyword_sections(node: ast.AST) -> Optional[List[ast.AST]]:
6262

6363

6464
class CodeActionHelperMixin:
65-
async def create_insert_keyword_workspace_edit(
65+
def create_insert_keyword_workspace_edit(
6666
self, document: TextDocument, model: ast.AST, namespace: Namespace, insert_text: str
6767
) -> Tuple[str, Range]:
6868
keyword_sections = find_keyword_sections(model)

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

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from collections import defaultdict
42
from dataclasses import dataclass
53
from string import Template as StringTemplate
@@ -91,7 +89,7 @@ class CodeActionData(CodeActionDataBase):
9189
class RobotCodeActionQuickFixesProtocolPart(RobotLanguageServerProtocolPart, ModelHelperMixin, CodeActionHelperMixin):
9290
_logger = LoggingDescriptor()
9391

94-
def __init__(self, parent: RobotLanguageServerProtocol) -> None:
92+
def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
9593
super().__init__(parent)
9694

9795
parent.code_action.collect.add(self.collect)
@@ -101,12 +99,12 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
10199

102100
@language_id("robotframework")
103101
@code_action_kinds([CodeActionKind.QUICK_FIX])
104-
async def collect(
102+
def collect(
105103
self, sender: Any, document: TextDocument, range: Range, context: CodeActionContext
106104
) -> Optional[List[Union[Command, CodeAction]]]:
107105
result = []
108106
for method in iter_methods(self, lambda m: m.__name__.startswith("code_action_")):
109-
code_actions = await method(document, range, context)
107+
code_actions = method(document, range, context)
110108
if code_actions:
111109
result.extend(code_actions)
112110

@@ -115,17 +113,17 @@ async def collect(
115113

116114
return None
117115

118-
async def resolve(self, sender: Any, code_action: CodeAction) -> Optional[CodeAction]:
116+
def resolve(self, sender: Any, code_action: CodeAction) -> Optional[CodeAction]:
119117
if code_action.data is not None and isinstance(code_action.data, Mapping):
120118
type = code_action.data.get("type", None)
121119
if type == "quickfix":
122120
method_name = code_action.data.get("method")
123121
method = next(iter_methods(self, lambda m: m.__name__ == f"resolve_code_action_{method_name}"))
124-
await method(code_action, data=from_dict(code_action.data, CodeActionData))
122+
method(code_action, data=from_dict(code_action.data, CodeActionData))
125123

126124
return None
127125

128-
async def code_action_create_keyword(
126+
def code_action_create_keyword(
129127
self, document: TextDocument, range: Range, context: CodeActionContext
130128
) -> Optional[List[Union[Command, CodeAction]]]:
131129
result: List[Union[Command, CodeAction]] = []
@@ -184,9 +182,7 @@ async def code_action_create_keyword(
184182

185183
return result if result else None
186184

187-
async def resolve_code_action_create_keyword(
188-
self, code_action: CodeAction, data: CodeActionData
189-
) -> Optional[CodeAction]:
185+
def resolve_code_action_create_keyword(self, code_action: CodeAction, data: CodeActionData) -> Optional[CodeAction]:
190186
document = self.parent.documents.get(data.document_uri)
191187
if document is None:
192188
return None
@@ -240,7 +236,7 @@ async def resolve_code_action_create_keyword(
240236
else:
241237
dest_document = document
242238

243-
code_action.edit, select_range = await self._apply_create_keyword(dest_document, insert_text)
239+
code_action.edit, select_range = self._apply_create_keyword(dest_document, insert_text)
244240

245241
code_action.command = Command(
246242
SHOW_DOCUMENT_SELECT_AND_RENAME_COMMAND,
@@ -251,13 +247,11 @@ async def resolve_code_action_create_keyword(
251247

252248
return None
253249

254-
async def _apply_create_keyword(self, document: TextDocument, insert_text: str) -> Tuple[WorkspaceEdit, Range]:
250+
def _apply_create_keyword(self, document: TextDocument, insert_text: str) -> Tuple[WorkspaceEdit, Range]:
255251
model = self.parent.documents_cache.get_model(document, False)
256252
namespace = self.parent.documents_cache.get_namespace(document)
257253

258-
insert_text, insert_range = await self.create_insert_keyword_workspace_edit(
259-
document, model, namespace, insert_text
260-
)
254+
insert_text, insert_range = self.create_insert_keyword_workspace_edit(document, model, namespace, insert_text)
261255

262256
we = WorkspaceEdit(
263257
document_changes=[
@@ -276,7 +270,7 @@ async def _apply_create_keyword(self, document: TextDocument, insert_text: str)
276270

277271
return we, selection_range
278272

279-
async def code_action_disable_robotcode_diagnostics_for_line(
273+
def code_action_disable_robotcode_diagnostics_for_line(
280274
self, document: TextDocument, range: Range, context: CodeActionContext
281275
) -> Optional[List[Union[Command, CodeAction]]]:
282276
if (
@@ -313,7 +307,7 @@ async def code_action_disable_robotcode_diagnostics_for_line(
313307

314308
return None
315309

316-
async def resolve_code_action_disable_robotcode_diagnostics_for_line(
310+
def resolve_code_action_disable_robotcode_diagnostics_for_line(
317311
self, code_action: CodeAction, data: CodeActionData
318312
) -> Optional[CodeAction]:
319313
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
@@ -350,7 +344,7 @@ async def resolve_code_action_disable_robotcode_diagnostics_for_line(
350344

351345
return None
352346

353-
async def code_action_create_local_variable(
347+
def code_action_create_local_variable(
354348
self, document: TextDocument, range: Range, context: CodeActionContext
355349
) -> Optional[List[Union[Command, CodeAction]]]:
356350
result: List[Union[Command, CodeAction]] = []
@@ -405,7 +399,7 @@ async def code_action_create_local_variable(
405399

406400
return result if result else None
407401

408-
async def resolve_code_action_create_local_variable(
402+
def resolve_code_action_create_local_variable(
409403
self, code_action: CodeAction, data: CodeActionData
410404
) -> Optional[CodeAction]:
411405
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
@@ -457,7 +451,7 @@ async def resolve_code_action_create_local_variable(
457451

458452
return None
459453

460-
async def code_action_create_suite_variable(
454+
def code_action_create_suite_variable(
461455
self, document: TextDocument, range: Range, context: CodeActionContext
462456
) -> Optional[List[Union[Command, CodeAction]]]:
463457
result: List[Union[Command, CodeAction]] = []
@@ -499,7 +493,7 @@ async def code_action_create_suite_variable(
499493

500494
return result if result else None
501495

502-
async def resolve_code_action_create_suite_variable(
496+
def resolve_code_action_create_suite_variable(
503497
self, code_action: CodeAction, data: CodeActionData
504498
) -> Optional[CodeAction]:
505499
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
@@ -589,7 +583,7 @@ async def resolve_code_action_create_suite_variable(
589583
return code_action
590584
return None
591585

592-
async def code_action_add_argument(
586+
def code_action_add_argument(
593587
self, document: TextDocument, range: Range, context: CodeActionContext
594588
) -> Optional[List[Union[Command, CodeAction]]]:
595589
result: List[Union[Command, CodeAction]] = []
@@ -638,9 +632,7 @@ async def code_action_add_argument(
638632

639633
return result if result else None
640634

641-
async def resolve_code_action_add_argument(
642-
self, code_action: CodeAction, data: CodeActionData
643-
) -> Optional[CodeAction]:
635+
def resolve_code_action_add_argument(self, code_action: CodeAction, data: CodeActionData) -> Optional[CodeAction]:
644636
if data.range.start.line == data.range.end.line and data.range.start.character <= data.range.end.character:
645637
document = self.parent.documents.get(data.document_uri)
646638
if document is None:

0 commit comments

Comments
 (0)