Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format: move layers, scenarios and steps to bottom of scope. #1424

Merged
7 changes: 7 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

#### Improvements 🧹

- Use shape specific sizing for grid containers [#1294](https://github.com/terrastruct/d2/pull/1294)
- Grid diagrams now support nested shapes or grid diagrams [#1309](https://github.com/terrastruct/d2/pull/1309)
- Grid diagrams will now also use `grid-gap`, `vertical-gap`, and `horizontal-gap` for padding [#1309](https://github.com/terrastruct/d2/pull/1309)
- Watch mode browser uses an error favicon to easily indicate compiler errors. Thanks @sinyo-matu ! [#1240](https://github.com/terrastruct/d2/pull/1240)
- Improves grid layout performance when there are many similarly sized shapes. [#1315](https://github.com/terrastruct/d2/pull/1315)
- Connections and labels now are adjusted for shapes with `3d` or `multiple`. [#1340](https://github.com/terrastruct/d2/pull/1340)
- Display version on CLI help invocation [#1400](https://github.com/terrastruct/d2/pull/1400)
- Improved readability of connection labels when they overlap another connection. [#447](https://github.com/terrastruct/d2/pull/447)
- Error message when `shape` is given a composite [#1415](https://github.com/terrastruct/d2/pull/1415)
- The autoformatter moves board declarations to the bottom of its scope. [#1424](https://github.com/terrastruct/d2/pull/1424)

#### Bugfixes ⛑️

Expand Down
12 changes: 12 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,18 @@ func (mb MapNodeBox) Unbox() MapNode {
}
}

func (mb MapNodeBox) IsBoardNode() bool {
if mb.MapKey == nil || mb.MapKey.Key == nil || len(mb.MapKey.Key.Path) == 0 {
return false
}
switch mb.MapKey.Key.Path[0].Unbox().ScalarString() {
case "layers", "scenarios", "steps":
return true
default:
return false
}
}

// ArrayNodeBox is used to box ArrayNode for JSON persistence.
type ArrayNodeBox struct {
Comment *Comment `json:"comment,omitempty"`
Expand Down
37 changes: 35 additions & 2 deletions d2format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,35 @@ func (p *printer) _map(m *d2ast.Map) {
}
}

prev := d2ast.Node(m)
nodes := []d2ast.MapNodeBox{}
// extract out layer, scenario, and step nodes
layerNodes := []d2ast.MapNodeBox{}
scenarioNodes := []d2ast.MapNodeBox{}
stepNodes := []d2ast.MapNodeBox{}
for i := 0; i < len(m.Nodes); i++ {
nb := m.Nodes[i]
node := m.Nodes[i]
if node.IsBoardNode() {
switch node.MapKey.Key.Path[0].Unbox().ScalarString() {
case "layers":
layerNodes = append(layerNodes, node)
case "scenarios":
scenarioNodes = append(scenarioNodes, node)
case "steps":
stepNodes = append(stepNodes, node)
}
} else {
nodes = append(nodes, node)
}
}

// append layers, scenarios, and steps at the end
nodes = append(nodes, layerNodes...)
nodes = append(nodes, scenarioNodes...)
nodes = append(nodes, stepNodes...)

prev := d2ast.Node(m)
for i := 0; i < len(nodes); i++ {
nb := nodes[i]
n := nb.Unbox()

// Handle inline comments.
Expand All @@ -302,6 +328,13 @@ func (p *printer) _map(m *d2ast.Map) {
p.sb.WriteString("; ")
}

if m.IsFileMap() && nb.IsBoardNode() {
currString := p.sb.String()
// if the two characters before the board node is not a double newline, we add one
if currString[len(currString)-2:] != "\n\n" {
p.newline()
}
}
p.node(n)
prev = n
}
Expand Down
133 changes: 132 additions & 1 deletion d2format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ meow
diagram: int {constraint: foreign_key}
}

meow <- diagrams.id
steps: {
shape: sql_table
id: {type: int; constraint: primary_key}
representation: {type: jsonb}
diagram: int {constraint: foreign_key}
}
meow <- diagrams.id
}

D2 AST Parser: {
Expand Down Expand Up @@ -657,6 +657,137 @@ x: @../file
x: @"x/../file"
`,
exp: `x: @file
`,
},
{
name: "layers_scenarios_steps_bottom_simple",
in: `a

layers: {
b: {
scenarios: {
p: {
x
}
}
e
}
}

g
`,
exp: `a

g

layers: {
b: {
e
scenarios: {
p: {
x
}
}
}
}
`,
},
{
name: "layers_scenarios_steps_bottom_complex",
in: `a

scenarios: {



scenario-1: {
steps: {
step-1: {
Test
}
step-2
}
non-step
}
}

layers: {
Test super nested: {
base-layer
layers: {
layers: {
grand-child-layer: {
grand-child-board
}
}
layer-board
}
last-layer
}
}
b
steps: {
1: {
step-1-content
}
}


scenarios: {
scenario-2: {
scenario-2-content
}
}

c
d
`,
exp: `a

b

c
d

layers: {
Test super nested: {
base-layer

last-layer
layers: {
layer-board
layers: {
grand-child-layer: {
grand-child-board
}
}
}
}
}

scenarios: {
scenario-1: {
non-step
steps: {
step-1: {
Test
}
step-2
}
}
}

scenarios: {
scenario-2: {
scenario-2-content
}
}

steps: {
1: {
step-1-content
}
}
`,
},
}
Expand Down