Skip to content

Commit 8bf9f21

Browse files
Refactored autoFormat and added MethodInvocation argument wrapping
* Refactor the Wrap-/ChopIfTooLong to use MinimizationVisitor to correctly count Method chain / Declaration length * Added more coverage to the MinimizationVisitor and fixed a bug in TryResource visitor not returning updated prefixes and markers * Added more tests for all intelliJ Spaces settings style coverage * Last fixes * Implement MethodCall Argument wrapping * Added tests for Method Invocation Wrapping * Split up the PR as it gets too big to review. * Refactor formatting to do a single edit to the tree's prefix/suffixes if needed. This allows for ChopIfTooLong and WrapIfLong to expect that parent objects will be formatted correctly during calculation of the wrapping phase. This also allows for formatting to remove additional enters which are introduced between items that should not have an enter in between. While this is in line with how auto-code-formatters behave (ignore if comment is present eg.), the impact downstream might be big in a lot of unit tests as they expect a certain style. * Remove files * private again * remove changes to SpacesStyle * eof * reset test * remove unused method * revert test changes * eof * review comments auto-bot * Fix 3 out of 4 failing tests * Rework Groovy visitor and Kotlin visitor to same concepts * bot comments * bot comments * fix case label suffix from disappearing * fix case label suffix from disappearing * fix case label suffix from disappearing + improved test coverage for 2 missed cases * Groovy specific SpacesVisitor * Fixed failing tests * Fixed failing tests * bot * tests * altered expectation to match intelliJ * Review comment * Remove unused package declarations * Remove more unused package declarations * Review comment * Add Nullable to reduce warning count * Reduce impact downstream compilation --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent cdf1a7a commit 8bf9f21

50 files changed

Lines changed: 11608 additions & 2716 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: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@
1717

1818
import org.jspecify.annotations.Nullable;
1919
import org.openrewrite.SourceFile;
20+
import org.openrewrite.Tree;
2021
import org.openrewrite.internal.StringUtils;
2122
import org.openrewrite.marker.Markers;
2223

2324
import java.lang.reflect.Field;
2425
import java.lang.reflect.InvocationTargetException;
2526
import java.lang.reflect.Method;
26-
import java.util.Collection;
27-
import java.util.HashSet;
28-
import java.util.List;
29-
import java.util.Set;
27+
import java.util.*;
3028
import java.util.function.Supplier;
3129

30+
import static java.util.Collections.emptySet;
31+
import static java.util.stream.Collectors.joining;
32+
3233
public class StyleHelper {
3334

3435
private static final Set<Class<?>> primitiveWrapperClasses = new HashSet<>();
@@ -57,9 +58,9 @@ private static boolean isEnum(Object value) {
5758
/**
5859
* Copies all non-null properties from right into left, recursively. Assumes use of @With from project lombok.
5960
*
60-
* @param left left object, target of merged properties
61+
* @param left left object, target of merged properties
6162
* @param right right object, source of merged properties
62-
* @param <T> Type of left and right
63+
* @param <T> Type of left and right
6364
* @return left object with merged properties from right
6465
*/
6566
public static <T> T merge(T left, T right) {
@@ -119,6 +120,14 @@ public static <T extends SourceFile> T addStyleMarker(T t, List<NamedStyles> sty
119120
return t;
120121
}
121122

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+
122131
public static <S extends Style, T extends SourceFile> @Nullable S getStyle(Class<S> styleClass, List<NamedStyles> styles, T sourceFile) {
123132
S projectStyle = Style.from(styleClass, sourceFile);
124133
S style = NamedStyles.merge(styleClass, styles);
@@ -139,4 +148,21 @@ public static <S extends Style, T extends SourceFile> S getStyle(Class<S> styleC
139148
}
140149
return projectStyle;
141150
}
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+
}
142168
}

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

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

3326
public class AutoFormatVisitor<P> extends GroovyIsoVisitor<P> {
3427
@Nullable
@@ -49,39 +42,20 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) {
4942
(JavaSourceFile) tree :
5043
cursor.firstEnclosingOrThrow(JavaSourceFile.class);
5144

52-
J t = new NormalizeFormatVisitor<>(stopAfter).visit(tree, 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());
45+
tree = new OmitParenthesesForLastArgumentLambdaVisitor<>(stopAfter).visitNonNull(tree, p, cursor.fork());
8046

47+
// Format the tree in multiple passes to visitors that "enlarge" the space (Eg. first spaces, then wrapping, then indents...)
48+
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());
8155
t = new RemoveTrailingWhitespaceVisitor<>(stopAfter).visit(t, p, cursor.fork());
56+
t = new MinimumViableSpacingVisitor<>(stopAfter).visitNonNull(t, p, cursor.fork());
8257

83-
t = new OmitParenthesesForLastArgumentLambdaVisitor<>(stopAfter).visitNonNull(t, p, cursor.fork());
84-
85-
return new MinimumViableSpacingVisitor<>(stopAfter).visitNonNull(t, p, cursor.fork());
58+
// With the updated tree, overwrite the original space with the newly computed space
59+
return new MergeSpacesVisitor().visit(tree, t);
8660
}
8761
}

0 commit comments

Comments
 (0)