-
Notifications
You must be signed in to change notification settings - Fork 465
5958: RemoveTrailingWhitespace from plain text files #6013
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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<?, ExecutionContext> getVisitor() { | ||
return new RemoveTrailingWhitespaceVisitor<>(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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<P> extends PlainTextVisitor<P> { | ||
@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); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,132 @@ | ||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||
* Copyright 2025 the original author or authors. | ||||||||||||||||||||||||||
* <p> | ||||||||||||||||||||||||||
* 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 | ||||||||||||||||||||||||||
* <p> | ||||||||||||||||||||||||||
* https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||
* <p> | ||||||||||||||||||||||||||
* 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( | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not fully sure, if this will work, but can you try adding a test which has a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already a RemoveTrailingWhitespace for Java (and a few others). |
||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit,\s\s | ||||||||||||||||||||||||||
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||||||||||||||||||||||||||
""", | ||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||
Comment on lines
+40
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you're seeing from the changes here we already have a recipe specifically for text blocks; no need to cover those in your recipe implementation. |
||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||
""", | ||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||
""", | ||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||
Comment on lines
+76
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||
Comment on lines
+94
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
""", | ||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
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, | ||||||||||||||||||||||||||
Comment on lines
+119
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||||||||||||||||||||||||||
""", | ||||||||||||||||||||||||||
SourceSpec::noTrim | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's usually no need to have a separate named & public visitor class; typically you can inline these into the recipe itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This follows the established pattern for other RemoveTrailingWhitespace implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we have some outdated patterns lingering; going forward let's start with a minimal public API.