Skip to content

Commit ce5a564

Browse files
committed
Implement API changes for Robotidy >= 2.2, fixes #55
1 parent 9a62acd commit ce5a564

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

robotcode/language_server/robotframework/parts/formatting.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from ..protocol import RobotLanguageServerProtocol
2424

2525
from ..configuration import RoboTidyConfig
26+
from ..utils.version import create_version_from_str
2627
from .model_helper import ModelHelperMixin
2728
from .protocol_part import RobotLanguageServerProtocolPart
2829

@@ -43,8 +44,8 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
4344

4445
parent.formatting.format.add(self.format)
4546

46-
# TODO implement range formatting
47-
# parent.formatting.format_range.add(self.format_range)
47+
if robotidy_installed():
48+
parent.formatting.format_range.add(self.format_range)
4849

4950
self.space_count = 4
5051
self.use_pipes = False
@@ -82,19 +83,37 @@ async def format(
8283
RE_LINEBREAKS = re.compile(r"\r\n|\r|\n")
8384

8485
async def format_robot_tidy(
85-
self, document: TextDocument, options: FormattingOptions, **further_options: Any
86+
self, document: TextDocument, options: FormattingOptions, range: Optional[Range] = None, **further_options: Any
8687
) -> Optional[List[TextEdit]]:
8788

8889
from difflib import SequenceMatcher
8990

9091
from robotidy.api import RobotidyAPI
92+
from robotidy.version import __version__
9193

9294
try:
9395
model = await self.parent.documents_cache.get_model(document, False)
96+
if model is None:
97+
return None
9498

9599
robot_tidy = RobotidyAPI(document.uri.to_path(), None)
96100

97-
changed, _, new = robot_tidy.transform(model)
101+
if range is not None:
102+
robot_tidy.formatting_config.start_line = range.start.line
103+
robot_tidy.formatting_config.end_line = range.end.line + 1
104+
105+
if create_version_from_str(__version__) >= (2, 2):
106+
from robotidy.disablers import RegisterDisablers
107+
108+
disabler_finder = RegisterDisablers(
109+
robot_tidy.formatting_config.start_line, robot_tidy.formatting_config.end_line
110+
)
111+
disabler_finder.visit(model)
112+
if disabler_finder.file_disabled:
113+
return None
114+
changed, _, new = robot_tidy.transform(model, disabler_finder.disablers)
115+
else:
116+
changed, _, new = robot_tidy.transform(model)
98117

99118
if not changed:
100119
return None
@@ -173,8 +192,9 @@ async def format_internal(
173192
async def format_range(
174193
self, sender: Any, document: TextDocument, range: Range, options: FormattingOptions, **further_options: Any
175194
) -> Optional[List[TextEdit]]:
176-
# TODO implement range formatting
177-
# config = await self.get_config(document)
178-
# if config and config.enabled and robotidy_installed():
179-
# return await self.format_robot_tidy(document, options, range=range, **further_options)
195+
196+
config = await self.get_config(document)
197+
if config and config.enabled and robotidy_installed():
198+
return await self.format_robot_tidy(document, options, range=range, **further_options)
199+
180200
return None

robotcode/language_server/robotframework/utils/version.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import re
22
from typing import NamedTuple, Optional
33

4-
__all__ = ["InvalidRobotVersionError", "get_robot_version"]
4+
__all__ = ["InvalidVersionError", "get_robot_version"]
55

66

7-
class InvalidRobotVersionError(Exception):
7+
class InvalidVersionError(Exception):
88
def __init__(self) -> None:
99
super().__init__("Invalid robot version string.")
1010

1111

12-
class RobotVersion(NamedTuple):
12+
class Version(NamedTuple):
1313
major: int
1414
minor: int
1515
patch: Optional[int] = None
@@ -18,13 +18,16 @@ class RobotVersion(NamedTuple):
1818
dev: Optional[int] = None
1919

2020

21-
def get_robot_version() -> RobotVersion:
21+
def get_robot_version() -> Version:
2222
import robot
2323

24+
return create_version_from_str(robot.get_version())
25+
26+
27+
def create_version_from_str(version_str: str) -> Version:
2428
def s_to_i(s: Optional[str]) -> Optional[int]:
2529
return int(s) if s is not None else None
2630

27-
robot_version = robot.get_version()
2831
try:
2932
m = re.match(
3033
r"(?P<major>\d+)"
@@ -33,11 +36,11 @@ def s_to_i(s: Optional[str]) -> Optional[int]:
3336
r"((?P<pre_id>a|b|rc)(?P<pre_number>\d+))?"
3437
r"(\.(dev(?P<dev>\d+)))?"
3538
r"(?P<rest>.+)?",
36-
robot_version,
39+
version_str,
3740
)
3841

3942
if m is not None and m.group("rest") is None:
40-
return RobotVersion(
43+
return Version(
4144
int(m.group("major")),
4245
int(m.group("minor")),
4346
s_to_i(m.group("patch")),
@@ -48,9 +51,9 @@ def s_to_i(s: Optional[str]) -> Optional[int]:
4851
except (SystemExit, KeyboardInterrupt):
4952
raise
5053
except BaseException as ex:
51-
raise InvalidRobotVersionError() from ex
54+
raise InvalidVersionError() from ex
5255

53-
raise InvalidRobotVersionError()
56+
raise InvalidVersionError()
5457

5558

5659
if __name__ == "__main__":

0 commit comments

Comments
 (0)