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 a8cd387
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 57 deletions.
10 changes: 9 additions & 1 deletion pylsp/text_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ 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.
"""

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 +77,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 a8cd387

Please sign in to comment.