Skip to content

Commit

Permalink
Add support for normalizing linebreak in trailing comma (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdebruin authored Dec 16, 2024
1 parent 0980a30 commit ffc4b7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rewrite/rewrite/python/format/normalize_line_breaks_visitor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from typing import Optional, TypeVar, Union
from typing import Optional, TypeVar, Union, cast

from rewrite import Tree, P, Cursor, list_map
from rewrite.java import J, Space, Comment, TextComment
from rewrite import Tree, P, Cursor, list_map, Marker
from rewrite.java import J, Space, Comment, TextComment, TrailingComma
from rewrite.python import PythonVisitor, PySpace, GeneralFormatStyle, PyComment
from rewrite.visitor import T

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

def visit_marker(self, marker: Marker, p: P) -> Marker:
m = cast(Marker, super().visit_marker(marker, p))
if isinstance(m, TrailingComma):
return m.with_suffix(self.visit_space(m.suffix, None, p))
return m


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def setUp(self):
" # some comment\r\n"
" def test(self):\r\n"
" print()\r\n"
" a = [\r\n"
" 1,\r\n"
" 2,\r\n"
" ]\r\n"
"\r\n"
)
# language=python
Expand All @@ -22,6 +26,10 @@ def setUp(self):
" # some comment\n"
" def test(self):\n"
" print()\n"
" a = [\n"
" 1,\n"
" 2,\n"
" ]\n"
"\n"
)

Expand Down

0 comments on commit ffc4b7b

Please sign in to comment.