Skip to content

Commit

Permalink
Add PlaintextOnly Option to FindAndReplace Recipe (#4277)
Browse files Browse the repository at this point in the history
* Added a PlaintextOnly property to FindAndReplace recipe to prevent the recipe from executing on non-plaintext files and causing Language specific LST information loss.

* Removed constructors
  • Loading branch information
ryan-hudson authored Jun 24, 2024
1 parent b40ffa5 commit 08a386c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
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>(){
@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

0 comments on commit 08a386c

Please sign in to comment.