chore: Update Eclipse formatter settings to version 25#4340
chore: Update Eclipse formatter settings to version 25#4340ggjulio wants to merge 1 commit intoredhat-developer:mainfrom
Conversation
|
Can you show a sample java file that exhibits issues with the current formatting? |
|
This should do. beforepublic class SwitchFormatterDemo {
public static String classicSwitch(int day) {
String name;
switch (day) {
case 1:
name = "Monday";
break;
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
case 4:
name = "Thursday";
break;
case 5:
name = "Friday";
break;
default:
name = "Weekend";
break;
}
return name;
}
public static String arrowSwitch(int day) {
return switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
default -> "Weekend";
};
}
public static int yieldSwitch(String season) {
int numMonths = switch (season) {
case "Spring", "Summer", "Autumn", "Winter" -> 3;
case "Q1" -> {
System.out.println("First quarter");
yield 3;
}
case "H1" -> {
System.out.println("First half");
yield 6;
}
default -> {
System.out.println("Unknown season: " + season);
yield 0;
}
};
return numMonths;
}
public static String multiLabelSwitch(int month) {
return switch (month) {
case 12, 1, 2 -> "Winter";
case 3, 4, 5 -> "Spring";
case 6, 7, 8 -> "Summer";
case 9, 10, 11 -> "Autumn";
default -> throw new IllegalArgumentException("Invalid month: " + month);
};
}
public static String nestedSwitch(String type, int value) {
return switch (type) {
case "day" -> switch (value) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Other day";
};
case "month" -> switch (value) {
case 1 -> "January";
case 2 -> "February";
default -> "Other month";
};
default -> "Unknown type";
};
}
public static void main(String[] args) {
System.out.println("=== Classic switch (Java ≤ 10, formatter OK) ===");
for (int i = 1; i <= 7; i++) {
System.out.println(" Day " + i + " → " + classicSwitch(i));
}
System.out.println("\n=== Arrow switch expression (Java 14+, formatter BROKEN) ===");
for (int i = 1; i <= 7; i++) {
System.out.println(" Day " + i + " → " + arrowSwitch(i));
}
System.out.println("\n=== Yield switch expression (Java 14+, formatter BROKEN) ===");
String[] seasons = {"Spring", "Summer", "Q1", "H1", "Unknown"};
for (String s : seasons) {
System.out.println(" " + s + " → " + yieldSwitch(s) + " months");
}
System.out.println("\n=== Multi-label switch expression (Java 14+, formatter BROKEN) ===");
for (int m = 1; m <= 12; m++) {
System.out.println(" Month " + m + " → " + multiLabelSwitch(m));
}
System.out.println("\n=== Nested switch expressions (Java 14+, formatter BROKEN) ===");
System.out.println(" type=day, value=1 → " + nestedSwitch("day", 1));
System.out.println(" type=month, value=2 → " + nestedSwitch("month", 2));
}
}After formatting with .targetPlatform, .compliance and .source at 10public class SwitchFormatterDemo {
public static String classicSwitch(int day) {
String name;
switch (day) {
case 1:
name = "Monday";
break;
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
case 4:
name = "Thursday";
break;
case 5:
name = "Friday";
break;
default:
name = "Weekend";
break;
}
return name;
}
// -----------------------------------------------------------------------
// Switch EXPRESSION with arrow labels (Java 14+)
// Eclipse formatter @ Java 10 does NOT reformat this block.
// -----------------------------------------------------------------------
public static String arrowSwitch(int day) {
return switch(day){case 1->"Monday";case 2->"Tuesday";case 3->"Wednesday";case 4->"Thursday";case 5->"Friday";default->"Weekend";};
}
public static int yieldSwitch(String season) {
int numMonths = switch (season) {
case "Spring", "Summer", "Autumn", "Winter" -> 3;
case "Q1" -> {
System.out.println("First quarter");
yield 3;
}
case "H1" -> {
System.out.println("First half");
yield 6;
}
default -> {
System.out.println("Unknown season: " + season);
yield 0;
}
};
return numMonths;
}
// -----------------------------------------------------------------------
// Multi-label arrow switch (Java 14+)
// ⚠ Comma-separated labels in one case are also unknown to Java-10
// formatter — alignment inside the block will be broken.
// -----------------------------------------------------------------------
public static String multiLabelSwitch(int month) {
return switch(month){case 12,1,2->"Winter";case 3,4,5->"Spring";case 6,7,8->"Summer";case 9,10,11->"Autumn";default->throw new IllegalArgumentException("Invalid month: "+month);};
}
public static String nestedSwitch(String type, int value) {
return switch (type) {
case "day" -> switch (value) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Other day";
};
case "month" -> switch (value) {
case 1 -> "January";
case 2 -> "February";
default -> "Other month";
};
default -> "Unknown type";
};
}
public static void main(String[] args) {
System.out.println("=== Classic switch (Java ≤ 10, formatter OK) ===");
for (int i = 1; i <= 7; i++) {
System.out.println(" Day " + i + " → " + classicSwitch(i));
}
System.out.println("\n=== Arrow switch expression (Java 14+, formatter BROKEN) ===");
for (int i = 1; i <= 7; i++) {
System.out.println(" Day " + i + " → " + arrowSwitch(i));
}
System.out.println("\n=== Yield switch expression (Java 14+, formatter BROKEN) ===");
String[] seasons = { "Spring", "Summer", "Q1", "H1", "Unknown" };
for (String s : seasons) {
System.out.println(" " + s + " → " + yieldSwitch(s) + " months");
}
System.out.println("\n=== Multi-label switch expression (Java 14+, formatter BROKEN) ===");
for (int m = 1; m <= 12; m++) {
System.out.println(" Month " + m + " → " + multiLabelSwitch(m));
}
System.out.println("\n=== Nested switch expressions (Java 14+, formatter BROKEN) ===");
System.out.println(" type=day, value=1 → " + nestedSwitch("day", 1));
System.out.println(" type=month, value=2 → " + nestedSwitch("month", 2));
}
} |
|
So I tried really hard to reproduce with your test file, putting it an empty folder, or existing java 11, 21 or 25 maven projects, generating the formatter file (containing java 10 reference), but I can't reproduce the breakage in switch formatting. Except for the switch in indentation from space to tab, there's nothing remarkable happening here. We'll certainly update the 5yo formatter template with a version from the latest eclipse build, but I'd really like to understand how you're seeing an issue when I don't. |
|
@fbricon really weird, i'm using the spotless maven plugin for formatting. https://github.com/diffplug/spotless <plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<java>
<eclipse>
<file>${maven.multiModuleProjectDirectory}/eclipse-formatter.xml</file>
<p2Mirrors>
<p2Mirror>
<prefix>https://download.eclipse.org</prefix>
<url>https://...eclipse-p2-mirror/</url>
</p2Mirror>
</p2Mirrors>
</eclipse>
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin> |
l'll close the PR then |
|
I'm using vscode's format document command. |
Bump the default version to avoid formatting issues on features newer than java 10.
(example: switch statement formatted on a single line)
Since a higher version is a superset, formatting on lower java version will still work.
