From 755678a23c894bb207cc1050006df309eeeeddc2 Mon Sep 17 00:00:00 2001 From: Michael Keppler Date: Sat, 30 Sep 2023 19:01:06 +0200 Subject: [PATCH] Avoid trailing blanks in SingleLineComment Fixes #3588. --- .../java/format/SingleLineCommentsTest.java | 13 +++++++++++++ .../openrewrite/java/format/SingleLineComments.java | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/format/SingleLineCommentsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/format/SingleLineCommentsTest.java index 0d2e1209d78..06178656c13 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/format/SingleLineCommentsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/format/SingleLineCommentsTest.java @@ -47,4 +47,17 @@ class Test { ) ); } + + @Test + void emptyCommentLineDoesNotGetTrailingBlank() { + rewriteRun( + java( + """ + // Copyright + // + // Some long license text + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/SingleLineComments.java b/rewrite-java/src/main/java/org/openrewrite/java/format/SingleLineComments.java index 0acf6205ecf..5ba900abc17 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/SingleLineComments.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/SingleLineComments.java @@ -42,8 +42,9 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) { return space.withComments(ListUtils.map(space.getComments(), c -> { if (!c.isMultiline()) { TextComment tc = (TextComment) c; - if (!tc.getText().startsWith(" ")) { - return tc.withText(" " + tc.getText()); + String commentText = tc.getText(); + if (!commentText.isEmpty() && !commentText.startsWith(" ")) { + return tc.withText(" " + commentText); } } return c;