Skip to content

Commit 3263e03

Browse files
authored
Fix indentation calculation (#916)
1 parent 83af53b commit 3263e03

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,21 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, Execution
372372
classDeclaration = new RemoveAnnotationVisitor(LOMBOK_VALUE_MATCHER).visitClassDeclaration(classDeclaration, ctx);
373373
maybeRemoveImport("lombok.Value");
374374

375+
List<J.Modifier> mappedModifiers = ListUtils.map(classDeclaration.getModifiers(), modifier -> {
376+
J.Modifier.Type type = modifier.getType();
377+
if (type == J.Modifier.Type.Static || type == J.Modifier.Type.Final) {
378+
return null;
379+
}
380+
return modifier;
381+
});
382+
J.ClassDeclaration.Kind kind = classDeclaration.withKind(J.ClassDeclaration.Kind.Type.Record).getPadding().getKind();
383+
if (mappedModifiers.isEmpty()) {
384+
kind = kind.withPrefix(kind.getPrefix().withWhitespace(""));
385+
}
386+
375387
classDeclaration = classDeclaration
376-
.withKind(J.ClassDeclaration.Kind.Type.Record)
377-
.withModifiers(ListUtils.map(classDeclaration.getModifiers(), modifier -> {
378-
J.Modifier.Type type = modifier.getType();
379-
if (type == J.Modifier.Type.Static || type == J.Modifier.Type.Final) {
380-
return null;
381-
}
382-
return modifier;
383-
}))
388+
.getPadding().withKind(kind)
389+
.withModifiers(mappedModifiers)
384390
.withType(buildRecordType(classDeclaration))
385391
.withBody(classDeclaration.getBody()
386392
.withStatements(bodyStatements)

src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class A {
6262
package example;
6363
6464
public record A(
65-
String test) {
65+
String test) {
6666
}
6767
"""
6868
),
@@ -121,9 +121,9 @@ public class Test {
121121
""",
122122
"""
123123
public record Test(
124-
String field1,
124+
String field1,
125125
126-
String field2) {
126+
String field2) {
127127
@Override
128128
public String toString() {
129129
return "Test(" +
@@ -166,7 +166,7 @@ public class B {
166166
import lombok.Value;
167167
168168
public record A(
169-
String test) {
169+
String test) {
170170
}
171171
172172
@Value
@@ -201,14 +201,12 @@ static class B {
201201
202202
public class A {
203203
record B(
204-
String test) {
204+
String test) {
205205
}
206206
}
207207
"""
208208
)
209209
);
210-
211-
212210
}
213211

214212
@Test
@@ -233,7 +231,7 @@ public class A implements Serializable {
233231
import java.io.Serializable;
234232
235233
public record A(
236-
String test) implements Serializable {
234+
String test) implements Serializable {
237235
}
238236
"""
239237
)
@@ -266,7 +264,7 @@ public class A implements Serializable {
266264
267265
@Builder
268266
public record A(
269-
String test) implements Serializable {
267+
String test) implements Serializable {
270268
}
271269
"""
272270
)
@@ -289,7 +287,7 @@ public class Foo {
289287
""",
290288
"""
291289
public record Foo(
292-
boolean bar) {
290+
boolean bar) {
293291
}"""
294292
),
295293
java(
@@ -329,9 +327,9 @@ public class Config {
329327
""",
330328
"""
331329
public record Config(
332-
boolean enabled,
333-
Boolean active,
334-
String name) {
330+
boolean enabled,
331+
Boolean active,
332+
String name) {
335333
}
336334
"""
337335
),
@@ -384,7 +382,7 @@ Supplier<String> usingMethodReference() {
384382
import java.util.function.Supplier;
385383
386384
record A(
387-
String test) {
385+
String test) {
388386
}
389387
390388
class Using {

0 commit comments

Comments
 (0)