File tree 2 files changed +62
-57
lines changed
2 files changed +62
-57
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,14 @@ def merge_sort_text_edits(text_edits):
61
61
return text_edits
62
62
63
63
64
+ class OverLappingTextEditException (Exception ):
65
+ """
66
+ Text edits are expected to be sorted
67
+ and compressed instead of overlapping.
68
+ This error is raised when two edits
69
+ are overlapping.
70
+ """
71
+
64
72
def apply_text_edits (doc , text_edits ):
65
73
text = doc .source
66
74
sorted_edits = merge_sort_text_edits (list (map (get_well_formatted_edit , text_edits )))
@@ -69,7 +77,7 @@ def apply_text_edits(doc, text_edits):
69
77
for e in sorted_edits :
70
78
start_offset = doc .offset_at_position (e ['range' ]['start' ])
71
79
if start_offset < last_modified_offset :
72
- raise Exception ('overlapping edit' )
80
+ raise OverLappingTextEditException ('overlapping edit' )
73
81
74
82
if start_offset > last_modified_offset :
75
83
spans .append (text [last_modified_offset :start_offset ])
Original file line number Diff line number Diff line change 1
- from pylsp .text_edit import apply_text_edits
1
+ from pylsp .text_edit import OverLappingTextEditException , apply_text_edits
2
2
from pylsp import uris
3
3
4
4
DOC_URI = uris .from_fs_path (__file__ )
@@ -245,70 +245,67 @@ def test_apply_text_edits_overlap(pylsp):
245
245
pylsp .workspace .put_document (DOC_URI , '012345678901234567890123456789' )
246
246
test_doc = pylsp .workspace .get_document (DOC_URI )
247
247
248
- over_lapping_edits1 = [{
249
- "range" : {
250
- "start" : {
251
- "line" : 0 ,
252
- "character" : 3
253
- },
254
- "end" : {
255
- "line" : 0 ,
256
- "character" : 6
257
- }
258
- },
259
- "newText" : "Hello"
260
- }, {
261
- "range" : {
262
- "start" : {
263
- "line" : 0 ,
264
- "character" : 3
265
- },
266
- "end" : {
267
- "line" : 0 ,
268
- "character" : 3
269
- }
270
- },
271
- "newText" : "World"
272
- }]
273
-
274
248
did_throw = False
275
249
try :
276
- apply_text_edits (test_doc , over_lapping_edits1 )
277
- except Exception :
250
+ apply_text_edits (test_doc , [{
251
+ "range" : {
252
+ "start" : {
253
+ "line" : 0 ,
254
+ "character" : 3
255
+ },
256
+ "end" : {
257
+ "line" : 0 ,
258
+ "character" : 6
259
+ }
260
+ },
261
+ "newText" : "Hello"
262
+ }, {
263
+ "range" : {
264
+ "start" : {
265
+ "line" : 0 ,
266
+ "character" : 3
267
+ },
268
+ "end" : {
269
+ "line" : 0 ,
270
+ "character" : 3
271
+ }
272
+ },
273
+ "newText" : "World"
274
+ }])
275
+ except OverLappingTextEditException :
278
276
did_throw = True
279
277
280
278
assert did_throw
281
279
282
- over_lapping_edits2 = [{
283
- "range" : {
284
- "start" : {
285
- "line" : 0 ,
286
- "character" : 3
287
- },
288
- "end" : {
289
- "line" : 0 ,
290
- "character" : 6
291
- }
292
- },
293
- "newText" : "Hello"
294
- }, {
295
- "range" : {
296
- "start" : {
297
- "line" : 0 ,
298
- "character" : 4
299
- },
300
- "end" : {
301
- "line" : 0 ,
302
- "character" : 4
303
- }
304
- },
305
- "newText" : "World"
306
- }]
307
280
did_throw = False
308
281
309
282
try :
310
- apply_text_edits (test_doc , over_lapping_edits2 )
311
- except Exception :
283
+ apply_text_edits (test_doc , [{
284
+ "range" : {
285
+ "start" : {
286
+ "line" : 0 ,
287
+ "character" : 3
288
+ },
289
+ "end" : {
290
+ "line" : 0 ,
291
+ "character" : 6
292
+ }
293
+ },
294
+ "newText" : "Hello"
295
+ }, {
296
+ "range" : {
297
+ "start" : {
298
+ "line" : 0 ,
299
+ "character" : 4
300
+ },
301
+ "end" : {
302
+ "line" : 0 ,
303
+ "character" : 4
304
+ }
305
+ },
306
+ "newText" : "World"
307
+ }])
308
+ except OverLappingTextEditException :
312
309
did_throw = True
313
310
314
311
assert did_throw
You can’t perform that action at this time.
0 commit comments