Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7168020
Add test cases for tabs and indents visitor
nielsdebruin Dec 16, 2024
b6dee44
Merge branch 'main' into autoformat/tabs-and-indents
nielsdebruin Dec 16, 2024
c2b1008
Merge branch 'main' into autoformat/tabs-and-indents
nielsdebruin Dec 19, 2024
1fd9945
Add extra test cases for autoformat
nielsdebruin Dec 19, 2024
52ae4a5
More tests
nielsdebruin Dec 19, 2024
dacd5b1
Add helper methods
nielsdebruin Dec 19, 2024
d5bbcb6
Add helper method to cursor
nielsdebruin Dec 19, 2024
092f9cf
Merge remote-tracking branch 'origin/main' into autoformat/tabs-and-i…
nielsdebruin Dec 20, 2024
dc7a404
Add WIP version of visitor
nielsdebruin Dec 20, 2024
a9aacdb
Merge remote-tracking branch 'origin/main' into autoformat/tabs-and-i…
nielsdebruin Dec 20, 2024
5312cef
Add extended test cases
nielsdebruin Dec 30, 2024
63186b4
WIP comment support and further extension
nielsdebruin Dec 30, 2024
62997a5
Add Tree Printer
nielsdebruin Dec 30, 2024
cb80a15
Merge remote-tracking branch 'origin/main' into autoformat/tabs-and-i…
nielsdebruin Jan 8, 2025
8757deb
Merge remote-tracking branch 'origin/main' into autoformat/tabs-and-i…
nielsdebruin Jan 8, 2025
30c9550
Fixes
nielsdebruin Jan 8, 2025
0c9bc40
Add fixed and some extra tests
nielsdebruin Jan 8, 2025
e2b5dd2
Add some trailing comma handling in collection literals
knutwannheden Jan 9, 2025
dfc85b1
Add extra test and some support code for METHOD_SELECT_SUFFIX
nielsdebruin Jan 9, 2025
9e1c596
Reorder
nielsdebruin Jan 9, 2025
cd7e796
Add extra test cases for comments and mark some test with xfail
nielsdebruin Jan 9, 2025
ae81f3c
Polish `TabsAndIndentsVisitor`
knutwannheden Jan 9, 2025
c03597d
Merge branch 'main' into autoformat/tabs-and-indents
nielsdebruin Jan 9, 2025
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
34 changes: 32 additions & 2 deletions rewrite/rewrite/java/support_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def with_comments(self, comments: List[Comment]) -> Space:
_whitespace: Optional[str]

@property
def whitespace(self) -> Optional[str]:
return self._whitespace
def whitespace(self) -> str:
return self._whitespace if self._whitespace is not None else ""

def with_whitespace(self, whitespace: Optional[str]) -> Space:
return self if whitespace is self._whitespace else replace(self, _whitespace=whitespace)
Expand All @@ -127,6 +127,36 @@ def format_first_prefix(cls, trees: List[J2], prefix: Space) -> List[J2]:
return formatted_trees
return trees

@property
def indent(self) -> str:
"""
The indentation after the last newline of either the last comment's suffix
or the global whitespace if no comments exist.
"""
return self._get_whitespace_indent(self.last_whitespace)

@property
def last_whitespace(self) -> str:
"""
The raw suffix from the last comment if it exists, otherwise the global
whitespace (or empty string if whitespace is None).
"""
if self._comments:
return self._comments[-1].suffix
return self._whitespace if self._whitespace is not None else ""

@staticmethod
def _get_whitespace_indent(whitespace: Optional[str]) -> str:
"""
A helper method that extracts everything after the last newline character
in `whitespace`. If no newline is present, returns `whitespace` as-is.
If the last newline is at the end, returns an empty string.
"""
if not whitespace:
return ""
last_newline = whitespace.rfind('\n')
return whitespace if last_newline == -1 else whitespace[last_newline + 1:]

EMPTY: ClassVar[Space]
SINGLE_SPACE: ClassVar[Space]

Expand Down
12 changes: 11 additions & 1 deletion rewrite/rewrite/python/format/auto_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from .normalize_tabs_or_spaces import NormalizeTabsOrSpacesVisitor
from .remove_trailing_whitespace_visitor import RemoveTrailingWhitespaceVisitor
from .spaces_visitor import SpacesVisitor
from .. import TabsAndIndentsStyle, GeneralFormatStyle
from .tabs_and_indents_visitor import TabsAndIndentsVisitor
from .. import TabsAndIndentsStyle, GeneralFormatStyle, WrappingAndBracesStyle
from ..style import BlankLinesStyle, SpacesStyle, IntelliJ
from ..visitor import PythonVisitor
from ... import Recipe, Tree, Cursor
Expand All @@ -28,13 +29,22 @@ def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) ->
cu = tree if isinstance(tree, JavaSourceFile) else self._cursor.first_enclosing_or_throw(JavaSourceFile)

tree = NormalizeFormatVisitor(self._stop_after).visit(tree, p, self._cursor.fork())

tree = BlankLinesVisitor(cu.get_style(BlankLinesStyle) or IntelliJ.blank_lines(), self._stop_after).visit(tree, p, self._cursor.fork())

tree = SpacesVisitor(cu.get_style(SpacesStyle) or IntelliJ.spaces(), self._stop_after).visit(tree, p, self._cursor.fork())

tree = NormalizeTabsOrSpacesVisitor(
cu.get_style(TabsAndIndentsStyle) or IntelliJ.tabs_and_indents(),
self._stop_after
).visit(tree, p, self._cursor.fork())

tree = TabsAndIndentsVisitor(cu.get_style(TabsAndIndentsStyle) or IntelliJ.tabs_and_indents(),
self._stop_after).visit(tree, p, self._cursor.fork())

tree = NormalizeLineBreaksVisitor(cu.get_style(GeneralFormatStyle) or GeneralFormatStyle(False),
self._stop_after).visit(tree, p, self._cursor.fork())

tree = RemoveTrailingWhitespaceVisitor(self._stop_after).visit(tree, self._cursor.fork())

return tree
Loading
Loading