-
Notifications
You must be signed in to change notification settings - Fork 7
Normalize line breaks visitor #101
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
Changes from all commits
482129d
96419ea
a5e1684
baf6141
2eae5c7
444ee2f
66b05f2
1e833ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional, TypeVar, Union | ||
|
|
||
| from rewrite import Tree, P, Cursor, list_map | ||
| from rewrite.java import J, Space, Comment, TextComment | ||
| from rewrite.python import PythonVisitor, PySpace, GeneralFormatStyle, PyComment | ||
| from rewrite.visitor import T | ||
|
|
||
| J2 = TypeVar('J2', bound=J) | ||
|
|
||
|
|
||
| class NormalizeLineBreaksVisitor(PythonVisitor): | ||
| def __init__(self, style: GeneralFormatStyle, stop_after: Tree = None): | ||
| self._stop_after = stop_after | ||
| self._stop = False | ||
| self._style = style | ||
|
|
||
| def visit_space(self, space: Optional[Space], loc: Optional[Union[PySpace.Location, Space.Location]], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here we will need an override for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be fixed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test case with a trailing comma for this visitor? I don't see the visit_marker override anywhere. |
||
| p: P) -> Space: | ||
| if not space or space is Space.EMPTY or not space.whitespace: | ||
| return space | ||
| s = space.with_whitespace(_normalize_new_lines(space.whitespace, self._style.use_crlf_new_lines)) | ||
|
|
||
| def process_comment(comment: Comment) -> Comment: | ||
| if comment.multiline: | ||
| if isinstance(comment, PyComment): | ||
| comment = comment.with_suffix(_normalize_new_lines(comment.suffix, self._style.use_crlf_new_lines)) | ||
| # TODO: Call PyComment Visitor, but this is not implemented yet.... | ||
| return comment | ||
| elif isinstance(comment, TextComment): | ||
| comment = comment.with_text(_normalize_new_lines(comment.text, self._style.use_crlf_new_lines)) | ||
|
|
||
| return comment.with_suffix(_normalize_new_lines(comment.suffix, self._style.use_crlf_new_lines)) | ||
|
|
||
| return s.with_comments(list_map(process_comment, s.comments)) | ||
|
|
||
| def post_visit(self, tree: T, _: object) -> Optional[T]: | ||
| if self._stop_after and tree == self._stop_after: | ||
| self._stop = True | ||
| return tree | ||
|
|
||
| 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) | ||
|
|
||
|
|
||
| STR = TypeVar('STR', bound=Optional[str]) | ||
|
|
||
|
|
||
| def _normalize_new_lines(text: STR, use_crlf: bool) -> STR: | ||
| """ | ||
| Normalize the line breaks in the given text to either use of CRLF or LF. | ||
| :param text: The text to normalize. | ||
| :param use_crlf: Whether to use CRLF line breaks. | ||
| :return: The text with normalized line breaks. | ||
| """ | ||
| if text is None or '\n' not in text: | ||
| return text | ||
|
|
||
| normalized = [] | ||
| for i, c in enumerate(text): | ||
| if use_crlf and c == '\n' and (i == 0 or text[i - 1] != '\r'): | ||
| normalized.append('\r\n') | ||
| elif use_crlf or c != '\r': | ||
| normalized.append(c) | ||
| return ''.join(normalized) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import unittest | ||
|
|
||
| from rewrite.python import GeneralFormatStyle | ||
| from rewrite.python.format.normalize_line_breaks_visitor import NormalizeLineBreaksVisitor | ||
| from rewrite.test import from_visitor, RecipeSpec, rewrite_run, python | ||
|
|
||
|
|
||
| class TestNormalizeLineBreaksVisitor(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| # language=python | ||
| self.windows = ( | ||
| "class Test:\r\n" | ||
| " # some comment\r\n" | ||
| " def test(self):\r\n" | ||
| " print()\r\n" | ||
| "\r\n" | ||
| ) | ||
| # language=python | ||
| self.linux = ( | ||
| "class Test:\n" | ||
| " # some comment\n" | ||
| " def test(self):\n" | ||
| " print()\n" | ||
| "\n" | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def normalize_line_breaks(use_crlf: bool) -> RecipeSpec: | ||
| style = GeneralFormatStyle(_use_crlf_new_lines=use_crlf) | ||
| return RecipeSpec().with_recipe(from_visitor(NormalizeLineBreaksVisitor(style))) | ||
|
|
||
| def test_windows_to_linux(self): | ||
| rewrite_run( | ||
| python( | ||
| self.windows, | ||
| self.linux | ||
| ), | ||
| spec=self.normalize_line_breaks(use_crlf=False) | ||
| ) | ||
|
|
||
| def test_linux_to_windows(self): | ||
| rewrite_run( | ||
| python( | ||
| self.linux, | ||
| self.windows | ||
| ), | ||
| spec=self.normalize_line_breaks(use_crlf=True) | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we still need to call the visitor.