Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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> {
@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(
"""
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
)
);
}
}
Loading