Skip to content
Closed
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
@@ -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> {
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

@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(
Copy link
Contributor

Choose a reason for hiding this comment

The 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 java() program?

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,\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.

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.\s\s
""",
"""
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.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,\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.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
\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
\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,
Comment on lines +119 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
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.


sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
""",
SourceSpec::noTrim
)
);
}
}
Loading