Skip to content

Commit

Permalink
Hopefully last lint
Browse files Browse the repository at this point in the history
  • Loading branch information
masad-frost committed Jun 2, 2022
1 parent 89f6211 commit b33a556
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 57 deletions.
11 changes: 10 additions & 1 deletion pylsp/text_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def merge_sort_text_edits(text_edits):
return text_edits


class OverLappingTextEditException(Exception):
"""
Text edits are expected to be sorted
and compressed instead of overlapping.
This error is raised when two edits
are overlapping.
"""
pass

def apply_text_edits(doc, text_edits):
text = doc.source
sorted_edits = merge_sort_text_edits(list(map(get_well_formatted_edit, text_edits)))
Expand All @@ -69,7 +78,7 @@ def apply_text_edits(doc, text_edits):
for e in sorted_edits:
start_offset = doc.offset_at_position(e['range']['start'])
if start_offset < last_modified_offset:
raise Exception('overlapping edit')
raise OverLappingTextEditException('overlapping edit')

if start_offset > last_modified_offset:
spans.append(text[last_modified_offset:start_offset])
Expand Down
109 changes: 53 additions & 56 deletions test/test_text_edit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pylsp.text_edit import apply_text_edits
from pylsp.text_edit import OverLappingTextEditException, apply_text_edits
from pylsp import uris

DOC_URI = uris.from_fs_path(__file__)
Expand Down Expand Up @@ -245,70 +245,67 @@ def test_apply_text_edits_overlap(pylsp):
pylsp.workspace.put_document(DOC_URI, '012345678901234567890123456789')
test_doc = pylsp.workspace.get_document(DOC_URI)

over_lapping_edits1 = [{
"range": {
"start": {
"line": 0,
"character": 3
},
"end": {
"line": 0,
"character": 6
}
},
"newText": "Hello"
}, {
"range": {
"start": {
"line": 0,
"character": 3
},
"end": {
"line": 0,
"character": 3
}
},
"newText": "World"
}]

did_throw = False
try:
apply_text_edits(test_doc, over_lapping_edits1)
except Exception:
apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
"character": 3
},
"end": {
"line": 0,
"character": 6
}
},
"newText": "Hello"
}, {
"range": {
"start": {
"line": 0,
"character": 3
},
"end": {
"line": 0,
"character": 3
}
},
"newText": "World"
}])
except OverLappingTextEditException:
did_throw = True

assert did_throw

over_lapping_edits2 = [{
"range": {
"start": {
"line": 0,
"character": 3
},
"end": {
"line": 0,
"character": 6
}
},
"newText": "Hello"
}, {
"range": {
"start": {
"line": 0,
"character": 4
},
"end": {
"line": 0,
"character": 4
}
},
"newText": "World"
}]
did_throw = False

try:
apply_text_edits(test_doc, over_lapping_edits2)
except Exception:
apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
"character": 3
},
"end": {
"line": 0,
"character": 6
}
},
"newText": "Hello"
}, {
"range": {
"start": {
"line": 0,
"character": 4
},
"end": {
"line": 0,
"character": 4
}
},
"newText": "World"
}])
except OverLappingTextEditException:
did_throw = True

assert did_throw
Expand Down

0 comments on commit b33a556

Please sign in to comment.