Skip to content

Commit 9b75805

Browse files
committed
Fix Incorrect Spacing for Left Braces in Record Declarations
This commit fixes formatter recognizing wrong left braces when record contains left braces in record's param annotations. Fixes : #4288
1 parent 940c710 commit 9b75805

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16536,4 +16536,68 @@ public int sample(String param1) {
1653616536
""";
1653716537
formatSource(input, expected);
1653816538
}
16539+
16540+
public void testRecordWithOpeningParanthesisInParam() throws JavaModelException {
16541+
setComplianceLevel(CompilerOptions.VERSION_16);
16542+
this.formatterPrefs.insert_space_before_opening_brace_in_record_declaration = true;
16543+
String input = """
16544+
public record X(String a, @SuppressWarnings({
16545+
"a", "b" }) String b){
16546+
16547+
}
16548+
""";
16549+
String expected = """
16550+
public record X(String a, @SuppressWarnings({
16551+
"a", "b" }) String b) {
16552+
16553+
}
16554+
""";
16555+
formatSource(input, expected);
16556+
}
16557+
public void testRecordWithOpeningParanthesisInParamWithSuperInterfaces() throws JavaModelException {
16558+
setComplianceLevel(CompilerOptions.VERSION_16);
16559+
this.formatterPrefs.insert_space_before_opening_brace_in_record_declaration = true;
16560+
String input = """
16561+
public record X2(String a,
16562+
@SuppressWarnings({
16563+
"a", "b" }) String b)
16564+
implements Runnable, AutoCloseable{
16565+
16566+
@Override
16567+
public void run() {
16568+
}
16569+
16570+
@Override
16571+
public void close() {
16572+
}
16573+
}
16574+
""";
16575+
String expected = """
16576+
public record X2(String a, @SuppressWarnings({
16577+
"a", "b" }) String b) implements Runnable, AutoCloseable {
16578+
16579+
@Override
16580+
public void run() {
16581+
}
16582+
16583+
@Override
16584+
public void close() {
16585+
}
16586+
}
16587+
""";
16588+
formatSource(input, expected);
16589+
}
16590+
16591+
public void testRecordWithTypeParams() throws JavaModelException {
16592+
setComplianceLevel(CompilerOptions.VERSION_16);
16593+
this.formatterPrefs.insert_space_before_opening_brace_in_record_declaration = true;
16594+
String input = """
16595+
record X<T>(T value){}
16596+
""";
16597+
String expected = """
16598+
record X<T>(T value) {
16599+
}
16600+
""";
16601+
formatSource(input, expected);
16602+
}
1653916603
}

org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,26 @@ public boolean visit(RecordDeclaration node) {
178178

179179
List<TypeParameter> typeParameters = node.typeParameters();
180180
handleTypeParameters(typeParameters);
181-
182-
handleToken(node.getName(), TokenNameLBRACE,
183-
this.options.insert_space_before_opening_brace_in_record_declaration, false);
184181
List<Type> superInterfaces = node.superInterfaceTypes();
185182
if (!superInterfaces.isEmpty()) {
186183
handleTokenBefore(superInterfaces.get(0), TokenNameimplements, true, true);
187184
handleCommas(superInterfaces, this.options.insert_space_before_comma_in_superinterfaces,
188185
this.options.insert_space_after_comma_in_superinterfaces);
189186
}
190187

191-
handleRecordComponents(node.recordComponents(), node.getName(), typeParameters);
188+
List<? extends ASTNode> components = node.recordComponents();
189+
handleRecordComponents(components, node.getName(), typeParameters);
190+
191+
ASTNode lastBeforeBrace = node.getName();
192+
if (!superInterfaces.isEmpty()) {
193+
lastBeforeBrace = superInterfaces.get(superInterfaces.size() - 1);
194+
} else if (!components.isEmpty()) {
195+
lastBeforeBrace = components.get(components.size() - 1);
196+
} else if (!typeParameters.isEmpty()) {
197+
lastBeforeBrace = typeParameters.get(typeParameters.size() - 1);
198+
}
199+
handleTokenAfter(lastBeforeBrace, TokenNameLBRACE,
200+
this.options.insert_space_before_opening_brace_in_record_declaration, false);
192201
return true;
193202
}
194203

0 commit comments

Comments
 (0)