Skip to content

Commit 75b6048

Browse files
Adopt Style.from instead of SourceFile.getStyle (#859)
* StyleFrom Use this link to re-run the recipe: https://app.moderne.io/builder/6OMjIdVBh?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne <[email protected]> * Replace Optional with method that takes a default value * Polish * Replace null check with default --------- Co-authored-by: Moderne <[email protected]>
1 parent 7082b93 commit 75b6048

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

src/main/java/org/openrewrite/java/migrate/UseTabsOrSpaces.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openrewrite.java.style.TabsAndIndentsStyle;
2626
import org.openrewrite.java.tree.J;
2727
import org.openrewrite.java.tree.JavaSourceFile;
28+
import org.openrewrite.style.Style;
2829

2930
import static java.util.Objects.requireNonNull;
3031

@@ -43,7 +44,7 @@ public String getDisplayName() {
4344
@Override
4445
public String getDescription() {
4546
return "This is useful for one-off migrations of a codebase that has mixed indentation styles, while " +
46-
"preserving all other auto-detected formatting rules.";
47+
"preserving all other auto-detected formatting rules.";
4748
}
4849

4950
@Override
@@ -53,11 +54,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5354
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
5455
if (tree instanceof JavaSourceFile) {
5556
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
56-
TabsAndIndentsStyle style = ((SourceFile) cu).getStyle(TabsAndIndentsStyle.class);
57-
if (style == null) {
58-
style = IntelliJ.tabsAndIndents();
59-
}
60-
style = style.withUseTabCharacter(useTabs);
57+
TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents)
58+
.withUseTabCharacter(useTabs);
6159
return new NormalizeTabsOrSpacesVisitor<>(style).visit(tree, ctx);
6260
}
6361
return (J) tree;

src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
import org.openrewrite.java.tree.TypeUtils;
3131
import org.openrewrite.marker.Markers;
3232
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;
33+
import org.openrewrite.style.Style;
3334

3435
import java.security.MessageDigest;
3536
import java.security.NoSuchAlgorithmException;
3637
import java.time.Duration;
3738
import java.util.ArrayList;
3839
import java.util.Base64;
3940
import java.util.List;
40-
import java.util.Optional;
4141

4242
import static java.util.Objects.requireNonNull;
4343
import static java.util.stream.Collectors.toList;
@@ -145,8 +145,7 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s
145145
}
146146
}
147147

148-
TabsAndIndentsStyle tabsAndIndentsStyle = Optional.ofNullable(getCursor().firstEnclosingOrThrow(SourceFile.class)
149-
.getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents());
148+
TabsAndIndentsStyle tabsAndIndentsStyle = Style.from(TabsAndIndentsStyle.class, getCursor().firstEnclosingOrThrow(SourceFile.class), IntelliJ::tabsAndIndents);
150149
boolean useTab = tabsAndIndentsStyle.getUseTabCharacter();
151150
int tabSize = tabsAndIndentsStyle.getTabSize();
152151

0 commit comments

Comments
 (0)