-
Notifications
You must be signed in to change notification settings - Fork 7
Add RemoveTrailingWhitespaceVisitor #100
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f7c9a4
Add RemoveTrailingWhitespaceVisitor
nielsdebruin ddf4c33
Add pre/post visit methods
nielsdebruin c5b5da5
Fix attribute
nielsdebruin 59d92d5
Call RemoveTrailingWhiteSpaceVisitor in AutoFormatVisitor
nielsdebruin 720d275
Merge branch 'main' into autoformat/trailing-whitespace
nielsdebruin 5452f52
Merge branch 'main' into autoformat/trailing-whitespace
nielsdebruin 163a9c9
Update rewrite/rewrite/python/format/remove_trailing_whitespace_visit…
nielsdebruin b457e35
Fix for trailing comma and clean
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
46 changes: 46 additions & 0 deletions
46
rewrite/rewrite/python/format/remove_trailing_whitespace_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,46 @@ | ||
| from typing import Optional, cast, Union | ||
|
|
||
| import rewrite.java as j | ||
| import rewrite.python as p | ||
| from rewrite import Tree | ||
| from rewrite.java import Space, J | ||
| from rewrite.python import PythonVisitor, CompilationUnit, PySpace | ||
| from rewrite.visitor import P, Cursor, T | ||
|
|
||
|
|
||
| class RemoveTrailingWhitespaceVisitor(PythonVisitor): | ||
| def __init__(self, stop_after: Optional[p.Tree] = None): | ||
| self._stop_after = stop_after | ||
| self._stop = False | ||
|
|
||
| def visit_compilation_unit(self, compilation_unit: CompilationUnit, p: P) -> J: | ||
| if not compilation_unit.prefix.comments: | ||
| compilation_unit = compilation_unit.with_prefix(Space.EMPTY) | ||
| cu: j.CompilationUnit = cast(j.CompilationUnit, super().visit_compilation_unit(compilation_unit, p)) | ||
|
|
||
| if cu.eof.whitespace: | ||
| clean = "".join([_ for _ in cu.eof.whitespace if _ in ['\n', '\r']]) | ||
| cu = cu.with_eof(cu.eof.with_whitespace(clean)) | ||
|
|
||
| return cu | ||
|
|
||
| def visit_space(self, space: Optional[Space], loc: Optional[Union[PySpace.Location, Space.Location]], | ||
| p: P) -> Space: | ||
| s = cast(Space, super().visit_space(space, loc, p)) | ||
|
|
||
| if not s or not s.whitespace: | ||
| return s | ||
|
|
||
| last_newline = s.whitespace.rfind('\n') | ||
| if last_newline > 0: | ||
| ws = [c for i, c in enumerate(s.whitespace) if i >= last_newline or c in {',', '\r', '\n'}] | ||
nielsdebruin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| s = s.with_whitespace(''.join(ws)) | ||
| return s | ||
|
|
||
| def post_visit(self, tree: T, p: P) -> 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) | ||
37 changes: 37 additions & 0 deletions
37
rewrite/tests/python/all/format/remove_trailing_whitespace.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,37 @@ | ||
| import pytest | ||
|
|
||
| from rewrite.python.format.remove_trailing_whitespace_visitor import RemoveTrailingWhitespaceVisitor | ||
| from rewrite.test import rewrite_run, RecipeSpec, from_visitor, python | ||
|
|
||
|
|
||
| class Foo: | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('n_spaces', list(range(0, 4))) | ||
| @pytest.mark.parametrize('linebreaks', ['\n', '\r\n', '\r\n\n', '\n\n']) | ||
| def test_tabs_to_spaces(n_spaces, linebreaks): | ||
| # noinspection PyInconsistentIndentation | ||
| spaces = ' ' * n_spaces | ||
| rewrite_run( | ||
| python( | ||
| # language=python | ||
| f"""\ | ||
| class Foo:{spaces}{linebreaks}\ | ||
| def bar(self): | ||
| return 42{linebreaks} | ||
| {spaces}{linebreaks} | ||
| """, | ||
| # language=python | ||
| f"""\ | ||
| class Foo:{linebreaks}\ | ||
| def bar(self): | ||
| return 42{linebreaks} | ||
| {linebreaks} | ||
| """ | ||
| ), | ||
| spec=RecipeSpec() | ||
| .with_recipes( | ||
| from_visitor(RemoveTrailingWhitespaceVisitor()) | ||
| ) | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.