Skip to content

Commit cc32470

Browse files
committed
Change "insert space" setting for type parameters based on Java version
Change `insert_space_after_closing_angle_bracket_in_type_parameters` setting when Java 8 is used so that a space is inserted after the parameter type. Later versions of the Eclipse formatter can use `do not insert` and they will correctly format classes. Earlier versions need to use `insert` otherwise the space disappears. This commit effectively reverts commit b11499d for Java 8 only. This shouldn't be a problem since records were introduced in Java 14. Fixes gh-363
1 parent 34cf2ab commit cc32470

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

spring-javaformat/spring-javaformat-formatter-tests/src/test/java/io/spring/javaformat/formatter/AbstractFormatterTests.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,27 @@ protected static Item[] items(String expectedOverride) {
5454
File configDir = new File("src/test/resources/config");
5555
File expectedOverrideDir = new File("src/test/resources/" + expectedOverride);
5656
for (File source : sourceDir.listFiles((dir, name) -> !name.startsWith("."))) {
57-
File expected = new File(expectedOverrideDir, source.getName());
58-
if (!expected.exists()) {
59-
expected = new File(expectedDir, source.getName());
60-
}
6157
File config = new File(configDir, source.getName());
6258
for (JavaBaseline javaBaseline : JavaBaseline.values()) {
59+
File expected = getExpected(source, javaBaseline, expectedOverrideDir, expectedDir);
6360
addItem(items, javaBaseline, source, expected, config);
6461
}
6562
}
6663
return items.toArray(new Item[0]);
6764
}
6865

66+
private static File getExpected(File source, JavaBaseline javaBaseline, File... expectedDirs) {
67+
for (File expectedDir : expectedDirs) {
68+
File versionSpecificExpectedDir = new File(expectedDir, javaBaseline.toString().toLowerCase());
69+
File expected = new File(versionSpecificExpectedDir, source.getName());
70+
expected = (!expected.exists()) ? new File(expectedDir, source.getName()) : expected;
71+
if (expected.exists()) {
72+
return expected;
73+
}
74+
}
75+
throw new IllegalStateException("Unable to find expected file");
76+
}
77+
6978
private static void addItem(List<Item> items, JavaBaseline javaBaseline, File source, File expected, File config) {
7079
if (source.getName().contains("lineendings")) {
7180
items.add(new Item(javaBaseline, copy(source, LineEnding.CR), copy(expected, LineEnding.CR), config));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package simple;
2+
3+
/**
4+
* gh-363.
5+
*/
6+
class SpectatorToDoubleGauge<T> extends AbstractMeter<T> implements Gauge {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package simple;
2+
3+
/**
4+
* Record with generic.
5+
*
6+
* @author Andy Wilkinson
7+
* @since 1.0.0
8+
*/
9+
public record SomeRecord<T> (T item) {
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package simple;
2+
3+
/**
4+
* gh-363.
5+
*/
6+
class SpectatorToDoubleGauge<T> extends AbstractMeter<T> implements Gauge {
7+
8+
}

spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/eclipse/Options.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import java.util.Properties;
2525

2626
import io.spring.javaformat.config.IndentationStyle;
27+
import io.spring.javaformat.config.JavaBaseline;
2728
import io.spring.javaformat.config.JavaFormatConfig;
2829

2930
/**
@@ -63,8 +64,12 @@ private Map<String, String> loadProperties() throws IOException {
6364
}
6465

6566
private void applyConfig(Map<String, String> properties, JavaFormatConfig javaFormatConfig) {
67+
String coreFormatter = this.prefix + ".core.formatter.";
6668
if (javaFormatConfig.getIndentationStyle() == IndentationStyle.SPACES) {
67-
properties.put(this.prefix + ".core.formatter.tabulation.char", "space");
69+
properties.put(coreFormatter + "tabulation.char", "space");
70+
}
71+
if (javaFormatConfig.getJavaBaseline() == JavaBaseline.V8) {
72+
properties.put(coreFormatter + "insert_space_after_closing_angle_bracket_in_type_parameters", "insert");
6873
}
6974
}
7075

0 commit comments

Comments
 (0)