Skip to content

Commit 68d0c3d

Browse files
authored
Added formatting support for single/multiline comments in parameters / record header (#6176)
1 parent 32f6b10 commit 68d0c3d

3 files changed

Lines changed: 134 additions & 3 deletions

File tree

rewrite-java/src/main/java/org/openrewrite/java/format/SpacesVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
242242

243243
if (c.getPrimaryConstructor() != null) {
244244
c = c.withPrimaryConstructor(ListUtils.map(c.getPrimaryConstructor(), (ix, param) -> {
245-
if (param.getPrefix().getLastWhitespace().contains("\n")) {
245+
if (param.getPrefix().getWhitespace().contains("\n") || (!param.getPrefix().getComments().isEmpty() && param.getPrefix().getComments().stream().noneMatch(comment -> comment.getSuffix().contains("\n")))) {
246246
return param;
247247
}
248248
if (ix == 0 && param.getComments().isEmpty()) {

rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ public Space visitSpace(Space space, Space.Location loc, P p) {
205205
IndentType indentType = getCursor().getParent().getNearestMessage("indentType", IndentType.ALIGN);
206206
if (right.getElement() instanceof J) {
207207
J elem = (J) right.getElement();
208-
if ((right.getAfter().getLastWhitespace().contains("\n") ||
209-
elem.getPrefix().getLastWhitespace().contains("\n"))) {
208+
if (right.getAfter().getLastWhitespace().contains("\n") ||
209+
elem.getPrefix().getLastWhitespace().contains("\n") ||
210+
(elem.getPrefix().getWhitespace().contains("\n") && elem.getPrefix().getComments().stream().noneMatch(c -> c.getSuffix().contains("\n")))) {
210211
switch (loc) {
211212
case FOR_CONDITION:
212213
case FOR_UPDATE: {

rewrite-java/src/test/java/org/openrewrite/java/format/AutoFormatTest.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,64 @@ record someRecord5(@Foo @Foo String name, int age
21342134
) {
21352135
}
21362136
"""
2137+
),
2138+
java(
2139+
"""
2140+
record someRecord6(
2141+
String name,
2142+
/* some comment */ int age) {
2143+
}
2144+
""",
2145+
"""
2146+
record someRecord6(
2147+
String name,
2148+
/* some comment */ int age) {
2149+
}
2150+
"""
2151+
),
2152+
java(
2153+
"""
2154+
record someRecord7(
2155+
String name,
2156+
// some comment
2157+
int age) {
2158+
}
2159+
""",
2160+
"""
2161+
record someRecord7(
2162+
String name,
2163+
// some comment
2164+
int age) {
2165+
}
2166+
"""
2167+
),
2168+
java(
2169+
"""
2170+
record someRecord8(
2171+
String name, // some comment
2172+
int age) {
2173+
}
2174+
""",
2175+
"""
2176+
record someRecord8(
2177+
String name, // some comment
2178+
int age) {
2179+
}
2180+
"""
2181+
),
2182+
java(
2183+
"""
2184+
record someRecord9(
2185+
String name, /* some comment */ int age
2186+
) {
2187+
}
2188+
""",
2189+
"""
2190+
record someRecord9(
2191+
String name, /* some comment */ int age
2192+
) {
2193+
}
2194+
"""
21372195
)
21382196
);
21392197
}
@@ -2302,6 +2360,78 @@ void someMethod5(String name, int age
23022360
}
23032361
}
23042362
"""
2363+
),
2364+
java(
2365+
"""
2366+
class Test6 {
2367+
void someMethod6(
2368+
String name,
2369+
/* some comment */ int age) {
2370+
}
2371+
}
2372+
""",
2373+
"""
2374+
class Test6 {
2375+
void someMethod6(
2376+
String name,
2377+
/* some comment */ int age) {
2378+
}
2379+
}
2380+
"""
2381+
),
2382+
java(
2383+
"""
2384+
class Test7 {
2385+
void someMethod7(
2386+
String name,
2387+
// some comment
2388+
int age) {
2389+
}
2390+
}
2391+
""",
2392+
"""
2393+
class Test7 {
2394+
void someMethod7(
2395+
String name,
2396+
// some comment
2397+
int age) {
2398+
}
2399+
}
2400+
"""
2401+
),
2402+
java(
2403+
"""
2404+
class Test8 {
2405+
void someMethod8(
2406+
String name, // some comment
2407+
int age) {
2408+
}
2409+
}
2410+
""",
2411+
"""
2412+
class Test8 {
2413+
void someMethod8(
2414+
String name, // some comment
2415+
int age) {
2416+
}
2417+
}
2418+
"""
2419+
),
2420+
java(
2421+
"""
2422+
class Test9 {
2423+
void someMethod9(
2424+
String name, /* some comment */ int age) {
2425+
}
2426+
}
2427+
""",
2428+
"""
2429+
class Test9 {
2430+
void someMethod9(
2431+
String name, /* some comment */ int age) {
2432+
}
2433+
}
2434+
"""
23052435
)
23062436
);
23072437
}

0 commit comments

Comments
 (0)