This repository was archived by the owner on Jan 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Normalize line breaks visitor #101
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
482129d
WIP draft of normalize line breaks visitor
nielsdebruin 96419ea
Further improve code
nielsdebruin a5e1684
Merge branch 'main' into autoformat/normalize-linebreaks-visitor
nielsdebruin baf6141
Merge branch 'main' into autoformat/normalize-linebreaks-visitor
nielsdebruin 2eae5c7
Merge remote-tracking branch 'origin/main' into autoformat/normalize-…
nielsdebruin 444ee2f
Add call inside autoformat visitor
nielsdebruin 66b05f2
Merge branch 'main' into autoformat/normalize-linebreaks-visitor
nielsdebruin 1e833ce
Merge remote-tracking branch 'origin/main' into autoformat/normalize-…
nielsdebruin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
rewrite/rewrite/python/format/normalize_line_breaks_visitor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
rewrite/tests/python/all/format/normalize_line_breaks_visitor_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.