File tree Expand file tree Collapse file tree 2 files changed +63
-57
lines changed Expand file tree Collapse file tree 2 files changed +63
-57
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,15 @@ def merge_sort_text_edits(text_edits):
6161 return text_edits
6262
6363
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+ pass
72+
6473def apply_text_edits (doc , text_edits ):
6574 text = doc .source
6675 sorted_edits = merge_sort_text_edits (list (map (get_well_formatted_edit , text_edits )))
@@ -69,7 +78,7 @@ def apply_text_edits(doc, text_edits):
6978 for e in sorted_edits :
7079 start_offset = doc .offset_at_position (e ['range' ]['start' ])
7180 if start_offset < last_modified_offset :
72- raise Exception ('overlapping edit' )
81+ raise OverLappingTextEditException ('overlapping edit' )
7382
7483 if start_offset > last_modified_offset :
7584 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
22from pylsp import uris
33
44DOC_URI = uris .from_fs_path (__file__ )
@@ -245,70 +245,67 @@ def test_apply_text_edits_overlap(pylsp):
245245 pylsp .workspace .put_document (DOC_URI , '012345678901234567890123456789' )
246246 test_doc = pylsp .workspace .get_document (DOC_URI )
247247
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-
274248 did_throw = False
275249 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 :
278276 did_throw = True
279277
280278 assert did_throw
281279
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- }]
307280 did_throw = False
308281
309282 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 :
312309 did_throw = True
313310
314311 assert did_throw
You can’t perform that action at this time.
0 commit comments