Skip to content

Commit 2f25c78

Browse files
committed
refactor(langserver): remove async code from formatting and some import corrections
1 parent 58f185b commit 2f25c78

File tree

5 files changed

+35
-42
lines changed

5 files changed

+35
-42
lines changed

packages/language_server/src/robotcode/language_server/common/parts/declaration.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 asyncio import CancelledError
42
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union
53

@@ -26,7 +24,7 @@
2624
class DeclarationProtocolPart(LanguageServerProtocolPart):
2725
_logger: Final = LoggingDescriptor()
2826

29-
def __init__(self, parent: LanguageServerProtocol) -> None:
27+
def __init__(self, parent: "LanguageServerProtocol") -> None:
3028
super().__init__(parent)
3129
self.link_support = False
3230

packages/language_server/src/robotcode/language_server/common/parts/definition.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

@@ -26,7 +24,7 @@
2624
class DefinitionProtocolPart(LanguageServerProtocolPart):
2725
_logger: Final = LoggingDescriptor()
2826

29-
def __init__(self, parent: LanguageServerProtocol) -> None:
27+
def __init__(self, parent: "LanguageServerProtocol") -> None:
3028
super().__init__(parent)
3129
self.link_support = False
3230

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

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

3-
import asyncio
43
import os
54
import re
65
import threading
@@ -148,7 +147,7 @@ def get_or_open_document(
148147
text=self.read_document_text(uri, language_id),
149148
version=version,
150149
)
151-
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
150+
except (SystemExit, KeyboardInterrupt):
152151
raise
153152
except BaseException as e:
154153
raise CantReadDocumentError(f"Error reading document '{path}': {e!s}") from e

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

Lines changed: 16 additions & 14 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 typing import TYPE_CHECKING, Any, Final, List, Optional
53

6-
from robotcode.core.async_tools import async_tasking_event
7-
from robotcode.core.concurrent import threaded
4+
from robotcode.core.concurrent import check_current_thread_canceled, threaded
5+
from robotcode.core.event import event
86
from robotcode.core.lsp.types import (
97
DocumentFormattingOptions,
108
DocumentFormattingParams,
@@ -32,20 +30,20 @@
3230
class FormattingProtocolPart(LanguageServerProtocolPart):
3331
_logger: Final = LoggingDescriptor()
3432

35-
def __init__(self, parent: LanguageServerProtocol) -> None:
33+
def __init__(self, parent: "LanguageServerProtocol") -> None:
3634
super().__init__(parent)
3735

38-
@async_tasking_event
39-
async def format(
36+
@event
37+
def format(
4038
sender,
4139
document: TextDocument,
4240
options: FormattingOptions,
4341
**further_options: Any, # NOSONAR
4442
) -> Optional[List[TextEdit]]:
4543
...
4644

47-
@async_tasking_event
48-
async def format_range(
45+
@event
46+
def format_range(
4947
sender,
5048
document: TextDocument,
5149
range: Range,
@@ -62,7 +60,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
6260

6361
@rpc_method(name="textDocument/formatting", param_type=DocumentFormattingParams)
6462
@threaded
65-
async def _text_document_formatting(
63+
def _text_document_formatting(
6664
self,
6765
params: DocumentFormattingParams,
6866
text_document: TextDocumentIdentifier,
@@ -77,13 +75,15 @@ async def _text_document_formatting(
7775
if document is None:
7876
return None
7977

80-
for result in await self.format(
78+
for result in self.format(
8179
self,
8280
document,
8381
options,
8482
callback_filter=language_id_filter(document),
8583
**kwargs,
8684
):
85+
check_current_thread_canceled()
86+
8787
if isinstance(result, BaseException):
8888
if not isinstance(result, CancelledError):
8989
self._logger.exception(result, exc_info=result)
@@ -98,7 +98,7 @@ async def _text_document_formatting(
9898

9999
@rpc_method(name="textDocument/rangeFormatting", param_type=DocumentRangeFormattingParams)
100100
@threaded
101-
async def _text_document_range_formatting(
101+
def _text_document_range_formatting(
102102
self,
103103
params: DocumentFormattingParams,
104104
text_document: TextDocumentIdentifier,
@@ -113,14 +113,16 @@ async def _text_document_range_formatting(
113113
if document is None:
114114
return None
115115

116-
for result in await self.format_range(
116+
for result in self.format_range(
117117
self,
118118
document,
119119
range,
120120
options,
121121
callback_filter=language_id_filter(document),
122122
**kwargs,
123123
):
124+
check_current_thread_canceled()
125+
124126
if isinstance(result, BaseException):
125127
if not isinstance(result, CancelledError):
126128
self._logger.exception(result, exc_info=result)

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from __future__ import annotations
2-
3-
import asyncio
41
import io
52
import os
63
import re
4+
from concurrent.futures import CancelledError
75
from typing import TYPE_CHECKING, Any, List, Optional, cast
86

97
from robotcode.core.lsp.types import (
@@ -24,9 +22,7 @@
2422
from .protocol_part import RobotLanguageServerProtocolPart
2523

2624
if TYPE_CHECKING:
27-
from robotcode.language_server.robotframework.protocol import (
28-
RobotLanguageServerProtocol,
29-
)
25+
from robotcode.language_server.robotframework.protocol import RobotLanguageServerProtocol
3026

3127

3228
def robotidy_installed() -> bool:
@@ -40,7 +36,7 @@ def robotidy_installed() -> bool:
4036
class RobotFormattingProtocolPart(RobotLanguageServerProtocolPart, ModelHelperMixin):
4137
_logger = LoggingDescriptor()
4238

43-
def __init__(self, parent: RobotLanguageServerProtocol) -> None:
39+
def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
4440
super().__init__(parent)
4541

4642
parent.formatting.format.add(self.format)
@@ -54,29 +50,29 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
5450
self.short_test_name_length = 18
5551
self.setting_and_variable_name_length = 14
5652

57-
async def get_config(self, document: TextDocument) -> RoboTidyConfig:
53+
def get_config(self, document: TextDocument) -> RoboTidyConfig:
5854
folder = self.parent.workspace.get_workspace_folder(document.uri)
5955
if folder is None:
6056
return RoboTidyConfig()
6157

62-
return await self.parent.workspace.get_configuration_async(RoboTidyConfig, folder.uri)
58+
return self.parent.workspace.get_configuration(RoboTidyConfig, folder.uri)
6359

6460
@language_id("robotframework")
6561
@_logger.call
66-
async def format(
62+
def format(
6763
self,
6864
sender: Any,
6965
document: TextDocument,
7066
options: FormattingOptions,
7167
**further_options: Any,
7268
) -> Optional[List[TextEdit]]:
73-
config = await self.get_config(document)
69+
config = self.get_config(document)
7470

7571
if (config.enabled or get_robot_version() >= (5, 0)) and robotidy_installed():
76-
return await self.format_robot_tidy(document, options, config=config, **further_options)
72+
return self.format_robot_tidy(document, options, config=config, **further_options)
7773

7874
if get_robot_version() < (5, 0):
79-
return await self.format_internal(document, options, **further_options)
75+
return self.format_internal(document, options, **further_options)
8076

8177
self.parent.window.show_message(
8278
"RobotFramework formatter is not available, please install 'robotframework-tidy'.",
@@ -87,7 +83,7 @@ async def format(
8783

8884
RE_LINEBREAKS = re.compile(r"\r\n|\r|\n")
8985

90-
async def format_robot_tidy(
86+
def format_robot_tidy(
9187
self,
9288
document: TextDocument,
9389
options: FormattingOptions,
@@ -99,7 +95,7 @@ async def format_robot_tidy(
9995

10096
try:
10197
if config is None:
102-
config = await self.get_config(document)
98+
config = self.get_config(document)
10399

104100
robotidy_version = create_version_from_str(__version__)
105101

@@ -181,14 +177,14 @@ async def format_robot_tidy(
181177
)
182178
]
183179

184-
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
180+
except (SystemExit, KeyboardInterrupt, CancelledError):
185181
raise
186182
except BaseException as e:
187183
self._logger.exception(e)
188184
self.parent.window.show_message(f"Executing `robotidy` failed: {e}", MessageType.ERROR)
189185
return None
190186

191-
async def format_internal(
187+
def format_internal(
192188
self, document: TextDocument, options: FormattingOptions, **further_options: Any
193189
) -> Optional[List[TextEdit]]:
194190
from robot.parsing.model.blocks import File
@@ -227,16 +223,16 @@ async def format_internal(
227223
]
228224

229225
@language_id("robotframework")
230-
async def format_range(
226+
def format_range(
231227
self,
232228
sender: Any,
233229
document: TextDocument,
234230
range: Range,
235231
options: FormattingOptions,
236232
**further_options: Any,
237233
) -> Optional[List[TextEdit]]:
238-
config = await self.get_config(document)
234+
config = self.get_config(document)
239235
if config.enabled and robotidy_installed():
240-
return await self.format_robot_tidy(document, options, range=range, config=config, **further_options)
236+
return self.format_robot_tidy(document, options, range=range, config=config, **further_options)
241237

242238
return None

0 commit comments

Comments
 (0)