Skip to content

Commit

Permalink
Change "insert space" setting for type parameters based on Java version
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Feb 22, 2023
1 parent 34cf2ab commit cc32470
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,27 @@ protected static Item[] items(String expectedOverride) {
File configDir = new File("src/test/resources/config");
File expectedOverrideDir = new File("src/test/resources/" + expectedOverride);
for (File source : sourceDir.listFiles((dir, name) -> !name.startsWith("."))) {
File expected = new File(expectedOverrideDir, source.getName());
if (!expected.exists()) {
expected = new File(expectedDir, source.getName());
}
File config = new File(configDir, source.getName());
for (JavaBaseline javaBaseline : JavaBaseline.values()) {
File expected = getExpected(source, javaBaseline, expectedOverrideDir, expectedDir);
addItem(items, javaBaseline, source, expected, config);
}
}
return items.toArray(new Item[0]);
}

private static File getExpected(File source, JavaBaseline javaBaseline, File... expectedDirs) {
for (File expectedDir : expectedDirs) {
File versionSpecificExpectedDir = new File(expectedDir, javaBaseline.toString().toLowerCase());
File expected = new File(versionSpecificExpectedDir, source.getName());
expected = (!expected.exists()) ? new File(expectedDir, source.getName()) : expected;
if (expected.exists()) {
return expected;
}
}
throw new IllegalStateException("Unable to find expected file");
}

private static void addItem(List<Item> items, JavaBaseline javaBaseline, File source, File expected, File config) {
if (source.getName().contains("lineendings")) {
items.add(new Item(javaBaseline, copy(source, LineEnding.CR), copy(expected, LineEnding.CR), config));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package simple;

/**
* gh-363.
*/
class SpectatorToDoubleGauge<T> extends AbstractMeter<T> implements Gauge {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package simple;

/**
* Record with generic.
*
* @author Andy Wilkinson
* @since 1.0.0
*/
public record SomeRecord<T> (T item) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package simple;

/**
* gh-363.
*/
class SpectatorToDoubleGauge<T> extends AbstractMeter<T> implements Gauge {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import java.util.Properties;

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaBaseline;
import io.spring.javaformat.config.JavaFormatConfig;

/**
Expand Down Expand Up @@ -63,8 +64,12 @@ private Map<String, String> loadProperties() throws IOException {
}

private void applyConfig(Map<String, String> properties, JavaFormatConfig javaFormatConfig) {
String coreFormatter = this.prefix + ".core.formatter.";
if (javaFormatConfig.getIndentationStyle() == IndentationStyle.SPACES) {
properties.put(this.prefix + ".core.formatter.tabulation.char", "space");
properties.put(coreFormatter + "tabulation.char", "space");
}
if (javaFormatConfig.getJavaBaseline() == JavaBaseline.V8) {
properties.put(coreFormatter + "insert_space_after_closing_angle_bracket_in_type_parameters", "insert");
}
}

Expand Down

0 comments on commit cc32470

Please sign in to comment.