Skip to content

Commit

Permalink
Better whitespace handling
Browse files Browse the repository at this point in the history
The right-padding should only be consumed if the corresponding end-delimiter was encountered.
  • Loading branch information
knutwannheden committed Oct 11, 2024
1 parent de5da65 commit cd2c111
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
14 changes: 12 additions & 2 deletions rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,24 +1739,34 @@ def __convert_block(self, statements: Sequence, prefix: str = ':') -> j.Block:
def __pad_statement(self, stmt: ast.stmt) -> JRightPadded[Statement]:
statement = self.__convert(stmt)
# use whitespace until end of line as padding; what follows will be the prefix of next element
save_cursor = self._cursor
padding = self.__whitespace('\n')
if self._cursor < len(self._source) and self._source[self._cursor] == ';':
self._cursor += 1
markers = Markers.EMPTY.with_markers([Semicolon(random_id())])
else:
padding = Space.EMPTY
markers = Markers.EMPTY
self._cursor = save_cursor
return JRightPadded(statement, padding, markers)


def __pad_list_element(self, element: J, last: bool = False, pad_last: bool = True, end_delim: str = None) -> JRightPadded[J]:
save_cursor = self._cursor
padding = self.__whitespace() if pad_last or not last else Space.EMPTY
markers = Markers.EMPTY
if last and self._cursor < len(self._source):
if self._source[self._cursor] == ',' and end_delim != ',':
self._cursor += 1
markers = markers.with_markers([TrailingComma(random_id(), self.__whitespace())])
if end_delim is not None and self._source[self._cursor] == end_delim:
self._cursor += 1
elif self._source[self._cursor] != end_delim:
padding = Space.EMPTY
self._cursor = save_cursor
if end_delim and self._source[self._cursor] == end_delim:
self._cursor += len(end_delim)
elif last:
padding = Space.EMPTY
self._cursor = save_cursor
elif not last:
self._cursor += 1
markers = Markers.EMPTY
Expand Down
11 changes: 11 additions & 0 deletions rewrite/tests/python/all/import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ def test_relative_import_1():
def test_relative_import_2():
# language=python
rewrite_run(python("from ..foo import bar"))


# noinspection PyUnresolvedReferences
def test_crlf():
# language=python
rewrite_run(python(
"""\
import foo
import bar
""".replace('\n', '\r\n')
))

0 comments on commit cd2c111

Please sign in to comment.