8000 Fix Yapf formatting with CRLF line endings (#179) · python-lsp/python-lsp-server@7a3822d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a3822d

Browse files
authored
Fix Yapf formatting with CRLF line endings (#179)
1 parent 8add687 commit 7a3822d

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

pylsp/plugins/flake8_lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def run_flake8(flake8_executable, args, document):
8181
try:
8282
cmd = [flake8_executable]
8383
cmd.extend(args)
84-
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) # pylint: disable=consider-using-with
84+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
8585
except IOError:
8686
log.debug("Can't execute %s. Trying with '%s -m flake8'", flake8_executable, sys.executable)
8787
cmd = [sys.executable, '-m', 'flake8']

pylsp/plugins/pylint_lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def _run_pylint_stdio(pylint_executable, document, flags):
246246
cmd = [pylint_executable]
247247
cmd.extend(flags)
248248
cmd.extend(['--from-stdin', document.path])
249-
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) # pylint: disable=consider-using-with
249+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
250250
except IOError:
251251
log.debug("Can't execute %s. Trying with 'python -m pylint'", pylint_executable)
252252
cmd = ['python', '-m', 'pylint']

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_flake8_lint.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def test_flake8_unsaved(workspace):
4343

4444

4545
def test_flake8_lint(workspace):
46+
name, doc = temp_document(DOC, workspace)
4647
try:
47-
name, doc = temp_document(DOC, workspace)
4848
diags = flake8_lint.pylsp_lint(workspace, doc)
4949
msg = 'F841 local variable \'a\' is assigned to but never used'
5050
unused_var = [d for d in diags if d['message'] == msg][0]
@@ -54,7 +54,6 @@ def test_flake8_lint(workspace):
5454
assert unused_var['range']['start'] == {'line': 5, 'character': 1}
5555
assert unused_var['range']['end'] == {'line': 5, 'character': 11}
5656
assert unused_var['severity'] == lsp.DiagnosticSeverity.Warning
57-
5857
finally:
5958
os.remove(name)
6059

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_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)
0