Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for normalizing linebreak on trailing commas #103

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading