Skip to content

Commit 1db8f03

Browse files
committed
Fix Yapf formatting with CRLF line endings
Also add a test for CRLF eols with Autopep8
1 parent 552dea3 commit 1db8f03

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

pylsp/plugins/yapf_format.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ def pylsp_format_range(document, range): # pylint: disable=redefined-builtin
3737

3838

3939
def _format(document, lines=None):
40-
# Yapf doesn't work with CR line endings, so we replace them by '\n'
40+
# Yapf doesn't work with CRLF/CR line endings, so we replace them by '\n'
4141
# and restore them below.
42-
replace_cr = False
42+
replace_eols = False
4343
source = document.source
4444
eol_chars = get_eol_chars(source)
45-
if eol_chars == '\r':
46-
replace_cr = True
47-
source = source.replace('\r', '\n')
45+
if eol_chars in ['\r', '\r\n']:
46+
replace_eols = True
47+
source = source.replace(eol_chars, '\n')
4848

4949
new_source, changed = FormatCode(
5050
source,
@@ -58,8 +58,8 @@ def _format(document, lines=None):
5858
if not changed:
5959
return []
6060

61-
if replace_cr:
62-
new_source = new_source.replace('\n', '\r')
61+
if replace_eols:
62+
new_source = new_source.replace('\n', eol_chars)
6363

6464
# I'm too lazy at the moment to parse diffs into TextEdit items
6565
# So let's just return the entire file...

test/plugins/test_autopep8_format.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33

4+
import pytest
5+
46
from pylsp import uris
57
from pylsp.plugins.autopep8_format import pylsp_format_document, pylsp_format_range
68
from pylsp.workspace import Document
@@ -73,8 +75,9 @@ def test_hanging_indentation(config, workspace):
7375
assert res[0]['newText'] == CORRECT_INDENTED_DOC
7476

7577

76-
def test_cr_line_endings(config, workspace):
77-
doc = Document(DOC_URI, workspace, 'import os;import sys\r\rdict(a=1)')
78+
@pytest.mark.parametrize('newline', ['\r\n', '\r'])
79+
def test_line_endings(config, workspace, newline):
80+
doc = Document(DOC_URI, workspace, f'import os;import sys{2 * newline}dict(a=1)')
7881
res = pylsp_format_document(config, doc)
7982

80-
assert res[0]['newText'] == 'import os\rimport sys\r\rdict(a=1)\r'
83+
assert res[0]['newText'] == f'import os{newline}import sys{2 * newline}dict(a=1){newline}'

test/plugins/test_yapf_format.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33

4+
import pytest
5+
46
from pylsp import uris
57
from pylsp.plugins.yapf_format import pylsp_format_document, pylsp_format_range
68
from pylsp.workspace import Document
@@ -60,8 +62,9 @@ def test_config_file(tmpdir, workspace):
6062
assert pylsp_format_document(doc)[0]['newText'] == "A = [\n 'h', 'w',\n 'a'\n]\n\nB = ['h', 'w']\n"
6163

6264

63-
def test_cr_line_endings(workspace):
64-
doc = Document(DOC_URI, workspace, 'import os;import sys\r\rdict(a=1)')
65+
@pytest.mark.parametrize('newline', ['\r\n', '\r'])
66+
def test_cr_line_endings(workspace, newline):
67+
doc = Document(DOC_URI, workspace, f'import os;import sys{2 * newline}dict(a=1)')
6568
res = pylsp_format_document(doc)
6669

67-
assert res[0]['newText'] == 'import os\rimport sys\r\rdict(a=1)\r'
70+
assert res[0]['newText'] == f'import os{newline}import sys{2 * newline}dict(a=1){newline}'

0 commit comments

Comments
 (0)