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.

Comment on lines +37 to +42
"Line with trailing spaces \n" +
"Line without trailing spaces\n" +
"Another line with spaces \n",
"Line with trailing spaces\n" +
"Line without trailing spaces\n" +
"Another line with spaces\n"
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
"Line with trailing spaces \n" +
"Line without trailing spaces\n" +
"Another line with spaces \n",
"Line with trailing spaces\n" +
"Line without trailing spaces\n" +
"Another line with spaces\n"
"""
Line with trailing spaces \s
Line without trailing spaces
Another line with spaces \s
""",
"""
Line with trailing spaces
Line without trailing spaces
Another line with spaces
"""

Comment on lines +51 to +56
"Line with trailing tabs\t\t\n" +
"Line without trailing tabs\n" +
"Mixed tabs and spaces\t \n",
"Line with trailing tabs\n" +
"Line without trailing tabs\n" +
"Mixed tabs and spaces\n"
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
"Line with trailing tabs\t\t\n" +
"Line without trailing tabs\n" +
"Mixed tabs and spaces\t \n",
"Line with trailing tabs\n" +
"Line without trailing tabs\n" +
"Mixed tabs and spaces\n"
"""
Line with trailing tabs
Line without trailing tabs
Mixed tabs and spaces \s
""",
"""
Line with trailing tabs
Line without trailing tabs
Mixed tabs and spaces
"""

Comment on lines +65 to +68
"First line\n" +
"Second line ",
"First line\n" +
"Second line"
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
"First line\n" +
"Second line ",
"First line\n" +
"Second line"
"""
First line
Second line """,
"""
First line
Second line"""

Comment on lines +77 to +86
"Line 1 \n" +
"\n" +
"Line 3\t\n" +
"\n" +
"Line 5",
"Line 1\n" +
"\n" +
"Line 3\n" +
"\n" +
"Line 5"
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
"Line 1 \n" +
"\n" +
"Line 3\t\n" +
"\n" +
"Line 5",
"Line 1\n" +
"\n" +
"Line 3\n" +
"\n" +
"Line 5"
"""
Line 1 \s
Line 3
Line 5""",
"""
Line 1
Line 3
Line 5"""

Comment on lines +95 to +100
"Spaces then tabs \t\t\n" +
"Tabs then spaces\t\t \n" +
"Mixed whitespace \t \t \n",
"Spaces then tabs\n" +
"Tabs then spaces\n" +
"Mixed whitespace\n"
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
"Spaces then tabs \t\t\n" +
"Tabs then spaces\t\t \n" +
"Mixed whitespace \t \t \n",
"Spaces then tabs\n" +
"Tabs then spaces\n" +
"Mixed whitespace\n"
"""
Spaces then tabs
Tabs then spaces \s
Mixed whitespace \s
""",
"""
Spaces then tabs
Tabs then spaces
Mixed whitespace
"""

Comment on lines +109 to +111
"Clean line 1\n" +
"Clean line 2\n" +
"Clean line 3"
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
"Clean line 1\n" +
"Clean line 2\n" +
"Clean line 3"
"""
Clean line 1
Clean line 2
Clean line 3"""

Comment on lines +127 to +132
"line with content\n" +
" \n" +
"another line",
"line with content\n" +
"\n" +
"another line"
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
"line with content\n" +
" \n" +
"another line",
"line with content\n" +
"\n" +
"another line"
"""
line with content
\s
another line""",
"""
line with content
another line"""

Comment on lines +141 to +146
" Indented line \n" +
"\tTab indented line\t\n" +
" Mixed indentation\t ",
" Indented line\n" +
"\tTab indented line\n" +
" Mixed indentation"
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
" Indented line \n" +
"\tTab indented line\t\n" +
" Mixed indentation\t ",
" Indented line\n" +
"\tTab indented line\n" +
" Mixed indentation"
"""
Indented line \s
Tab indented line
Mixed indentation """,
"""
Indented line
Tab indented line
Mixed indentation"""

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
3 participants