Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PlaintextOnly Option to FindAndReplace Recipe #4277

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.binary.Binary;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.AlreadyReplaced;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.quark.Quark;
import org.openrewrite.remote.Remote;

Expand Down Expand Up @@ -99,6 +101,11 @@ public String getDescription() {
@Nullable
String filePattern;

@Option(displayName = "Plaintext only", description = "Only alter files that are parsed as plaintext to prevent language-specific LST information loss. Defaults to false.",
required = false)
@Nullable
Boolean plaintextOnly;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
TreeVisitor<?, ExecutionContext> visitor = new TreeVisitor<Tree, ExecutionContext>() {
Expand Down Expand Up @@ -156,6 +163,15 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {

visitor = Preconditions.check(check, visitor);
}

if (Boolean.TRUE.equals(plaintextOnly)) {
visitor = Preconditions.check(new PlainTextVisitor<ExecutionContext>(){
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
@Override
public @NonNull PlainText visitText(@NonNull PlainText text, @NonNull ExecutionContext ctx) {
return SearchResult.found(text);
}
}, visitor);
}
return visitor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public String getDescription() {
public List<Recipe> getRecipeList() {
return Arrays.asList(
new DeleteSourceFiles("test.txt"),
new FindAndReplace("test", "", null, null, null, null, null));
new FindAndReplace("test", "", null, null, null, null, null, null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RecipeRunTest implements RewriteTest {
@Test
void printDatatable() {
rewriteRun(
recipeSpec -> recipeSpec.recipe(new FindAndReplace("replace_me", "replacement", null, null, null, null, null))
recipeSpec -> recipeSpec.recipe(new FindAndReplace("replace_me", "replacement", null, null, null, null, null, null))
.afterRecipe(recipeRun -> {
StringBuilder output = new StringBuilder();
final String dataTableName = SourcesFileResults.class.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FindAndReplaceTest implements RewriteTest {
@Test
void nonTxtExtension() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace(".", "G", null, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace(".", "G", null, null, null, null, null, null)),
text(
"""
This is text.
Expand All @@ -48,7 +48,7 @@ void nonTxtExtension() {
@Test
void removeWhenNullOrEmpty() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace("Bar", null, null, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace("Bar", null, null, null, null, null, null, null)),
text(
"""
Foo
Expand All @@ -67,7 +67,7 @@ void removeWhenNullOrEmpty() {
@Test
void defaultNonRegex() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace(".", "G", null, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace(".", "G", null, null, null, null, null, null)),
text(
"""
This is text.
Expand All @@ -82,7 +82,7 @@ void defaultNonRegex() {
@Test
void regexReplace() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace(".", "G", true, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace(".", "G", true, null, null, null, null, null)),
text(
"""
This is text.
Expand All @@ -97,7 +97,7 @@ void regexReplace() {
@Test
void captureGroups() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace("This is ([^.]+).", "I like $1.", true, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace("This is ([^.]+).", "I like $1.", true, null, null, null, null, null)),
text(
"""
This is text.
Expand All @@ -112,7 +112,7 @@ void captureGroups() {
@Test
void noRecursive() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace("test", "tested", false, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace("test", "tested", false, null, null, null, null, null)),
text("test", "tested")
);
}
Expand All @@ -122,7 +122,7 @@ void dollarSignsTolerated() {
String find = "This is text ${dynamic}.";
String replace = "This is text ${dynamic}. Stuff";
rewriteRun(
spec -> spec.recipe(new FindAndReplace(find, replace, null, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace(find, replace, null, null, null, null, null, null)),
text(find, replace)
);
}
Expand All @@ -144,9 +144,9 @@ public String getDescription() {
@Override
public List<Recipe> getRecipeList() {
return Arrays.asList(
new FindAndReplace("one", "two", null, null, null, null, null),
new FindAndReplace("two", "three", null, null, null, null, null),
new FindAndReplace("three", "four", null, null, null, null, null));
new FindAndReplace("one", "two", null, null, null, null, null, null),
new FindAndReplace("two", "three", null, null, null, null, null, null),
new FindAndReplace("three", "four", null, null, null, null, null, null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@ class FindAndReplaceJavaTest implements RewriteTest {
@Test
void findAndReplaceJava() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace("Test", "Replaced", null, null, null, null, null)),
spec -> spec.recipe(new FindAndReplace("Test", "Replaced", null, null, null, null, null, null)),
java(
"class Test {}",
"class Replaced {}"
)
);
}

@Test
void ignoresJavaFileWithPlaintextFlagEnabled() {
rewriteRun(
spec -> spec.recipe(new FindAndReplace("Test", "Replaced", null, null, null, null, null, true)),
java("class Test {}")
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/3532")
void filePatternShouldLimitApplication() {
Expand Down