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
3 changes: 3 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/SourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite;

import com.google.errorprone.annotations.InlineMe;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.style.Style;
Expand Down Expand Up @@ -74,6 +75,7 @@ default boolean printEqualsInput(Parser.Input input, ExecutionContext ctx) {
* @deprecated Use {@link org.openrewrite.style.Style#from(Class, SourceFile)} instead.
*/
@Deprecated
@InlineMe(replacement = "Style.from(styleClass, this)", imports = "org.openrewrite.style.Style")
default <S extends Style> @Nullable S getStyle(Class<S> styleClass) {
return Style.from(styleClass, this);
}
Expand All @@ -82,6 +84,7 @@ default boolean printEqualsInput(Parser.Input input, ExecutionContext ctx) {
* @deprecated Use {@link org.openrewrite.style.Style#from(Class, SourceFile, Supplier)} instead.
*/
@Deprecated
@InlineMe(replacement = "Style.from(styleClass, this, () -> defaultStyle)", imports = "org.openrewrite.style.Style")
default <S extends Style> S getStyle(Class<S> styleClass, S defaultStyle) {
return Style.from(styleClass, this, () -> defaultStyle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) {

t = new SpacesVisitor<>(
spacesStyle,
cu.getStyle(EmptyForInitializerPadStyle.class),
Style.from(EmptyForInitializerPadStyle.class, cu),
Style.from(EmptyForIteratorPadStyle.class, cu),
stopAfter
).visit(t, p, cursor.fork());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.style.Style;

import java.util.Optional;

Expand All @@ -47,7 +48,7 @@ private static class OmitParenthesesFromCompilationUnitStyle extends JavaIsoVisi
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
SourceFile cu = (SourceFile) requireNonNull(tree);
OmitParenthesesStyle style = Optional.ofNullable(cu.getStyle(OmitParenthesesStyle.class)).orElse(OmitParenthesesStyle.DEFAULT);
OmitParenthesesStyle style = Style.from(OmitParenthesesStyle.class, cu, () -> OmitParenthesesStyle.DEFAULT);
if (style.getLastArgumentLambda()) {
doAfterVisit(new OmitParenthesesForLastArgumentLambda().getVisitor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* In Groovy methods can be declared with a return type and also a redundant 'def' keyword.
* This captures the extra def keyword.
* @deprecated The `def` keyword is now parsed as a {@link J.Modifier.Type.LanguageExtension} type.
* @deprecated The `def` keyword is now parsed as a {@link J.Modifier.Type#LanguageExtension} type.
*/
@Value
@With
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.openrewrite.*;
import org.openrewrite.hcl.HclIsoVisitor;
import org.openrewrite.hcl.tree.Hcl;
import org.openrewrite.style.Style;

public class BlankLines extends Recipe {

Expand All @@ -39,17 +40,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private static class BlankLinesFromCompilationUnitStyle extends HclIsoVisitor<ExecutionContext> {
@Override
public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile configFile, ExecutionContext ctx) {
BlankLinesStyle style = configFile.getStyle(BlankLinesStyle.class);
if (style == null) {
style = BlankLinesStyle.DEFAULT;
}
BlankLinesStyle style = Style.from(BlankLinesStyle.class, configFile, () -> BlankLinesStyle.DEFAULT);
return (Hcl.ConfigFile) new BlankLinesVisitor<>(style).visitNonNull(configFile, ctx);
}
}

public static <H extends Hcl> H formatBlankLines(Hcl j, Cursor cursor) {
BlankLinesStyle style = cursor.firstEnclosingOrThrow(SourceFile.class)
.getStyle(BlankLinesStyle.class);
BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class));
//noinspection unchecked
return (H) new BlankLinesVisitor<>(style == null ? BlankLinesStyle.DEFAULT : style)
.visitNonNull(j, 0, cursor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.hcl.HclIsoVisitor;
import org.openrewrite.hcl.style.SpacesStyle;
import org.openrewrite.hcl.tree.Hcl;
import org.openrewrite.style.Style;

public class Spaces extends Recipe {

Expand All @@ -42,10 +43,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private static class SpacesFromCompilationUnitStyle extends HclIsoVisitor<ExecutionContext> {
@Override
public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) {
SpacesStyle style = cf.getStyle(SpacesStyle.class);
if (style == null) {
style = SpacesStyle.DEFAULT;
}
SpacesStyle style = Style.from(SpacesStyle.class, cf, () -> SpacesStyle.DEFAULT);
return (Hcl.ConfigFile) new SpacesVisitor<>(style).visitNonNull(cf, ctx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.hcl.HclIsoVisitor;
import org.openrewrite.hcl.style.TabsAndIndentsStyle;
import org.openrewrite.hcl.tree.Hcl;
import org.openrewrite.style.Style;

public class TabsAndIndents extends Recipe {

Expand All @@ -42,10 +43,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private static class TabsAndIndentsFromCompilationUnitStyle extends HclIsoVisitor<ExecutionContext> {
@Override
public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) {
TabsAndIndentsStyle style = cf.getStyle(TabsAndIndentsStyle.class);
if (style == null) {
style = TabsAndIndentsStyle.DEFAULT;
}
TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cf, () -> TabsAndIndentsStyle.DEFAULT);
return (Hcl.ConfigFile) new TabsAndIndentsVisitor<>(style).visitNonNull(cf, ctx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.java.style.ImportLayoutStyle;
import org.openrewrite.java.style.IntelliJ;
import org.openrewrite.java.tree.*;
import org.openrewrite.style.Style;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -58,8 +59,7 @@ public RemoveImport(String type, boolean force) {
J j = tree;
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) tree;
ImportLayoutStyle importLayoutStyle = Optional.ofNullable(((SourceFile) cu).getStyle(ImportLayoutStyle.class))
.orElse(IntelliJ.importLayout());
ImportLayoutStyle importLayoutStyle = Style.from(ImportLayoutStyle.class, cu, IntelliJ::importLayout);

boolean typeUsed = false;
Set<String> otherTypesInPackageUsed = new TreeSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -50,6 +51,7 @@ public static <J2 extends J> TreeVisitor<?, ExecutionContext> modifyOnly(J2 scop
}

@Value
@EqualsAndHashCode(callSuper = false)
private static class SimplifySingleElementAnnotationVisitor extends JavaIsoVisitor<ExecutionContext> {
@Nullable
J scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.style.IntelliJ;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.style.Style;

import static java.util.Objects.requireNonNull;

Expand All @@ -46,21 +47,16 @@ private static class BlankLinesFromCompilationUnitStyle extends JavaIsoVisitor<E
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
BlankLinesStyle style = cu.getStyle(BlankLinesStyle.class);
if (style == null) {
style = IntelliJ.blankLines();
}
BlankLinesStyle style = Style.from(BlankLinesStyle.class, cu, IntelliJ::blankLines);
return new BlankLinesVisitor<>(style).visit(cu, ctx);
}
return (J) tree;
}
}

public static <J2 extends J> J2 formatBlankLines(J j, Cursor cursor) {
BlankLinesStyle style = cursor.firstEnclosingOrThrow(SourceFile.class)
.getStyle(BlankLinesStyle.class);
BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class), IntelliJ::blankLines);
//noinspection unchecked
return (J2) new BlankLinesVisitor<>(style == null ? IntelliJ.blankLines() : style)
.visitNonNull(j, 0, cursor);
return (J2) new BlankLinesVisitor<>(style).visitNonNull(j, 0, cursor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
SourceFile cu = (SourceFile) requireNonNull(tree);
spacesStyle = Optional.ofNullable(cu.getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces());
methodParamPadStyle = Optional.ofNullable(Style.from(MethodParamPadStyle.class, cu)).orElse(Checkstyle.methodParamPadStyle());
spacesStyle = Style.from(SpacesStyle.class, cu, IntelliJ::spaces);
methodParamPadStyle = Style.from(MethodParamPadStyle.class, cu, Checkstyle::methodParamPadStyle);

spacesStyle = spacesStyle.withBeforeParentheses(
spacesStyle.getBeforeParentheses()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.style.GeneralFormatStyle;
import org.openrewrite.style.Style;

import static java.util.Objects.requireNonNull;
import static org.openrewrite.java.format.AutodetectGeneralFormatStyle.autodetectGeneralFormatStyle;
Expand Down Expand Up @@ -48,10 +49,7 @@ private static class LineBreaksFromCompilationUnitStyle extends JavaIsoVisitor<E
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
GeneralFormatStyle generalFormatStyle = ((SourceFile) cu).getStyle(GeneralFormatStyle.class);
if (generalFormatStyle == null) {
generalFormatStyle = autodetectGeneralFormatStyle(cu);
}
GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, cu, () -> autodetectGeneralFormatStyle(cu));
doAfterVisit(new NormalizeLineBreaksVisitor<>(generalFormatStyle));
}
return (J) tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.style.TabsAndIndentsStyle;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.style.Style;

import static java.util.Objects.requireNonNull;

Expand All @@ -47,10 +48,7 @@ private static class TabsAndIndentsFromCompilationUnitStyle extends JavaIsoVisit
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
TabsAndIndentsStyle style = ((SourceFile) cu).getStyle(TabsAndIndentsStyle.class);
if (style == null) {
style = IntelliJ.tabsAndIndents();
}
TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents);
return new NormalizeTabsOrSpacesVisitor<>(style).visit(cu, ctx);
}
return (J) tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
SourceFile cu = (SourceFile) requireNonNull(tree);
if (cu.getStyle(EmptyForIteratorPadStyle.class) != null || cu.getStyle(EmptyForInitializerPadStyle.class) != null) {
if (Style.from(EmptyForIteratorPadStyle.class, cu) != null || Style.from(EmptyForInitializerPadStyle.class, cu) != null) {
return cu.withMarkers(cu.getMarkers().add(new SearchResult(randomId(), null)));
}
return (JavaSourceFile) cu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.Space;
import org.openrewrite.style.Style;

import java.util.Objects;
import java.util.Optional;
Expand All @@ -39,8 +40,7 @@ private ShiftFormat() {

public static <J2 extends J> J2 indent(J j, Cursor cursor, int shift) {
JavaSourceFile cu = cursor.firstEnclosingOrThrow(JavaSourceFile.class);
TabsAndIndentsStyle tabsAndIndents = Optional.ofNullable(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class))
.orElse(IntelliJ.tabsAndIndents());
TabsAndIndentsStyle tabsAndIndents = Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents);

//noinspection unchecked
return (J2) Objects.requireNonNull(new JavaIsoVisitor<Integer>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) tree;
SpacesStyle style = cu.getStyle(SpacesStyle.class);
if (style == null) {
style = IntelliJ.spaces();
}
return new SpacesVisitor<>(style, cu.getStyle(EmptyForInitializerPadStyle.class),
cu.getStyle(EmptyForIteratorPadStyle.class)).visit(cu, ctx);
return new SpacesVisitor<>(
Style.from(SpacesStyle.class, cu, IntelliJ::spaces),
Style.from(EmptyForInitializerPadStyle.class, cu),
Style.from(EmptyForIteratorPadStyle.class, cu))
.visit(cu, ctx);
}
return super.visit(tree, ctx);
}
Expand All @@ -64,7 +63,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {

public static <J2 extends J> J2 formatSpaces(J j, Cursor cursor) {
SourceFile cu = cursor.firstEnclosingOrThrow(SourceFile.class);
SpacesStyle style = cu.getStyle(SpacesStyle.class);
SpacesStyle style = Style.from(SpacesStyle.class, cu);
//noinspection unchecked
return (J2) new SpacesVisitor<>(style == null ? IntelliJ.spaces() : style,
Style.from(EmptyForInitializerPadStyle.class, cu),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.java.style.TypecastParenPadStyle;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.style.Style;

import java.util.Optional;

Expand Down Expand Up @@ -55,9 +56,8 @@ private static class TypecastParenPadVisitor extends JavaIsoVisitor<ExecutionCon
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
SourceFile cu = (SourceFile) requireNonNull(tree);
spacesStyle = Optional.ofNullable(cu.getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces());
typecastParenPadStyle = Optional.ofNullable(cu.getStyle(TypecastParenPadStyle.class)).orElse(Checkstyle.typecastParenPadStyle());

spacesStyle = Style.from(SpacesStyle.class, cu, IntelliJ::spaces);
typecastParenPadStyle = Style.from(TypecastParenPadStyle.class, cu, Checkstyle::typecastParenPadStyle);
spacesStyle = spacesStyle.withWithin(spacesStyle.getWithin().withTypeCastParentheses(typecastParenPadStyle.getSpace()));
}
return super.visit(tree, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ private static class WrappingAndBracesCompilationUnitStyle extends JavaIsoVisito
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
WrappingAndBracesStyle style = ((SourceFile) cu).getStyle(WrappingAndBracesStyle.class);
if (style == null) {
style = IntelliJ.wrappingAndBraces();
}
WrappingAndBracesStyle style = Style.from(WrappingAndBracesStyle.class, cu, IntelliJ::wrappingAndBraces);
return new WrappingAndBracesVisitor<>(style).visit(cu, ctx);
}
return (J) tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.marker.Markers;
import org.openrewrite.style.GeneralFormatStyle;
import org.openrewrite.style.Style;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -182,8 +183,7 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String
}
}

ImportLayoutStyle layoutStyle = Optional.ofNullable(cu.getStyle(ImportLayoutStyle.class))
.orElse(IntelliJ.importLayout());
ImportLayoutStyle layoutStyle = Style.from(ImportLayoutStyle.class, cu, IntelliJ::importLayout);

List<JavaType.FullyQualified> classpath = cu.getMarkers().findFirst(JavaSourceSet.class)
.map(JavaSourceSet::getClasspath)
Expand All @@ -192,8 +192,8 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String
List<JRightPadded<J.Import>> newImports = layoutStyle.addImport(cu.getPadding().getImports(), importToAdd, cu.getPackageDeclaration(), classpath);

// ImportLayoutStyle::addImport adds always `\n` as newlines. Checking if we need to fix them
GeneralFormatStyle generalFormatStyle = Optional.ofNullable(cu.getStyle(GeneralFormatStyle.class))
.orElse(autodetectGeneralFormatStyle(cu));
K.CompilationUnit finalCu = cu;
GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, cu, ()-> autodetectGeneralFormatStyle(finalCu));
newImports = checkCRLF(newImports, generalFormatStyle);

cu = cu.getPadding().withImports(newImports);
Expand Down
Loading