From 4d06a57456aaa3dc1b21a234ca33d498dd04fa97 Mon Sep 17 00:00:00 2001 From: David Grieve Date: Mon, 8 Sep 2025 10:36:31 -0400 Subject: [PATCH] skeleton for RemoveTrailingWhitespace from plain text files --- .../text/RemoveTrailingWhitespace.java | 37 +++++ .../text/RemoveTrailingWhitespaceVisitor.java | 41 ++++++ .../text/RemoveTrailingWhitespaceTest.java | 132 ++++++++++++++++++ 3 files changed, 210 insertions(+) create mode 100644 rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespace.java create mode 100644 rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespaceVisitor.java create mode 100644 rewrite-core/src/test/java/org/openrewrite/text/RemoveTrailingWhitespaceTest.java diff --git a/rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespace.java b/rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespace.java new file mode 100644 index 0000000000..4266606ee9 --- /dev/null +++ b/rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespace.java @@ -0,0 +1,37 @@ +/* + * Copyright 2025 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.text; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; + +public class RemoveTrailingWhitespace extends Recipe { + @Override + public String getDisplayName() { + return "Remove trailing whitespace"; + } + + @Override + public String getDescription() { + return "Remove any extra trailing whitespace from the end of each line."; + } + + @Override + public TreeVisitor getVisitor() { + return new RemoveTrailingWhitespaceVisitor<>(); + } +} diff --git a/rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespaceVisitor.java b/rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespaceVisitor.java new file mode 100644 index 0000000000..3723c94e89 --- /dev/null +++ b/rewrite-core/src/main/java/org/openrewrite/text/RemoveTrailingWhitespaceVisitor.java @@ -0,0 +1,41 @@ +/* + * Copyright 2025 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.text; + +import com.fasterxml.jackson.annotation.JsonCreator; +import org.jspecify.annotations.Nullable; +import org.openrewrite.Cursor; +import org.openrewrite.Tree; + +public class RemoveTrailingWhitespaceVisitor

extends PlainTextVisitor

{ + @Nullable + private final Tree stopAfter; + + @JsonCreator + public RemoveTrailingWhitespaceVisitor(@Nullable Tree stopAfter) { + this.stopAfter = stopAfter; + } + + public RemoveTrailingWhitespaceVisitor() { + this(null); + } + + @Override + public @Nullable Tree visit(@Nullable Tree tree, P p, Cursor parent) { + return super.visit(tree, p, parent); + } + +} diff --git a/rewrite-core/src/test/java/org/openrewrite/text/RemoveTrailingWhitespaceTest.java b/rewrite-core/src/test/java/org/openrewrite/text/RemoveTrailingWhitespaceTest.java new file mode 100644 index 0000000000..c72be29cbc --- /dev/null +++ b/rewrite-core/src/test/java/org/openrewrite/text/RemoveTrailingWhitespaceTest.java @@ -0,0 +1,132 @@ +/* + * Copyright 2025 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.text; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.SourceSpec; + +import static org.openrewrite.test.SourceSpecs.text; + +class RemoveTrailingWhitespaceTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new RemoveTrailingWhitespace()); + } + + @DocumentExample + @SuppressWarnings("TrailingWhitespacesInTextBlock") + @Test + void removeTrailingFirst() { + rewriteRun( + text( + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit,\s\s + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + """, + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + """, + SourceSpec::noTrim + ) + ); + } + + @SuppressWarnings("TrailingWhitespacesInTextBlock") + @Test + void removeTrailingLast() { + rewriteRun( + text( + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\s\s + """, + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + """, + SourceSpec::noTrim + ) + ); + } + + @SuppressWarnings("TrailingWhitespacesInTextBlock") + @Test + void removeTrailingAll() { + rewriteRun( + text( + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit,\s\s + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\s\s + """, + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + """, + SourceSpec::noTrim + ) + ); + } + + @SuppressWarnings("TrailingWhitespacesInTextBlock") + @Test + void removeTrailingBetween() { + rewriteRun( + text( + """ + \s\s + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + \s\s + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + \s\s + """, + """ + + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + + """, + SourceSpec::noTrim + ) + ); + } + + + @SuppressWarnings("TrailingWhitespacesInTextBlock") + @Test + void removeTrailingConsecutive() { + rewriteRun( + text( + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit,\s\s + \s\s + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + """, + """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + """, + SourceSpec::noTrim + ) + ); + } +}