2424import org .openrewrite .java .tree .J ;
2525import org .openrewrite .java .tree .JavaSourceFile ;
2626import org .openrewrite .style .GeneralFormatStyle ;
27- import org .openrewrite .style .NamedStyles ;
2827import org .openrewrite .style .Style ;
2928
30- import java .util .Arrays ;
31- import java .util .List ;
32- import java .util .function .Supplier ;
29+ import java .util .Optional ;
3330
3431import static java .util .Objects .requireNonNull ;
35- import static java .util .stream .Collectors .toList ;
3632import static org .openrewrite .java .format .AutodetectGeneralFormatStyle .autodetectGeneralFormatStyle ;
3733
3834public class AutoFormatVisitor <P > extends JavaIsoVisitor <P > {
3935 @ Nullable
4036 private final Tree stopAfter ;
4137
42- private final List <NamedStyles > styles ;
43-
4438 public AutoFormatVisitor () {
4539 this (null );
4640 }
4741
48- public AutoFormatVisitor (@ Nullable Tree stopAfter , NamedStyles ... style ) {
42+ public AutoFormatVisitor (@ Nullable Tree stopAfter ) {
4943 this .stopAfter = stopAfter ;
50- this .styles = Arrays .stream (style ).collect (toList ());
5144 }
5245
5346 @ Override
@@ -57,40 +50,41 @@ public boolean isAcceptable(SourceFile sourceFile, P p) {
5750
5851 @ Override
5952 public J visit (@ Nullable Tree tree , P p , Cursor cursor ) {
60- JavaSourceFile cu = (tree instanceof JavaSourceFile ) ? (JavaSourceFile ) tree : cursor .firstEnclosingOrThrow (JavaSourceFile .class );
61- if (tree == null ) {
62- tree = cursor .getValue ();
63- }
53+ JavaSourceFile cu = (tree instanceof JavaSourceFile ) ?
54+ (JavaSourceFile ) tree :
55+ cursor .firstEnclosingOrThrow (JavaSourceFile .class );
6456
65- J t = new NormalizeFormatVisitor <>(stopAfter )
66- .visitNonNull (tree , p , cursor .fork ());
57+ J t = new NormalizeFormatVisitor <>(stopAfter ).visit (tree , p , cursor .fork ());
6758
68- t = new MinimumViableSpacingVisitor <>(stopAfter )
69- .visitNonNull (t , p , cursor .fork ());
59+ t = new MinimumViableSpacingVisitor <>(stopAfter ).visit (t , p , cursor .fork ());
7060
71- t = new BlankLinesVisitor <>(getStyle (BlankLinesStyle .class , cu , IntelliJ ::blankLines ), stopAfter )
72- .visitNonNull (t , p , cursor .fork ());
61+ t = new BlankLinesVisitor <>(Style . from (BlankLinesStyle .class , cu , IntelliJ ::blankLines ), stopAfter )
62+ .visit (t , p , cursor .fork ());
7363
74- t = new WrappingAndBracesVisitor <>(getStyle (WrappingAndBracesStyle .class , cu , IntelliJ ::wrappingAndBraces ), stopAfter )
75- .visitNonNull (t , p , cursor .fork ());
64+ t = new WrappingAndBracesVisitor <>(Style . from (WrappingAndBracesStyle .class , cu , IntelliJ ::wrappingAndBraces ), stopAfter )
65+ .visit (t , p , cursor .fork ());
7666
77- SpacesStyle spacesStyle = getStyle (SpacesStyle .class , cu , IntelliJ ::spaces );
78- TabsAndIndentsStyle tabsAndIndentsStyle = getStyle (TabsAndIndentsStyle .class , cu , IntelliJ ::tabsAndIndents );
67+ SpacesStyle spacesStyle = Style . from (SpacesStyle .class , cu , IntelliJ ::spaces );
68+ TabsAndIndentsStyle tabsAndIndentsStyle = Style . from (TabsAndIndentsStyle .class , cu , IntelliJ ::tabsAndIndents );
7969
80- t = new SpacesVisitor <>(spacesStyle , getStyle (EmptyForInitializerPadStyle .class , cu ), getStyle (EmptyForIteratorPadStyle .class , cu ), stopAfter )
81- .visitNonNull (t , p , cursor .fork ());
70+ t = new SpacesVisitor <>(
71+ spacesStyle ,
72+ Style .from (EmptyForInitializerPadStyle .class , cu ),
73+ Style .from (EmptyForIteratorPadStyle .class , cu ),
74+ stopAfter
75+ ).visit (t , p , cursor .fork ());
8276
8377 t = new NormalizeTabsOrSpacesVisitor <>(tabsAndIndentsStyle , stopAfter )
84- .visitNonNull (t , p , cursor .fork ());
78+ .visit (t , p , cursor .fork ());
8579
8680 t = new TabsAndIndentsVisitor <>(tabsAndIndentsStyle , spacesStyle , stopAfter )
87- .visitNonNull (t , p , cursor .fork ());
81+ .visit (t , p , cursor .fork ());
8882
89- t = new NormalizeLineBreaksVisitor <>(getStyle (GeneralFormatStyle .class , cu , () -> autodetectGeneralFormatStyle (cu )), stopAfter )
90- .visitNonNull (t , p , cursor .fork ());
83+ t = new NormalizeLineBreaksVisitor <>(Optional .ofNullable (Style .from (GeneralFormatStyle .class , cu ))
84+ .orElse (autodetectGeneralFormatStyle (cu )), stopAfter )
85+ .visit (t , p , cursor .fork ());
9186
92- return new RemoveTrailingWhitespaceVisitor <>(stopAfter )
93- .visitNonNull (t , p , cursor .fork ());
87+ return new RemoveTrailingWhitespaceVisitor <>(stopAfter ).visit (t , p , cursor .fork ());
9488 }
9589
9690 @ Override
@@ -103,43 +97,32 @@ public J visit(@Nullable Tree tree, P p) {
10397 if (!(cu instanceof J .CompilationUnit )) {
10498 return cu ;
10599 }
106- JavaSourceFile t = (JavaSourceFile ) new RemoveTrailingWhitespaceVisitor <>(stopAfter )
107- .visitNonNull (cu , p );
100+ JavaSourceFile t = (JavaSourceFile ) new RemoveTrailingWhitespaceVisitor <>(stopAfter ).visit (cu , p );
108101
109- t = (JavaSourceFile ) new BlankLinesVisitor <>(getStyle (BlankLinesStyle .class , cu , IntelliJ ::blankLines ), stopAfter )
110- .visitNonNull (t , p );
102+ t = (JavaSourceFile ) new BlankLinesVisitor <>(Style . from (BlankLinesStyle .class , cu , IntelliJ ::blankLines ), stopAfter )
103+ .visit (t , p );
111104
112- SpacesStyle spacesStyle = getStyle ( SpacesStyle .class , cu , IntelliJ :: spaces );
113- TabsAndIndentsStyle tabsAndIndentsStyle = getStyle (TabsAndIndentsStyle .class , cu , IntelliJ ::tabsAndIndents );
105+ SpacesStyle spacesStyle = Optional . ofNullable ( Style . from ( SpacesStyle .class , cu )). orElse ( IntelliJ . spaces () );
106+ TabsAndIndentsStyle tabsAndIndentsStyle = Style . from (TabsAndIndentsStyle .class , cu , IntelliJ ::tabsAndIndents );
114107
115- t = (JavaSourceFile ) new SpacesVisitor <P >(spacesStyle , getStyle (EmptyForInitializerPadStyle .class , cu ), getStyle (EmptyForIteratorPadStyle .class , cu ), stopAfter )
116- .visitNonNull (t , p );
108+ t = (JavaSourceFile ) new SpacesVisitor <P >(spacesStyle ,
109+ Style .from (EmptyForInitializerPadStyle .class , cu ),
110+ Style .from (EmptyForIteratorPadStyle .class , cu ),
111+ stopAfter )
112+ .visit (t , p );
117113
118- t = (JavaSourceFile ) new WrappingAndBracesVisitor <>(getStyle (WrappingAndBracesStyle .class , cu , IntelliJ ::wrappingAndBraces ), stopAfter )
119- .visitNonNull (t , p );
114+ t = (JavaSourceFile ) new WrappingAndBracesVisitor <>(Style . from (WrappingAndBracesStyle .class , cu , IntelliJ ::wrappingAndBraces ), stopAfter )
115+ .visit (t , p );
120116
121117 t = (JavaSourceFile ) new NormalizeTabsOrSpacesVisitor <>(tabsAndIndentsStyle , stopAfter )
122- .visitNonNull (t , p );
118+ .visit (t , p );
123119
124- return new TabsAndIndentsVisitor <>(tabsAndIndentsStyle , spacesStyle , stopAfter )
125- .visitNonNull (t , p );
126- }
127- return (J ) tree ;
128- }
120+ t = (JavaSourceFile ) new TabsAndIndentsVisitor <>(tabsAndIndentsStyle , spacesStyle , stopAfter )
121+ .visit (t , p );
129122
130- private <S extends Style > @ Nullable S getStyle (Class <S > styleClass , JavaSourceFile sourceFile ) {
131- S style = NamedStyles .merge (styleClass , styles );
132- if (style != null ) {
133- return style ;
123+ assert t != null ;
124+ return t ;
134125 }
135- return Style .from (styleClass , sourceFile );
136- }
137-
138- private <S extends Style > S getStyle (Class <S > styleClass , JavaSourceFile sourceFile , Supplier <S > defaultStyle ) {
139- S style = NamedStyles .merge (styleClass , styles );
140- if (style != null ) {
141- return style ;
142- }
143- return Style .from (styleClass , sourceFile , defaultStyle );
126+ return (J ) tree ;
144127 }
145128}
0 commit comments