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
2 changes: 1 addition & 1 deletion docs/reference/support-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The default API behavior is TOML 1.1-compatible. Strict TOML 1.0 parsing/decodin
| Trivia preservation on document items and table entries | Partial | Available through `Toml::parse($input, true)` for leading/trailing trivia on parsed items |
| Trivia preservation inside parsed arrays and inline tables | Partial | Collection-local item spacing and comments are preserved where represented in the AST |
| Comment preservation on re-encode | Partial | Preserved for parsed document items, table entries, and collection items when trivia is available |
| Formatting preservation on re-encode | Partial | Available in `DocumentFormattingMode::SourceAware` for trivia-preserving ASTs |
| Formatting preservation on re-encode | Partial | Available in `DocumentFormattingMode::SourceAware` for trivia-preserving ASTs, including local spacing preservation for common single-line collection edits |
| `encodeDocument()` round-trip fidelity | Partial | Normalized by default; source-aware mode is lossless for unchanged parsed regions, while edited regions preserve local style when inferable and otherwise canonicalize locally |

## Tooling and Errors
Expand Down
23 changes: 23 additions & 0 deletions src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,18 @@ private function inferSingleLineInlineTableStyle(InlineTable $value): ?array
$afterComma = null;
$observedPair = false;
$itemCount = count($value->items);
$reusableItemCount = 0;
$lastReusableItem = null;

for ($index = 1; $index < $itemCount; $index++) {
if (!$this->isSyntheticNode($value->items[$index - 1])) {
$reusableItemCount++;
$lastReusableItem = $value->items[$index - 1];
}
if ($index === $itemCount - 1 && !$this->isSyntheticNode($value->items[$index])) {
$reusableItemCount++;
$lastReusableItem = $value->items[$index];
}
if ($this->isSyntheticNode($value->items[$index - 1]) || $this->isSyntheticNode($value->items[$index])) {
continue;
}
Expand All @@ -1140,6 +1150,19 @@ private function inferSingleLineInlineTableStyle(InlineTable $value): ?array
}

if (!$observedPair) {
if ($reusableItemCount === 1) {
$closingSpacing = $closing;
if ($closingSpacing === '' && $lastReusableItem !== null) {
$closingSpacing = $this->singleLineWhitespaceTrivia($lastReusableItem->getTrailingTrivia()) ?? '';
}

return [
'opening' => $opening !== '' ? $opening : ' ',
'afterComma' => $closingSpacing !== '' ? $closingSpacing : ($opening !== '' ? $opening : ' '),
'closing' => $closingSpacing !== '' ? $closingSpacing : ' ',
];
}

return null;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/EditingFixtureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function testEditedFixturesReencodeAsExpected(string $caseDir): void
'single-line-array-remove-to-one' => $this->applySingleLineArrayRemovalToOne($document),
'single-line-array-remove-consistent' => $this->applySingleLineArrayRemoval($document),
'single-line-inline-insert' => $this->applySingleLineInlineInsert($document),
'single-line-inline-insert-from-one' => $this->applySingleLineInlineInsert($document),
'nested-single-line-inline-insert-from-one' => $this->applyNestedSingleLineInlineInsert($document),
'single-line-inline-remove-to-one' => $this->applySingleLineInlineRemovalToOne($document),
'single-line-inline-remove-consistent' => $this->applySingleLineInlineRemoval($document),
'single-line-value-replace' => $this->applySingleLineValueReplace($document),
Expand Down Expand Up @@ -274,6 +276,20 @@ private function applyNestedInlineReplacement(Document $document): void
);
}

private function applyNestedSingleLineInlineInsert(Document $document): void
{
$item = $document->items[0];
self::assertInstanceOf(KeyValue::class, $item);
self::assertInstanceOf(InlineTable::class, $item->value);
self::assertInstanceOf(InlineTable::class, $item->value->items[0]->value);

$item->value->items[0]->value->items[] = new KeyValue(
new Key(['y'], [KeyStyle::Bare], $this->span()),
new IntegerValue(2, IntegerBase::Decimal, $this->span()),
$this->span(),
);
}

private function readFixture(string $path): string
{
$contents = file_get_contents($path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outer = { inner = { x = 1, y = 2 } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outer = { inner = { x = 1 } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
point = { x = 1, z = 3 }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
point = { x = 1 }
Loading