Skip to content

Commit

Permalink
Merge pull request #2093 from alixander/sequence-layers
Browse files Browse the repository at this point in the history
d2sequence: fix sequence diagrams when appearing in child boards
  • Loading branch information
alixander authored Sep 11, 2024
2 parents 4740645 + 90dd854 commit fe64773
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 24 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

- Sequence diagram: multi-line edge labels no longer can collide with other elements [#2049](https://github.com/terrastruct/d2/pull/2049)
- Sequence diagram: long self-referential edge labels no longer can collide neighboring actors (or its own) lifeline edges [#2050](https://github.com/terrastruct/d2/pull/2050)
- Sequence diagram: fixes layout when sequence diagrams are in children boards (e.g. a layer) [#1692](https://github.com/terrastruct/d2/issues/1692)
- Globs: An edge case was fixed where globs used in edges were creating nodes when it shouldn't have [#2051](https://github.com/terrastruct/d2/pull/2051)
- Render: Multi-line class labels/headers are rendered correctly [#2057](https://github.com/terrastruct/d2/pull/2057)
- CLI: Watch mode uses correct backlinks (`_` usages) [#2058](https://github.com/terrastruct/d2/pull/2058)
Expand Down
4 changes: 2 additions & 2 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2979,8 +2979,8 @@ qa: {
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "dev.env", g.Objects[1].AbsID())
tassert.Equal(t, "Dev Environment", g.Objects[1].Label.Value)
tassert.Equal(t, "qa.env", g.Objects[2].AbsID())
tassert.Equal(t, "Qa Environment", g.Objects[2].Label.Value)
tassert.Equal(t, "qa.env", g.Objects[4].AbsID())
tassert.Equal(t, "Qa Environment", g.Objects[4].Label.Value)
},
},
{
Expand Down
4 changes: 3 additions & 1 deletion d2graph/seqdiagram.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package d2graph

import "oss.terrastruct.com/d2/d2target"
import (
"oss.terrastruct.com/d2/d2target"
)

func (obj *Object) IsSequenceDiagram() bool {
return obj != nil && obj.Shape.Value == d2target.ShapeSequenceDiagram
Expand Down
11 changes: 4 additions & 7 deletions d2ir/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ func (c *compiler) _import(imp *d2ast.Import) (Node, bool) {
if !ok {
return nil, false
}
nilScopeMap(ir)
if len(imp.IDA()) > 0 {
f := ir.GetField(imp.IDA()...)
if f == nil {
c.errorf(imp, "import key %q doesn't exist inside import", imp.IDA())
return nil, false
}
if f.Map() != nil {
nilScopeMap(f.Map())
}
return f, true
}
nilScopeMap(ir)
return ir, true
}

Expand Down Expand Up @@ -132,15 +135,9 @@ func nilScopeMap(n Node) {
for _, r := range n.References {
r.Context_.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
case *Field:
for _, r := range n.References {
r.Context_.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
}
}
9 changes: 9 additions & 0 deletions d2layouts/d2sequence/sequence_diagram.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package d2sequence

import (
"cmp"
"errors"
"fmt"
"math"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -76,6 +78,13 @@ func newSequenceDiagram(objects []*d2graph.Object, messages []*d2graph.Edge) (*s
var actors []*d2graph.Object
var groups []*d2graph.Object

slices.SortFunc(objects, func(a, b *d2graph.Object) int {
return cmp.Compare(getObjEarliestLineNum(a), getObjEarliestLineNum(b))
})
slices.SortFunc(messages, func(a, b *d2graph.Edge) int {
return cmp.Compare(getEdgeEarliestLineNum(a), getEdgeEarliestLineNum(b))
})

for _, obj := range objects {
if obj.IsSequenceDiagramGroup() {
queue := []*d2graph.Object{obj}
Expand Down
24 changes: 24 additions & 0 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ func TestCLI_E2E(t *testing.T) {
assert.TestdataDir(t, filepath.Join(dir, "layer-link"))
},
},
{
name: "sequence-layer",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "index.d2", `k; layers: { seq: @seq.d2 }`)
writeFile(t, dir, "seq.d2", `shape: sequence_diagram
a: me
b: github.com/terrastruct/d2
a -> b: issue about a bug
a."some note about the bug"
if i'm right: {
a <- b: fix
}
if i'm wrong: {
a <- b: nah, intended
}`)
err := runTestMain(t, ctx, dir, env, "index.d2")
assert.Success(t, err)

assert.TestdataDir(t, filepath.Join(dir, "index"))
},
},
{
// Skip the empty base board so the animation doesn't show blank for 1400ms
name: "empty-base",
Expand Down
95 changes: 95 additions & 0 deletions e2etests-cli/testdata/TestCLI_E2E/sequence-layer/index.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fe64773

Please sign in to comment.