Skip to content

Fix grid routing, shape sizing, declaration order and balancing - #3036

Merged
alixander merged 10 commits into
masterfrom
fix/grid-layout-audit
Nov 25, 2026
Merged

alixander merged 10 commits into
masterfrom
fix/grid-layout-audit

Conversation

@alixander

@alixander alixander commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Human


AI

This fixes grid layout cases where D2 changed the meaning of a diagram. A three-cell grid could draw A → C through B even with TALA, a foreign key could attach to the wrong SQL field, and repeating an unchanged dimension could swap two cells or animate an otherwise unchanged step.

  • Route direct grid links with the selected router, preserve named SQL endpoints, separate reciprocal edges even when cells touch, and draw self-loops. ELK and TALA also support loops on grid containers. Dagre retains its container-loop diagnostic.
  • Retain the first effective axis declaration across overrides, imports and inherited steps. Keep cells introduced by a variable spread at the spread’s position when later connections reference them. Allow TALA annotations to target a grid container.
  • Allocate enough room for nested contents, border labels and icons, and preserve shape constraints when a grid track stretches. A 60 × 60 circle beside a 200 px tall peer now stays round.
  • Minimize total deviation from average track length for bounded integer partitions, and score every affordable fractional partition. The audit’s five-cell grid now occupies 140 px instead of 240 px. Searches above the work or memory budget retain the bounded heuristic.

The regression tests cover the parser-to-layout path, rendered endpoints, nested shapes, serialization, step animation and partition scores against exhaustive enumeration. Updated rendering snapshots record the resulting geometry. Additional review cases cover touching cells on both axes, C4 person body bounds, border icons that grow with their cells, and rerouting beside an existing TALA loop.

Additional review fixes

  • Nested variable spreads now keep cells in declaration order through forward references, repeated imports and steps. Editing one use of a shared grid template preserves its sibling's children.
  • Nested grid containers include the final cross-cell route, caption, icon and arrowhead labels. Zero-gap grids make space for self-loops, including a loop on the center cell of a 3 × 3 grid. Long reciprocal captions use separate positions.
  • Moving SQL connections stay attached to named rows as cells swap. Straight links disappear while the shapes cover the shaft. Links between separate rows can follow the vertical gap during a column swap. Regression tests check the intermediate frames in both link directions.
  • Grid children clear the crown of a person, the folded corner of a page and the curves of a cloud. The SVG viewport includes border icons and decorated captions at zero padding. The original border-icon fixture clipped 29 px.

Variable spread precedence

A map spread now supplies its values at the line where it is used. This four-cell example draws two rows with Dagre, ELK, and TALA. Moving the spread below the explicit setting draws one row.

vars: {defaults: {grid-rows: 1}}
...${defaults}
grid-rows: 2
A; B; C; D

The same rule applies to labels and styles. Nested maps keep their other fields, and later references to a shape leave its assigned value intact. Regression tests exercise repeated imports, inherited steps, wildcard assignments, array replacement, and deletion followed by re-creation.

Tracking source positions also uses some memory. On an Apple M4, the existing compiler benchmarks allocated about 5% more bytes per compile for 1,000 flat fields or 1,000 fields with a leading wildcard. Fifty selected imports allocated about 10% more. Those comparisons use the previous PR head (a8da1c7) as the baseline.

Partition search benchmark

Median of three runs of the existing BenchmarkDynamicGridSearch on an Apple M4, one Go worker, 400 ms per case. These measurements time the partition search alone.

Cells / rows Master (00d5059) This PR (07704de) Allocated per search, master → PR
20 / 4 8.45 µs 1.75 µs 2,032 → 2,104 B
50 / 8 11.93 ms 5.38 µs 6,120 → 5,616 B
100 / 10 27.94 ms 11.38 µs 22,680 → 12,096 B
400 / 20 262.14 ms 75.14 µs 27,432 → 65,616 B

The larger case uses more memory to recover the optimal cuts. The dynamic program caps the number of states at one million.

Verification

  • Two differential checks compared 8,320 four-statement programs against the same declarations written inline, including deletion and re-creation of connected endpoints. All compiled outputs matched.
  • The full Go suite passed locally, including the rendering snapshots and CLI end-to-end tests.
  • Grid and loop regression tests passed with the race detector. go vet --composites=false ./... passed.
  • I rendered the original fixtures with the rebuilt CLI, checked the SVG geometry across Dagre, ELK and TALA, and verified that redeclaring a grid dimension leaves the animated step stationary.

Fixes #1405
Fixes #1741
Fixes #2115

  • sent from alixander's Codex

@alixander
alixander marked this pull request as ready for review November 25, 2026 10:24
@alixander
alixander merged commit 4e2eeed into master Nov 25, 2026
17 checks passed
@alixander
alixander deleted the fix/grid-layout-audit branch November 25, 2026 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In order placement of imported objects multiple connections between grid cells blend together grid resizing person shape past aspect ratio limit

1 participant