@@ -95,8 +95,6 @@ async def format_robot_tidy(
95
95
range : Optional [Range ] = None ,
96
96
** further_options : Any ,
97
97
) -> Optional [List [TextEdit ]]:
98
- from difflib import SequenceMatcher
99
-
100
98
from robotidy .version import __version__
101
99
102
100
try :
@@ -111,7 +109,7 @@ async def format_robot_tidy(
111
109
robot_tidy = get_robotidy (document .uri .to_path (), None )
112
110
113
111
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
115
113
robot_tidy .config .formatting .end_line = range .end .line + 1
116
114
117
115
disabler_finder = RegisterDisablers (
@@ -121,15 +119,19 @@ async def format_robot_tidy(
121
119
disabler_finder .visit (model )
122
120
if disabler_finder .file_disabled :
123
121
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 )
125
127
126
128
else :
127
129
from robotidy .api import RobotidyAPI
128
130
129
131
robot_tidy = RobotidyAPI (document .uri .to_path (), None )
130
132
131
133
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
133
135
robot_tidy .formatting_config .end_line = range .end .line + 1
134
136
135
137
if robotidy_version >= (2 , 2 ):
@@ -142,42 +144,25 @@ async def format_robot_tidy(
142
144
disabler_finder .visit (model )
143
145
if disabler_finder .file_disabled :
144
146
return None
145
- changed , _ , new = robot_tidy .transform (model , disabler_finder .disablers )
147
+ _ , _ , new = robot_tidy .transform (model , disabler_finder .disablers )
146
148
else :
147
- changed , _ , new = robot_tidy .transform (model )
149
+ _ , _ , new = robot_tidy .transform (model )
148
150
149
- if not changed :
151
+ if new . text == document . text () :
150
152
return None
151
153
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
+ ]
181
166
182
167
except (SystemExit , KeyboardInterrupt , asyncio .CancelledError ):
183
168
raise
0 commit comments