Skip to content

Commit 522827a

Browse files
authored
Revert "Refactored autoFormat and added MethodInvocation argument wrapping" (#6329)
This reverts commit 8bf9f21.
1 parent d48d707 commit 522827a

50 files changed

Lines changed: 2711 additions & 11603 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

rewrite-core/src/main/java/org/openrewrite/style/StyleHelper.java

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717

1818
import org.jspecify.annotations.Nullable;
1919
import org.openrewrite.SourceFile;
20-
import org.openrewrite.Tree;
2120
import org.openrewrite.internal.StringUtils;
2221
import org.openrewrite.marker.Markers;
2322

2423
import java.lang.reflect.Field;
2524
import java.lang.reflect.InvocationTargetException;
2625
import java.lang.reflect.Method;
27-
import java.util.*;
26+
import java.util.Collection;
27+
import java.util.HashSet;
28+
import java.util.List;
29+
import java.util.Set;
2830
import java.util.function.Supplier;
2931

30-
import static java.util.Collections.emptySet;
31-
import static java.util.stream.Collectors.joining;
32-
3332
public class StyleHelper {
3433

3534
private static final Set<Class<?>> primitiveWrapperClasses = new HashSet<>();
@@ -58,9 +57,9 @@ private static boolean isEnum(Object value) {
5857
/**
5958
* Copies all non-null properties from right into left, recursively. Assumes use of @With from project lombok.
6059
*
61-
* @param left left object, target of merged properties
60+
* @param left left object, target of merged properties
6261
* @param right right object, source of merged properties
63-
* @param <T> Type of left and right
62+
* @param <T> Type of left and right
6463
* @return left object with merged properties from right
6564
*/
6665
public static <T> T merge(T left, T right) {
@@ -120,14 +119,6 @@ public static <T extends SourceFile> T addStyleMarker(T t, List<NamedStyles> sty
120119
return t;
121120
}
122121

123-
public static <S extends Style> S getStyle(Class<S> styleClass, List<NamedStyles> styles, Supplier<S> defaultStyle) {
124-
S style = NamedStyles.merge(styleClass, styles);
125-
if (style != null) {
126-
return StyleHelper.merge(defaultStyle.get(), style);
127-
}
128-
return defaultStyle.get();
129-
}
130-
131122
public static <S extends Style, T extends SourceFile> @Nullable S getStyle(Class<S> styleClass, List<NamedStyles> styles, T sourceFile) {
132123
S projectStyle = Style.from(styleClass, sourceFile);
133124
S style = NamedStyles.merge(styleClass, styles);
@@ -148,21 +139,4 @@ public static <S extends Style, T extends SourceFile> S getStyle(Class<S> styleC
148139
}
149140
return projectStyle;
150141
}
151-
152-
public static <S extends Style> @Nullable S getStyle(Class<S> styleClass, List<NamedStyles> styles) {
153-
S style = NamedStyles.merge(styleClass, styles);
154-
if (style != null) {
155-
return (S) style.applyDefaults();
156-
}
157-
return null;
158-
}
159-
160-
public static NamedStyles fromStyles(Style... styles) {
161-
return new NamedStyles(Tree.randomId(),
162-
"MergedStyles",
163-
"Merged styles",
164-
"Merged styles from " + Arrays.stream(styles).map(Object::getClass).map(Class::getSimpleName).collect(joining(", ")),
165-
emptySet(),
166-
Arrays.asList(styles));
167-
}
168142
}

rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@
2020
import org.openrewrite.Tree;
2121
import org.openrewrite.groovy.GroovyIsoVisitor;
2222
import org.openrewrite.java.format.*;
23+
import org.openrewrite.java.style.*;
2324
import org.openrewrite.java.tree.J;
2425
import 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

2633
public 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

Comments
 (0)