Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/style/NamedStyles.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,27 @@ public class NamedStyles implements Marker {
Set<String> tags;
Collection<Style> styles;

/**
* Returns the effective style for a given style class.
* There may be multiple named styles which provide the same style class, in whole or in part.
* Consider the example of a formatting recipe which want so to know whether to use tabs or spaces for indentation.
* The build plugins will attach one or more NamedStyles which provide TabsAndIndents.
* Autodetect and Checkstyle may both provide some or all of TabsAndBraces.
* Autodetect is a weaker signal than explicit configuration so it is attached earlier in the parsing process.
* Therefore, this method gives precedence to non-null values from styles which appear later in the list.
*
* @param styleClass the style class whose effective value is being queried for.
* @param namedStyles the list of named styles to extract a specific effective style from.
* @return the effective style as computed from the list of named styles.
* @param <S> a style class.
*/
@SuppressWarnings("unchecked")
public static <S extends Style> @Nullable S merge(Class<S> styleClass,
Iterable<? extends NamedStyles> namedStyles) {
S merged = null;
for (NamedStyles namedStyle : namedStyles) {
Collection<Style> styles = namedStyle.styles;
//noinspection ConstantValue
if (styles != null) {
for (Style style : styles) {
if (styleClass.isInstance(style)) {
Expand Down Expand Up @@ -97,6 +112,7 @@ public class NamedStyles implements Marker {

List<Style> mergedStyles = new ArrayList<>(styleClasses.size());
for (Class<? extends Style> styleClass : styleClasses) {
//noinspection DataFlowIssue
mergedStyles.add(NamedStyles.merge(styleClass, styles));
}

Expand Down
4 changes: 2 additions & 2 deletions rewrite-core/src/main/java/org/openrewrite/style/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ default String getJacksonPolymorphicTypeTag() {
return getClass().getName();
}

default Style merge(Style lowerPrecedence) {
return this;
default Style merge(Style higherPriority) {
return StyleHelper.merge(this, higherPriority);
}

default Style applyDefaults() { return this; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ protected List<FieldAccessor> computeValue(Class<?> styleClass) {
*/
public static <T> T merge(T left, T right) {
Class<?> styleClass = left.getClass();
if (right.getClass() != styleClass) {
throw new RuntimeException(left.getClass().getName() + " and " + right.getClass().getName() + " should match exactly.");
}
for (FieldAccessor accessor : FIELD_ACCESSORS.get(styleClass)) {
try {
Object rightValue = accessor.getGetter().invoke(right);
Expand All @@ -118,6 +115,7 @@ public static <T> T merge(T left, T right) {
return left;
}

@SuppressWarnings("unused")
public static <T extends SourceFile> T addStyleMarker(T t, List<NamedStyles> styles) {
if (!styles.isEmpty()) {
Set<NamedStyles> newNamedStyles = new HashSet<>(styles);
Expand Down Expand Up @@ -170,6 +168,7 @@ public static <S extends Style, T extends SourceFile> S getStyle(Class<S> styleC
public static <S extends Style> @Nullable S getStyle(Class<S> styleClass, List<NamedStyles> styles) {
S style = NamedStyles.merge(styleClass, styles);
if (style != null) {
//noinspection unchecked
return (S) style.applyDefaults();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public AutoFormatVisitor(@Nullable Tree stopAfter) {

@Override
public J visit(@Nullable Tree tree, P p, Cursor cursor) {
if (tree == null) {
tree = cursor.getValue();
}
JavaSourceFile cu = (tree instanceof JavaSourceFile) ?
(JavaSourceFile) tree :
cursor.firstEnclosingOrThrow(JavaSourceFile.class);
Expand All @@ -51,14 +54,14 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) {

// Format the tree in multiple passes to visitors that "enlarge" the space (Eg. first spaces, then wrapping, then indents...)
J t = new NormalizeFormatVisitor<>(stopAfter).visitNonNull(tree, p, cursor.fork());
t = new SpacesVisitor<>(activeStyles, stopAfter).visit(t, p, cursor.fork());
t = new SpacesVisitor<>(activeStyles, stopAfter).visitNonNull(t, p, cursor.fork());
t = new WrappingAndBracesVisitor<>(activeStyles, stopAfter).visitNonNull(t, p, cursor.fork());
t = new NormalizeTabsOrSpacesVisitor<>(activeStyles, stopAfter).visitNonNull(t, p, cursor.fork());
t = new TabsAndIndentsVisitor<>(activeStyles, stopAfter).visitNonNull(t, p, cursor.fork());
t = new MinimumViableSpacingVisitor<>(stopAfter).visitNonNull(t, p, cursor.fork());

// With the updated tree, overwrite the original space with the newly computed space
tree = new MergeSpacesVisitor(activeStyles).visit(tree, t, cursor.fork());
tree = new MergeSpacesVisitor(activeStyles).visitNonNull(tree, t, cursor.fork());

// Then apply formatting that applies on line-endings / #lines / ...
tree = new BlankLinesVisitor<>(activeStyles, stopAfter).visitNonNull(tree, p, cursor.fork());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ private <T extends SourceFile> T withStyles(T cu, List<NamedStyles> parsedStyles
if (allPresent) {
return cu;
}
// New styles must appear first to take precedence
// New styles must appear last to take precedence
List<Marker> markers = cu.getMarkers().getMarkers();
for (NamedStyles namedStyle : parsedStyles) {
if (existingStyles.stream().noneMatch(es -> namedStylesEqual(namedStyle, es))) {
markers = ListUtils.concat(namedStyle, markers);
markers = ListUtils.concat(markers, namedStyle);
}
}
return cu.withMarkers(Markers.build(markers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) {
if (tree == null) {
tree = cursor.getValue();
}
List<NamedStyles> activeStyles = new ArrayList<>(styles);
activeStyles.addAll(cu.getMarkers().findAll(NamedStyles.class));
List<NamedStyles> activeStyles = new ArrayList<>(cu.getMarkers().findAll(NamedStyles.class));
activeStyles.addAll(styles);

// Format the tree in multiple passes to visitors that "enlarge" the space (Eg. first spaces, then wrapping, then indents...)
J t = new NormalizeFormatVisitor<>(stopAfter).visitNonNull(tree, p, cursor.fork());
Expand All @@ -69,7 +69,7 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) {
t = new TabsAndIndentsVisitor<>(activeStyles, stopAfter).visitNonNull(t, p, cursor.fork());

// With the updated tree, overwrite the original space with the newly computed space
tree = new MergeSpacesVisitor(activeStyles).visit(tree, t, cursor.fork());
tree = new MergeSpacesVisitor(activeStyles).visitNonNull(tree, t, cursor.fork());

// Then apply formatting that applies on line-endings / #lines / ...
tree = new BlankLinesVisitor<>(activeStyles, stopAfter).visitNonNull(tree, p, cursor.fork());
Expand All @@ -93,8 +93,8 @@ public J visit(@Nullable Tree tree, P p) {
if (!(cu instanceof J.CompilationUnit)) {
return cu;
}
List<NamedStyles> activeStyles = new ArrayList<>(styles);
activeStyles.addAll(cu.getMarkers().findAll(NamedStyles.class));
List<NamedStyles> activeStyles = new ArrayList<>(cu.getMarkers().findAll(NamedStyles.class));
activeStyles.addAll(styles);

// Format the tree in multiple passes to visitors that "enlarge" the space (Eg. first spaces, then wrapping, then indents...)
JavaSourceFile t = (JavaSourceFile) new NormalizeFormatVisitor<>(stopAfter).visitNonNull(tree, p);
Expand All @@ -105,7 +105,7 @@ public J visit(@Nullable Tree tree, P p) {
t = (JavaSourceFile) new TabsAndIndentsVisitor<>(activeStyles, stopAfter).visitNonNull(t, p);

// With the updated tree, overwrite the original space with the newly computed space
tree = new MergeSpacesVisitor(activeStyles).visit(tree, t);
tree = new MergeSpacesVisitor(activeStyles).visitNonNull(tree, t);

// Then apply formatting that applies on line-endings / #lines / ...
tree = new BlankLinesVisitor<>(activeStyles, stopAfter).visitNonNull(tree, p);
Expand All @@ -116,6 +116,7 @@ public J visit(@Nullable Tree tree, P p) {
return addStyleMarker((JavaSourceFile) tree, styles);
}
}
//noinspection DataFlowIssue
return (J) tree;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ private Checkstyle() {
operatorWrapStyle(),
typecastParenPadStyle(),
unnecessaryParentheses(),
customImportOrderStyle(),
unusedImportsStyle()
));
}
Expand All @@ -72,10 +71,11 @@ public static DefaultComesLastStyle defaultComesLast() {
return new DefaultComesLastStyle(false);
}

public static final EmptyBlockStyle.BlockPolicy defaultBlockPolicy = EmptyBlockStyle.BlockPolicy.TEXT;
// https://checkstyle.sourceforge.io/checks/blocks/emptyblock.html
public static final EmptyBlockStyle.BlockPolicy defaultBlockPolicy = EmptyBlockStyle.BlockPolicy.STATEMENT;

public static EmptyBlockStyle emptyBlock() {
return new EmptyBlockStyle(EmptyBlockStyle.BlockPolicy.TEXT, true, true, true, true,
return new EmptyBlockStyle(EmptyBlockStyle.BlockPolicy.STATEMENT, true, true, true, true,
true, true, true, true, true, true, true, true);
}

Expand Down Expand Up @@ -107,9 +107,10 @@ public static NeedBracesStyle needBracesStyle() {
return new NeedBracesStyle(false, false);
}

// https://checkstyle.sourceforge.io/checks/whitespace/nowhitespaceafter.html
public static NoWhitespaceAfterStyle noWhitespaceAfterStyle() {
return new NoWhitespaceAfterStyle(true, false, false, true, true, true,
true, false, true, true, true, true, true, true);
true, true, true, true, true, true, true, true);
}

public static NoWhitespaceBeforeStyle noWhitespaceBeforeStyle() {
Expand Down Expand Up @@ -150,13 +151,6 @@ public static MethodParamPadStyle methodParamPadStyle() {
return new MethodParamPadStyle(false, false);
}

public static CustomImportOrderStyle customImportOrderStyle() {
return new CustomImportOrderStyle(Arrays.asList(new CustomImportOrderStyle.GroupWithDepth(CustomImportOrderStyle.CustomImportOrderGroup.STATIC, null),
new CustomImportOrderStyle.GroupWithDepth(CustomImportOrderStyle.CustomImportOrderGroup.STANDARD_JAVA_PACKAGE, null),
new CustomImportOrderStyle.GroupWithDepth(CustomImportOrderStyle.CustomImportOrderGroup.THIRD_PARTY_PACKAGE, null)),
true, false, "^$", "^(java|javax)\\.", ".*");
}

private static UnusedImportsStyle unusedImportsStyle() {
return new UnusedImportsStyle(false);
}
Expand Down
Loading