Skip to content

Commit

Permalink
d2sequence: ensure order for child boards
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Sep 11, 2024
1 parent 4e93f70 commit 7c96795
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 1 deletion.
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
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 7c96795

Please sign in to comment.