Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Fixed
- `forbidWildcardImports` and `forbidModuleImports` now detect imports that have leading whitespace (indentation/tabs). ([#2939](https://github.com/diffplug/spotless/pull/2939))
- `versionCatalog` step no longer splits long inline tables across multiple lines — Gradle's TOML 1.0 parser cannot read multi-line inline tables. The `maxLineLength` option has been removed. ([#2948](https://github.com/diffplug/spotless/issues/2948))
### Changes
- `Formatter` no longer recomputes line-ending normalization (`LineEnding.toUnix`) a second time for every formatter step that changes content, removing redundant O(n) work from the core formatting loop. ([#2934](https://github.com/diffplug/spotless/pull/2934))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public final class VersionCatalogStep {
private VersionCatalogStep() {}

private static final String NAME = "versionCatalog";
private static final int DEFAULT_MAX_LINE_LENGTH = 120;

private static final List<String> TABLE_ORDER = Arrays.asList(
"[versions]",
Expand All @@ -45,20 +44,16 @@ private VersionCatalogStep() {}
private static final Pattern ENTRY_LINE = Pattern.compile("^([^=]+)=(.+)$");

public static FormatterStep create() {
return create(false, DEFAULT_MAX_LINE_LENGTH);
return create(false);
}

public static FormatterStep create(boolean stripQuotedKeys) {
return create(stripQuotedKeys, DEFAULT_MAX_LINE_LENGTH);
}

public static FormatterStep create(boolean stripQuotedKeys, int maxLineLength) {
return FormatterStep.createLazy(NAME,
() -> new State(stripQuotedKeys, maxLineLength),
() -> new State(stripQuotedKeys),
State::toFormatter);
}

static String format(String raw, boolean stripQuotedKeys, int maxLineLength) {
static String format(String raw, boolean stripQuotedKeys) {
if (raw.trim().isEmpty()) {
return raw;
}
Expand Down Expand Up @@ -95,8 +90,7 @@ static String format(String raw, boolean stripQuotedKeys, int maxLineLength) {
result.append(header).append('\n');

for (Entry entry : entries) {
String formatted = formatEntry(entry.content, stripQuotedKeys);
entry.formatted = applyLineLength(formatted, maxLineLength);
entry.formatted = formatEntry(entry.content, stripQuotedKeys);
}
Collections.sort(entries, Comparator.comparing(Entry::sortKey));

Expand Down Expand Up @@ -191,8 +185,7 @@ private static boolean isBalanced(String text) {
}

private static String extractKey(String formattedEntry) {
String firstLine = formattedEntry.contains("\n") ? formattedEntry.substring(0, formattedEntry.indexOf('\n')) : formattedEntry;
Matcher matcher = ENTRY_LINE.matcher(firstLine);
Matcher matcher = ENTRY_LINE.matcher(formattedEntry);
if (!matcher.matches()) {
return formattedEntry;
}
Expand Down Expand Up @@ -231,52 +224,6 @@ static String formatEntry(String entry, boolean stripQuotedKeys) {
return key + " = " + value;
}

private static String applyLineLength(String formattedEntry, int maxLineLength) {
if (maxLineLength <= 0) {
return formattedEntry;
}

String inlineComment = extractInlineComment(formattedEntry);
String entryWithoutComment = inlineComment != null
? formattedEntry.substring(0, formattedEntry.length() - inlineComment.length()).trim()
: formattedEntry;

Matcher matcher = ENTRY_LINE.matcher(entryWithoutComment);
if (!matcher.matches()) {
return formattedEntry;
}

String key = matcher.group(1).trim();
String value = matcher.group(2).trim();
String commentSuffix = inlineComment != null ? " " + inlineComment : "";

if (value.startsWith("{") && value.endsWith("}")) {
if (formattedEntry.length() > maxLineLength) {
return splitInlineTable(key, value, commentSuffix);
}
return formattedEntry;
}

return formattedEntry;
}

private static String splitInlineTable(String key, String value, String commentSuffix) {
String inner = value.substring(1, value.length() - 1).trim();
String[] pairs = splitTopLevel(inner, ',');

StringBuilder result = new StringBuilder();
result.append(key).append(" = {").append(commentSuffix).append('\n');
for (int i = 0; i < pairs.length; i++) {
String pair = pairs[i].trim();
if (pair.isEmpty()) {
continue;
}
result.append(" ").append(pair).append(',').append('\n');
}
result.append('}');
return result.toString();
}

private static String extractInlineComment(String valueAndComment) {
boolean inQuote = false;
int depth = 0;
Expand Down Expand Up @@ -398,18 +345,16 @@ private static boolean isBareKey(String key) {
}

private static final class State implements Serializable {
private static final long serialVersionUID = 2L;
private static final long serialVersionUID = 3L;

private final boolean stripQuotedKeys;
private final int maxLineLength;

State(boolean stripQuotedKeys, int maxLineLength) {
State(boolean stripQuotedKeys) {
this.stripQuotedKeys = stripQuotedKeys;
this.maxLineLength = maxLineLength;
}

FormatterFunc toFormatter() {
return raw -> format(raw, stripQuotedKeys, maxLineLength);
return raw -> format(raw, stripQuotedKeys);
}
}

Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Fixed
- `forbidWildcardImports` and `forbidModuleImports` now detect imports that have leading whitespace (indentation/tabs). ([#2939](https://github.com/diffplug/spotless/pull/2939))
- `versionCatalog()` no longer splits long inline tables across multiple lines — Gradle's TOML 1.0 parser cannot read multi-line inline tables. The `maxLineLength` option has been removed. ([#2948](https://github.com/diffplug/spotless/issues/2948))
### Changes
- Improved formatting performance by eliminating redundant per-step line-ending normalization in the core formatter loop. ([#2934](https://github.com/diffplug/spotless/pull/2934))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public VersionCatalogConfig versionCatalog() {

public class VersionCatalogConfig {
private boolean stripQuotedKeys;
private int maxLineLength = 120;

public VersionCatalogConfig() {
this.stripQuotedKeys = false;
Expand All @@ -54,13 +53,8 @@ public void stripQuotedKeys(boolean stripQuotedKeys) {
replaceStep(createStep());
}

public void maxLineLength(int maxLineLength) {
this.maxLineLength = maxLineLength;
replaceStep(createStep());
}

private FormatterStep createStep() {
return VersionCatalogStep.create(stripQuotedKeys, maxLineLength);
return VersionCatalogStep.create(stripQuotedKeys);
}
}
}
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Fixed
- `<versionCatalog>` no longer splits long inline tables across multiple lines — Gradle's TOML 1.0 parser cannot read multi-line inline tables. The `maxLineLength` option has been removed. ([#2948](https://github.com/diffplug/spotless/issues/2948))
- `spotless:apply` no longer aborts on the first file with lints; it now formats all files and reports a single aggregated lint failure across every file, matching the Gradle plugin's behavior. ([#2937](https://github.com/diffplug/spotless/pull/2937))
- `forbidWildcardImports` and `forbidModuleImports` now detect imports that have leading whitespace (indentation/tabs). ([#2939](https://github.com/diffplug/spotless/pull/2939))
### Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ public class VersionCatalog implements FormatterStepFactory {
@Parameter
private boolean stripQuotedKeys;

@Parameter
private int maxLineLength = 120;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
return VersionCatalogStep.create(stripQuotedKeys, maxLineLength);
return VersionCatalogStep.create(stripQuotedKeys);
}
}
5 changes: 1 addition & 4 deletions testlib/src/main/resources/toml/versionCatalogClean.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine" }
junit-jupiter-params = { module = "org.junit.jupiter:junit-jupiter-params" }
junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher" }
testcontainers-junit-jupiter = {
module = "org.testcontainers:testcontainers-junit-jupiter",
version.ref = "testcontainers",
}
testcontainers-junit-jupiter = { module = "org.testcontainers:testcontainers-junit-jupiter", version.ref = "testcontainers" }
testcontainers-postgresql = { module = "org.testcontainers:testcontainers-postgresql", version.ref = "testcontainers" }

[bundles]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,27 @@ void multiLineInlineTableWithTrailingComma() throws Exception {
}

@Test
void longLineSplitsInlineTable() throws Exception {
StepHarness harness = StepHarness.forStep(VersionCatalogStep.create(false, 40));
harness.test(
"[libraries]\nfoo = { module = \"org.example:foo-bar\", version.ref = \"fooBar\" }\n",
"[libraries]\nfoo = {\n module = \"org.example:foo-bar\",\n version.ref = \"fooBar\",\n}\n");
}

@Test
void shortLineStaysSingleLine() throws Exception {
StepHarness harness = StepHarness.forStep(VersionCatalogStep.create(false, 200));
harness.test(
"[libraries]\nfoo = {module=\"org:foo\",version.ref=\"bar\"}\n",
"[libraries]\nfoo = { module = \"org:foo\", version.ref = \"bar\" }\n");
}

@Test
void splitLineIdempotent() throws Exception {
StepHarness harness = StepHarness.forStep(VersionCatalogStep.create(false, 40));
void longLineStaysSingleLine() throws Exception {
StepHarness harness = StepHarness.forStep(VersionCatalogStep.create());
harness.testUnaffected(
"[libraries]\nfoo = {\n module = \"org.example:foo-bar\",\n version.ref = \"fooBar\",\n}\n");
"[libraries]\nfoo = { module = \"org.example:foo-bar-baz-qux\", version.ref = \"fooBarBazQux\" }\n");
}

@Test
void equality() throws Exception {
new SerializableEqualityTester() {
boolean stripQuotedKeys;
int maxLineLength = 120;

@Override
protected void setupTest(API api) {
api.areDifferentThan();
stripQuotedKeys = true;
api.areDifferentThan();
maxLineLength = 80;
api.areDifferentThan();
}

@Override
protected FormatterStep create() {
return VersionCatalogStep.create(stripQuotedKeys, maxLineLength);
return VersionCatalogStep.create(stripQuotedKeys);
}
}.testEquals();
}
Expand Down
Loading