Skip to content

Commit d6ef897

Browse files
Tabs and Indents visitor (#104)
* Add test cases for tabs and indents visitor * Add extra test cases for autoformat * More tests * Add helper methods * Add helper method to cursor * Add WIP version of visitor * Add extended test cases * WIP comment support and further extension * Add Tree Printer * Fixes * Add fixed and some extra tests * Add some trailing comma handling in collection literals * Add extra test and some support code for METHOD_SELECT_SUFFIX * Reorder * Add extra test cases for comments and mark some test with xfail * Polish `TabsAndIndentsVisitor` --------- Co-authored-by: Knut Wannheden <knut@moderne.io>
1 parent fe1a22a commit d6ef897

File tree

7 files changed

+1349
-7
lines changed

7 files changed

+1349
-7
lines changed

rewrite/rewrite/java/support_types.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def with_comments(self, comments: List[Comment]) -> Space:
106106
_whitespace: Optional[str]
107107

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

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

130+
@property
131+
def indent(self) -> str:
132+
"""
133+
The indentation after the last newline of either the last comment's suffix
134+
or the global whitespace if no comments exist.
135+
"""
136+
return self._get_whitespace_indent(self.last_whitespace)
137+
138+
@property
139+
def last_whitespace(self) -> str:
140+
"""
141+
The raw suffix from the last comment if it exists, otherwise the global
142+
whitespace (or empty string if whitespace is None).
143+
"""
144+
if self._comments:
145+
return self._comments[-1].suffix
146+
return self._whitespace if self._whitespace is not None else ""
147+
148+
@staticmethod
149+
def _get_whitespace_indent(whitespace: Optional[str]) -> str:
150+
"""
151+
A helper method that extracts everything after the last newline character
152+
in `whitespace`. If no newline is present, returns `whitespace` as-is.
153+
If the last newline is at the end, returns an empty string.
154+
"""
155+
if not whitespace:
156+
return ""
157+
last_newline = whitespace.rfind('\n')
158+
return whitespace if last_newline == -1 else whitespace[last_newline + 1:]
159+
130160
EMPTY: ClassVar[Space]
131161
SINGLE_SPACE: ClassVar[Space]
132162

rewrite/rewrite/python/format/auto_format.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from .normalize_tabs_or_spaces import NormalizeTabsOrSpacesVisitor
77
from .remove_trailing_whitespace_visitor import RemoveTrailingWhitespaceVisitor
88
from .spaces_visitor import SpacesVisitor
9-
from .. import TabsAndIndentsStyle, GeneralFormatStyle
9+
from .tabs_and_indents_visitor import TabsAndIndentsVisitor
10+
from .. import TabsAndIndentsStyle, GeneralFormatStyle, WrappingAndBracesStyle
1011
from ..style import BlankLinesStyle, SpacesStyle, IntelliJ
1112
from ..visitor import PythonVisitor
1213
from ... import Recipe, Tree, Cursor
@@ -28,13 +29,22 @@ def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) ->
2829
cu = tree if isinstance(tree, JavaSourceFile) else self._cursor.first_enclosing_or_throw(JavaSourceFile)
2930

3031
tree = NormalizeFormatVisitor(self._stop_after).visit(tree, p, self._cursor.fork())
32+
3133
tree = BlankLinesVisitor(cu.get_style(BlankLinesStyle) or IntelliJ.blank_lines(), self._stop_after).visit(tree, p, self._cursor.fork())
34+
3235
tree = SpacesVisitor(cu.get_style(SpacesStyle) or IntelliJ.spaces(), self._stop_after).visit(tree, p, self._cursor.fork())
36+
3337
tree = NormalizeTabsOrSpacesVisitor(
3438
cu.get_style(TabsAndIndentsStyle) or IntelliJ.tabs_and_indents(),
3539
self._stop_after
3640
).visit(tree, p, self._cursor.fork())
41+
42+
tree = TabsAndIndentsVisitor(cu.get_style(TabsAndIndentsStyle) or IntelliJ.tabs_and_indents(),
43+
self._stop_after).visit(tree, p, self._cursor.fork())
44+
3745
tree = NormalizeLineBreaksVisitor(cu.get_style(GeneralFormatStyle) or GeneralFormatStyle(False),
3846
self._stop_after).visit(tree, p, self._cursor.fork())
47+
3948
tree = RemoveTrailingWhitespaceVisitor(self._stop_after).visit(tree, self._cursor.fork())
49+
4050
return tree

0 commit comments

Comments
 (0)