Skip to content

Commit

Permalink
Remove apply_text_edits from document method
Browse files Browse the repository at this point in the history
  • Loading branch information
masad-frost committed Dec 9, 2021
1 parent 8315b59 commit 341732d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
41 changes: 20 additions & 21 deletions pylsp/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ def merge_sort_text_edits(text_edits):
right_idx += 1
return text_edits

def apply_text_edits(doc, text_edits):
text = doc.source
sorted_edits = merge_sort_text_edits(list(map(get_well_formatted_edit,text_edits)))
last_modified_offset = 0
spans = []
for e in sorted_edits:
start_offset = doc.offset_at_position(e['range']['start'])
if start_offset < last_modified_offset:
raise Exception('overlapping edit')
elif start_offset > last_modified_offset:
spans.append(text[last_modified_offset:start_offset])

if len(e['newText']):
spans.append(e['newText'])
last_modified_offset = doc.offset_at_position(e['range']['end'])

spans.append(text[last_modified_offset:])
return ''.join(spans)

class Document:

def __init__(self, uri, workspace, source=None, version=None, local=True, extra_sys_path=None,
Expand Down Expand Up @@ -273,27 +292,7 @@ def apply_change(self, change):
if i == end_line:
new.write(line[end_col:])

self._source = new.getvalue()

@lock
def apply_text_edits(self, text_edits):
text = self._source
sorted_edits = merge_sort_text_edits(list(map(get_well_formatted_edit,text_edits)))
last_modified_offset = 0
spans = []
for e in sorted_edits:
start_offset = self.offset_at_position(e['range']['start'])
if start_offset < last_modified_offset:
raise Exception('overlapping edit')
elif start_offset > last_modified_offset:
spans.append(text[last_modified_offset:start_offset])

if len(e['newText']):
spans.append(e['newText'])
last_modified_offset = self.offset_at_position(e['range']['end'])

spans.append(text[last_modified_offset:])
return ''.join(spans)
self._source = new.getvalue()

def offset_at_position(self, position):
"""Return the byte-offset pointed at by the given position."""
Expand Down
26 changes: 13 additions & 13 deletions test/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from pylsp import uris

from pylsp.workspace import apply_text_edits

DOC_URI = uris.from_fs_path(__file__)

Expand Down Expand Up @@ -299,7 +299,7 @@ def test_apply_text_edits_insert(pylsp):
pylsp.workspace.put_document(DOC_URI, '012345678901234567890123456789')
test_doc = pylsp.workspace.get_document(DOC_URI)

assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand All @@ -312,7 +312,7 @@ def test_apply_text_edits_insert(pylsp):
},
"newText": "Hello"
}]) == 'Hello012345678901234567890123456789'
assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand All @@ -325,7 +325,7 @@ def test_apply_text_edits_insert(pylsp):
},
"newText": "Hello"
}]) == '0Hello12345678901234567890123456789'
assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand All @@ -350,7 +350,7 @@ def test_apply_text_edits_insert(pylsp):
},
"newText": "World"
}]) == '0HelloWorld12345678901234567890123456789'
assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand Down Expand Up @@ -416,7 +416,7 @@ def test_apply_text_edits_replace(pylsp):
pylsp.workspace.put_document(DOC_URI, '012345678901234567890123456789')
test_doc = pylsp.workspace.get_document(DOC_URI)

assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand All @@ -429,7 +429,7 @@ def test_apply_text_edits_replace(pylsp):
},
"newText": "Hello"
}]) == '012Hello678901234567890123456789'
assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand All @@ -454,7 +454,7 @@ def test_apply_text_edits_replace(pylsp):
},
"newText": "World"
}]) == '012HelloWorld901234567890123456789'
assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand All @@ -479,7 +479,7 @@ def test_apply_text_edits_replace(pylsp):
},
"newText": "World"
}]) == '012HelloWorld678901234567890123456789'
assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand All @@ -504,7 +504,7 @@ def test_apply_text_edits_replace(pylsp):
},
"newText": "Hello"
}]) == '012HelloWorld678901234567890123456789'
assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand Down Expand Up @@ -537,7 +537,7 @@ def test_apply_text_edits_overlap(pylsp):

did_throw = False
try:
test_doc.apply_text_edits([{
apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand Down Expand Up @@ -568,7 +568,7 @@ def test_apply_text_edits_overlap(pylsp):
assert did_throw

try:
test_doc.apply_text_edits([{
apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 0,
Expand Down Expand Up @@ -602,7 +602,7 @@ def test_apply_text_edits_multiline(pylsp):
pylsp.workspace.put_document(DOC_URI, '0\n1\n2\n3\n4')
test_doc = pylsp.workspace.get_document(DOC_URI)

assert test_doc.apply_text_edits([{
assert apply_text_edits(test_doc, [{
"range": {
"start": {
"line": 2,
Expand Down

0 comments on commit 341732d

Please sign in to comment.