Skip to content

Commit ffc4b7b

Browse files
authored
Add support for normalizing linebreak in trailing comma (#103)
1 parent 0980a30 commit ffc4b7b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

rewrite/rewrite/python/format/normalize_line_breaks_visitor.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

3-
from typing import Optional, TypeVar, Union
3+
from typing import Optional, TypeVar, Union, cast
44

5-
from rewrite import Tree, P, Cursor, list_map
6-
from rewrite.java import J, Space, Comment, TextComment
5+
from rewrite import Tree, P, Cursor, list_map, Marker
6+
from rewrite.java import J, Space, Comment, TextComment, TrailingComma
77
from rewrite.python import PythonVisitor, PySpace, GeneralFormatStyle, PyComment
88
from rewrite.visitor import T
99

@@ -43,6 +43,12 @@ def post_visit(self, tree: T, _: object) -> Optional[T]:
4343
def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) -> Optional[T]:
4444
return tree if self._stop else super().visit(tree, p, parent)
4545

46+
def visit_marker(self, marker: Marker, p: P) -> Marker:
47+
m = cast(Marker, super().visit_marker(marker, p))
48+
if isinstance(m, TrailingComma):
49+
return m.with_suffix(self.visit_space(m.suffix, None, p))
50+
return m
51+
4652

4753
STR = TypeVar('STR', bound=Optional[str])
4854

rewrite/tests/python/all/format/normalize_line_breaks_visitor_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def setUp(self):
1414
" # some comment\r\n"
1515
" def test(self):\r\n"
1616
" print()\r\n"
17+
" a = [\r\n"
18+
" 1,\r\n"
19+
" 2,\r\n"
20+
" ]\r\n"
1721
"\r\n"
1822
)
1923
# language=python
@@ -22,6 +26,10 @@ def setUp(self):
2226
" # some comment\n"
2327
" def test(self):\n"
2428
" print()\n"
29+
" a = [\n"
30+
" 1,\n"
31+
" 2,\n"
32+
" ]\n"
2533
"\n"
2634
)
2735

0 commit comments

Comments
 (0)