Skip to content

Commit 2f9a509

Browse files
committed
handle EOF newline and an extra test
1 parent 8847a5d commit 2f9a509

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

pylsp/plugins/yapf_format.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,18 @@ def _format(document, lines=None, options=None):
100100
diff = next(patch_generator)
101101
patch_generator.close()
102102

103-
# To keep things simple our text edits will be line based
104-
# and uncompacted
103+
# To keep things simple our text edits will be line based.
104+
# We will also return the edits uncompacted, meaning a
105+
# line replacement will come in as a line remove followed
106+
# by a line add instead of a line replace.
105107
textEdits = []
106108
# keep track of line number since additions
107109
# don't include the line number it's being added
108110
# to in diffs. lsp is 0-indexed so we'll start with -1
109111
prev_line_no = -1
110112
for change in diff.changes:
111113
if change.old and change.new:
112-
# no change
114+
# old and new are the same line, no change
113115
# diffs are 1-indexed
114116
prev_line_no = change.old - 1
115117
elif change.new:
@@ -149,4 +151,23 @@ def _format(document, lines=None, options=None):
149151
})
150152
prev_line_no = lsp_line_no
151153

154+
# diffs don't include EOF newline https://github.com/google/yapf/issues/1008
155+
# we'll add it ourselves if our document doesn't already have it and the diff
156+
# does not change the last line.
157+
if not source.endswith(eol_chars) and diff.changes \
158+
and diff.changes[-1].old and diff.changes[-1].new:
159+
textEdits.append({
160+
'range': {
161+
'start': {
162+
'line': prev_line_no,
163+
'character': 0
164+
},
165+
'end': {
166+
'line': prev_line_no + 1,
167+
'character': 0
168+
}
169+
},
170+
'newText': diff.changes[-1].line + eol_chars
171+
})
172+
152173
return textEdits

test/plugins/test_yapf_format.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,42 @@ def test_config_file(tmpdir, workspace):
6868
def test_line_endings(workspace, newline):
6969
doc = Document(DOC_URI, workspace, f'import os;import sys{2 * newline}dict(a=1)')
7070
res = pylsp_format_document(doc)
71-
71+
7272
assert apply_text_edits(doc, res) == f'import os{newline}import sys{2 * newline}dict(a=1){newline}'
7373

7474

7575
def test_format_with_tab_size_option(workspace):
7676
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
7777
res = pylsp_format_document(doc, {"tabSize": "8"})
7878

79-
assert len(res) == 1
80-
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", " ")
79+
assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace(" ", " ")
8180

8281

8382
def test_format_with_insert_spaces_option(workspace):
8483
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
8584
res = pylsp_format_document(doc, {"insertSpaces": False})
8685

87-
assert len(res) == 1
88-
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", "\t")
86+
assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace(" ", "\t")
8987

9088

9189
def test_format_with_yapf_specific_option(workspace):
9290
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
9391
res = pylsp_format_document(doc, {"USE_TABS": True})
9492

95-
assert len(res) == 1
96-
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", "\t")
93+
assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace(" ", "\t")
94+
95+
def test_format_returns_text_edit_per_line(workspace):
96+
single_space_indent = """def wow():
97+
print("x")
98+
print("hi")"""
99+
doc = Document(DOC_URI, workspace, single_space_indent)
100+
res = pylsp_format_document(doc)
101+
102+
# two removes and two adds
103+
assert len(res) == 4
104+
assert res[0]['newText'] == ""
105+
assert res[1]['newText'] == ""
106+
assert res[2]['newText'] == " print(\"x\")\n"
107+
assert res[3]['newText'] == " print(\"hi\")\n"
108+
109+

0 commit comments

Comments
 (0)