Skip to content

Commit 77719d2

Browse files
avimtatimtebeek
andauthored
Add space after // in single-line comments (#854)
* Add recipe to enforce space after // in single-line comments * fix review comments * Use ListUtils.map as suggested by reviewer * fix review comments: use @Getter fields, skip block comments, update copyright year * Invert instanceof check and combine with isMultiline guard * Skip noinspection and region/endregion IDE comments --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent e51f2b4 commit 77719d2

3 files changed

Lines changed: 332 additions & 100 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openrewrite.staticanalysis;
18+
19+
import lombok.Getter;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.internal.ListUtils;
24+
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.tree.Space;
26+
import org.openrewrite.java.tree.TextComment;
27+
28+
import java.time.Duration;
29+
import java.util.Set;
30+
31+
import static java.util.Collections.singleton;
32+
33+
@Getter
34+
public class SingleLineCommentSpacing extends Recipe {
35+
36+
final String displayName = "Add space after // in single-line comments";
37+
38+
final String description = "Ensures there is exactly one space after // in single-line comments when missing.";
39+
40+
final Set<String> tags = singleton("formatting");
41+
42+
final Duration estimatedEffortPerOccurrence = Duration.ofMinutes(1);
43+
44+
@Override
45+
public TreeVisitor<?, ExecutionContext> getVisitor() {
46+
return new JavaIsoVisitor<ExecutionContext>() {
47+
48+
@Override
49+
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
50+
Space s = super.visitSpace(space, loc, ctx);
51+
52+
return s.withComments(ListUtils.map(s.getComments(), comment -> {
53+
if (!(comment instanceof TextComment) || ((TextComment) comment).isMultiline()) {
54+
return comment;
55+
}
56+
TextComment tc = (TextComment) comment;
57+
String text = tc.getText();
58+
if (text.isEmpty() || text.startsWith(" ") ||
59+
text.startsWith("language=") ||
60+
text.startsWith("noinspection") ||
61+
text.startsWith("region") ||
62+
text.startsWith("endregion")) {
63+
return comment;
64+
}
65+
return tc.withText(" " + text);
66+
}));
67+
}
68+
};
69+
}
70+
}

0 commit comments

Comments
 (0)