Skip to content

Conversation

@dsgrieve
Copy link

@dsgrieve dsgrieve commented Sep 8, 2025

What's changed?

Recipe to remove trailing whitespace

What's your motivation?

Fixes #732

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Patterned after other RemoveTrailingWhitespace recipes in openrewrite/rewrite.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@timtebeek timtebeek self-requested a review September 8, 2025 19:29
@dsgrieve dsgrieve changed the title 5958: Remove trailing whitespace 732: Remove trailing whitespace Sep 9, 2025
…tespace.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Comment on lines 186 to 192
input.append("Line ").append(i);
expected.append("Line ").append(i);

// Add i spaces as trailing whitespace
for (int j = 0; j < i; j++) {
input.append(" ");
}
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
input.append("Line ").append(i);
expected.append("Line ").append(i);
// Add i spaces as trailing whitespace
for (int j = 0; j < i; j++) {
input.append(" ");
}
input.append("Line ").append(i).repeat(" ", i);
expected.append("Line ").append(i);

Java 21 has this new repeat method on StringBuilder

Copy link
Author

Choose a reason for hiding this comment

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

Added in Java 11, IIRC.

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant: the tests run in Java 21, so this could be used here, no?


@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new PlainTextVisitor<ExecutionContext>() {
Copy link
Contributor

Choose a reason for hiding this comment

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

A PlainTextVisitor does not act on Java files. If you would want to "downcast" the Java lst as a text lst, you could build a generic TreeVisitor that does that conversion.

In this case though you do not want to do that as you will loose all rich information of the lst and every recipe that should run on java files after this one will not make any changes as the Lst's at that moment are no longer JavaSourceFiles but PlainText Sourcefiles causing the isAcceptable of the JavaVisitor to skip the converted files.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Recipe to remove trailing whitespace

4 participants