|
2 | 2 | # Copyright 2021- Python Language Server Contributors.
|
3 | 3 |
|
4 | 4 | import logging
|
| 5 | + |
5 | 6 | import pycodestyle
|
6 | 7 | from autopep8 import fix_code, continued_indentation as autopep8_c_i
|
| 8 | + |
7 | 9 | from pylsp import hookimpl
|
| 10 | +from pylsp._utils import get_eol_chars |
8 | 11 |
|
9 | 12 | log = logging.getLogger(__name__)
|
10 | 13 |
|
@@ -38,15 +41,27 @@ def _format(config, document, line_range=None):
|
38 | 41 | del pycodestyle._checks['logical_line'][pycodestyle.continued_indentation]
|
39 | 42 | pycodestyle.register_check(autopep8_c_i)
|
40 | 43 |
|
41 |
| - new_source = fix_code(document.source, options=options) |
| 44 | + # Autopep8 doesn't work with CR line endings, so we replace them by '\n' |
| 45 | + # and restore them below. |
| 46 | + replace_cr = False |
| 47 | + source = document.source |
| 48 | + eol_chars = get_eol_chars(source) |
| 49 | + if eol_chars == '\r': |
| 50 | + replace_cr = True |
| 51 | + source = source.replace('\r', '\n') |
| 52 | + |
| 53 | + new_source = fix_code(source, options=options) |
42 | 54 |
|
43 | 55 | # Switch it back
|
44 | 56 | del pycodestyle._checks['logical_line'][autopep8_c_i]
|
45 | 57 | pycodestyle.register_check(pycodestyle.continued_indentation)
|
46 | 58 |
|
47 |
| - if new_source == document.source: |
| 59 | + if new_source == source: |
48 | 60 | return []
|
49 | 61 |
|
| 62 | + if replace_cr: |
| 63 | + new_source = new_source.replace('\n', '\r') |
| 64 | + |
50 | 65 | # I'm too lazy at the moment to parse diffs into TextEdit items
|
51 | 66 | # So let's just return the entire file...
|
52 | 67 | return [{
|
|
0 commit comments