2020import org .openrewrite .Tree ;
2121import org .openrewrite .groovy .GroovyIsoVisitor ;
2222import org .openrewrite .java .format .*;
23+ import org .openrewrite .java .style .*;
2324import org .openrewrite .java .tree .J ;
2425import org .openrewrite .java .tree .JavaSourceFile ;
26+ import org .openrewrite .style .GeneralFormatStyle ;
27+ import org .openrewrite .style .Style ;
28+
29+ import java .util .Optional ;
30+
31+ import static org .openrewrite .java .format .AutodetectGeneralFormatStyle .autodetectGeneralFormatStyle ;
2532
2633public class AutoFormatVisitor <P > extends GroovyIsoVisitor <P > {
2734 @ Nullable
@@ -42,20 +49,39 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) {
4249 (JavaSourceFile ) tree :
4350 cursor .firstEnclosingOrThrow (JavaSourceFile .class );
4451
45- tree = new OmitParenthesesForLastArgumentLambdaVisitor <>(stopAfter ).visitNonNull (tree , p , cursor .fork ());
46-
47- // Format the tree in multiple passes to visitors that "enlarge" the space (Eg. first spaces, then wrapping, then indents...)
4852 J t = new NormalizeFormatVisitor <>(stopAfter ).visit (tree , p , cursor .fork ());
49- t = new BlankLinesVisitor <>(cu , stopAfter ).visit (t , p , cursor .fork ());
50- t = new SpacesVisitor <>(cu , false , stopAfter ).visit (t , p , cursor .fork ());
51- t = new WrappingAndBracesVisitor <>(cu , stopAfter ).visit (t , p , cursor .fork ());
52- t = new NormalizeTabsOrSpacesVisitor <>(cu , stopAfter ).visit (t , p , cursor .fork ());
53- t = new TabsAndIndentsVisitor <>(cu , stopAfter ).visit (t , p , cursor .fork ());
54- t = new NormalizeLineBreaksVisitor <>(cu , stopAfter ).visit (t , p , cursor .fork ());
53+
54+ t = new BlankLinesVisitor <>(Style .from (BlankLinesStyle .class , cu , IntelliJ ::blankLines ), stopAfter )
55+ .visit (t , p , cursor .fork ());
56+
57+ WrappingAndBracesStyle wrappingAndBracesStyle = Style .from (WrappingAndBracesStyle .class , cu , IntelliJ ::wrappingAndBraces );
58+ TabsAndIndentsStyle tabsAndIndentsStyle = Style .from (TabsAndIndentsStyle .class , cu , IntelliJ ::tabsAndIndents );
59+ SpacesStyle spacesStyle = Style .from (SpacesStyle .class , cu , IntelliJ ::spaces );
60+
61+ t = new WrappingAndBracesVisitor <>(spacesStyle , wrappingAndBracesStyle , stopAfter )
62+ .visit (t , p , cursor .fork ());
63+
64+ t = new SpacesVisitor <>(
65+ spacesStyle ,
66+ Style .from (EmptyForInitializerPadStyle .class , cu ),
67+ Style .from (EmptyForIteratorPadStyle .class , cu ),
68+ stopAfter
69+ ).visit (t , p , cursor .fork ());
70+
71+ t = new NormalizeTabsOrSpacesVisitor <>(tabsAndIndentsStyle , stopAfter )
72+ .visit (t , p , cursor .fork ());
73+
74+ t = new TabsAndIndentsVisitor <>(tabsAndIndentsStyle , spacesStyle , wrappingAndBracesStyle , stopAfter )
75+ .visit (t , p , cursor .fork ());
76+
77+ t = new NormalizeLineBreaksVisitor <>(Optional .ofNullable (Style .from (GeneralFormatStyle .class , cu ))
78+ .orElse (autodetectGeneralFormatStyle (cu )), stopAfter )
79+ .visit (t , p , cursor .fork ());
80+
5581 t = new RemoveTrailingWhitespaceVisitor <>(stopAfter ).visit (t , p , cursor .fork ());
56- t = new MinimumViableSpacingVisitor <>(stopAfter ).visitNonNull (t , p , cursor .fork ());
5782
58- // With the updated tree, overwrite the original space with the newly computed space
59- return new MergeSpacesVisitor ().visit (tree , t );
83+ t = new OmitParenthesesForLastArgumentLambdaVisitor <>(stopAfter ).visitNonNull (t , p , cursor .fork ());
84+
85+ return new MinimumViableSpacingVisitor <>(stopAfter ).visitNonNull (t , p , cursor .fork ());
6086 }
6187}
0 commit comments