Skip to content

Commit

Permalink
Fix edits that occur at the end of the file (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Aug 10, 2017
1 parent f746c9c commit 219b47b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pyls/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def __str__(self):

@property
def lines(self):
# An empty document is much nicer to deal with assuming it has a single
# line with no characters and no final newline.
return self.source.splitlines(True) or [u'']
return self.source.splitlines(True)

@property
def source(self):
Expand All @@ -118,7 +116,13 @@ def apply_change(self, change):
end_line = change_range['end']['line']
end_col = change_range['end']['character']

# Check for an edit occuring at the very end of the file
if start_line == len(self.lines):
self._source = self.source + text
return

new = io.StringIO()

# Iterate over the existing document until we hit the edit range,
# at which point we write the new text, then loop until we hit
# the end of the range and continue writing.
Expand Down
17 changes: 17 additions & 0 deletions test/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,20 @@ def test_document_multiline_edit():
"def hello(a, b):\n",
" print a, b\n"
]


def test_document_end_of_file_edit():
old = [
"print 'a'\n",
"print 'b'\n"
]
doc = Document('file:///uri', u''.join(old))
doc.apply_change({'text': u'o', 'range': {
'start': {'line': 2, 'character': 0},
'end': {'line': 2, 'character': 0}
}})
assert doc.lines == [
"print 'a'\n",
"print 'b'\n",
"o",
]

0 comments on commit 219b47b

Please sign in to comment.