Skip to content

Commit 219b47b

Browse files
authored
Fix edits that occur at the end of the file (#91)
1 parent f746c9c commit 219b47b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

pyls/workspace.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ def __str__(self):
9292

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

9997
@property
10098
def source(self):
@@ -118,7 +116,13 @@ def apply_change(self, change):
118116
end_line = change_range['end']['line']
119117
end_col = change_range['end']['character']
120118

119+
# Check for an edit occuring at the very end of the file
120+
if start_line == len(self.lines):
121+
self._source = self.source + text
122+
return
123+
121124
new = io.StringIO()
125+
122126
# Iterate over the existing document until we hit the edit range,
123127
# at which point we write the new text, then loop until we hit
124128
# the end of the range and continue writing.

test/test_document.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,20 @@ def test_document_multiline_edit():
7777
"def hello(a, b):\n",
7878
" print a, b\n"
7979
]
80+
81+
82+
def test_document_end_of_file_edit():
83+
old = [
84+
"print 'a'\n",
85+
"print 'b'\n"
86+
]
87+
doc = Document('file:///uri', u''.join(old))
88+
doc.apply_change({'text': u'o', 'range': {
89+
'start': {'line': 2, 'character': 0},
90+
'end': {'line': 2, 'character': 0}
91+
}})
92+
assert doc.lines == [
93+
"print 'a'\n",
94+
"print 'b'\n",
95+
"o",
96+
]

0 commit comments

Comments
 (0)