Skip to content

Commit 3b4c0e8

Browse files
committed
fix: correct formatting with robotframework-tidy, also support tidy 4.0 reruns now, closes #124
1 parent 7575a77 commit 3b4c0e8

File tree

1 file changed

+22
-37
lines changed
  • packages/language_server/src/robotcode/language_server/robotframework/parts

1 file changed

+22
-37
lines changed

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

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ async def format_robot_tidy(
9595
range: Optional[Range] = None,
9696
**further_options: Any,
9797
) -> Optional[List[TextEdit]]:
98-
from difflib import SequenceMatcher
99-
10098
from robotidy.version import __version__
10199

102100
try:
@@ -111,7 +109,7 @@ async def format_robot_tidy(
111109
robot_tidy = get_robotidy(document.uri.to_path(), None)
112110

113111
if range is not None:
114-
robot_tidy.config.formatting.start_line = range.start.line
112+
robot_tidy.config.formatting.start_line = range.start.line + 1
115113
robot_tidy.config.formatting.end_line = range.end.line + 1
116114

117115
disabler_finder = RegisterDisablers(
@@ -121,15 +119,19 @@ async def format_robot_tidy(
121119
disabler_finder.visit(model)
122120
if disabler_finder.file_disabled:
123121
return None
124-
changed, _, new = robot_tidy.transform(model, disabler_finder.disablers)
122+
123+
if robotidy_version >= (4, 0):
124+
_, _, new, _ = robot_tidy.transform_until_stable(model, disabler_finder)
125+
else:
126+
_, _, new = robot_tidy.transform(model, disabler_finder.disablers)
125127

126128
else:
127129
from robotidy.api import RobotidyAPI
128130

129131
robot_tidy = RobotidyAPI(document.uri.to_path(), None)
130132

131133
if range is not None:
132-
robot_tidy.formatting_config.start_line = range.start.line
134+
robot_tidy.formatting_config.start_line = range.start.line + 1
133135
robot_tidy.formatting_config.end_line = range.end.line + 1
134136

135137
if robotidy_version >= (2, 2):
@@ -142,42 +144,25 @@ async def format_robot_tidy(
142144
disabler_finder.visit(model)
143145
if disabler_finder.file_disabled:
144146
return None
145-
changed, _, new = robot_tidy.transform(model, disabler_finder.disablers)
147+
_, _, new = robot_tidy.transform(model, disabler_finder.disablers)
146148
else:
147-
changed, _, new = robot_tidy.transform(model)
149+
_, _, new = robot_tidy.transform(model)
148150

149-
if not changed:
151+
if new.text == document.text():
150152
return None
151153

152-
new_lines = self.RE_LINEBREAKS.split(new.text)
153-
154-
result: List[TextEdit] = []
155-
matcher = SequenceMatcher(a=document.get_lines(), b=new_lines, autojunk=False)
156-
for code, old_start, old_end, new_start, new_end in matcher.get_opcodes():
157-
if code == "insert" or code == "replace":
158-
result.append(
159-
TextEdit(
160-
range=Range(
161-
start=Position(line=old_start, character=0),
162-
end=Position(line=old_end, character=0),
163-
),
164-
new_text=os.linesep.join(new_lines[new_start:new_end]) + os.linesep,
165-
)
166-
)
167-
168-
elif code == "delete":
169-
result.append(
170-
TextEdit(
171-
range=Range(
172-
start=Position(line=old_start, character=0),
173-
end=Position(line=old_end, character=0),
174-
),
175-
new_text="",
176-
)
177-
)
178-
179-
if result:
180-
return result
154+
return [
155+
TextEdit(
156+
range=Range(
157+
start=Position(line=0, character=0),
158+
end=Position(
159+
line=len(document.get_lines()),
160+
character=len((document.get_lines())[-1]),
161+
),
162+
),
163+
new_text=new.text,
164+
)
165+
]
181166

182167
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
183168
raise

0 commit comments

Comments
 (0)