Rename to Layout

This commit is contained in:
Júlio César Batista 2022-12-01 11:47:27 -08:00
parent 7f26540d64
commit 21083910e5
No known key found for this signature in database
GPG key ID: 10C4B861BF314878
4 changed files with 29 additions and 21 deletions

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
@ -14,7 +13,6 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
"oss.terrastruct.com/d2" "oss.terrastruct.com/d2"
"oss.terrastruct.com/d2/d2layouts/d2sequence"
"oss.terrastruct.com/d2/d2plugin" "oss.terrastruct.com/d2/d2plugin"
"oss.terrastruct.com/d2/d2renderers/d2svg" "oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2renderers/textmeasure" "oss.terrastruct.com/d2/d2renderers/textmeasure"
@ -191,10 +189,6 @@ func compile(ctx context.Context, ms *xmain.State, isWatching bool, plugin d2plu
} }
layout := plugin.Layout layout := plugin.Layout
// TODO: remove, this is just a feature flag to test sequence diagrams as we work on them
if os.Getenv("D2_SEQUENCE") == "1" {
layout = d2sequence.Layout
}
d, err := d2.Compile(ctx, string(input), &d2.CompileOptions{ d, err := d2.Compile(ctx, string(input), &d2.CompileOptions{
Layout: layout, Layout: layout,
Ruler: ruler, Ruler: ruler,

8
d2.go
View file

@ -40,11 +40,9 @@ func Compile(ctx context.Context, input string, opts *CompileOptions) (*d2target
return nil, err return nil, err
} }
if layout, err := getLayout(opts); err == nil { if layout, err := getLayout(opts); err != nil {
if err := d2sequence.Layout2(ctx, g, layout); err != nil { return nil, err
return nil, err } else if err := d2sequence.Layout(ctx, g, layout); err != nil {
}
} else {
return nil, err return nil, err
} }

View file

@ -15,7 +15,7 @@ import (
"oss.terrastruct.com/d2/lib/shape" "oss.terrastruct.com/d2/lib/shape"
) )
func Layout2(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error { func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error {
oldObjects := g.Objects oldObjects := g.Objects
oldEdges := g.Edges oldEdges := g.Edges
@ -97,12 +97,6 @@ func Layout2(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Cont
return nil return nil
} }
func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
sd := newSequenceDiagram(nil, nil)
sd.layout()
return nil
}
type sequenceDiagram struct { type sequenceDiagram struct {
messages []*d2graph.Edge messages []*d2graph.Edge
lifelines []*d2graph.Edge lifelines []*d2graph.Edge

View file

@ -26,6 +26,7 @@ func TestBasicSequenceDiagram(t *testing.T) {
// ◄───────────────────────┤ // ◄───────────────────────┤
// │ │ // │ │
g := d2graph.NewGraph(nil) g := d2graph.NewGraph(nil)
g.Root.Attributes.Shape = d2graph.Scalar{Value: d2target.ShapeSequenceDiagram}
n1 := g.Root.EnsureChild([]string{"n1"}) n1 := g.Root.EnsureChild([]string{"n1"})
n1.Box = geo.NewBox(nil, 100, 100) n1.Box = geo.NewBox(nil, 100, 100)
n2 := g.Root.EnsureChild([]string{"n2"}) n2 := g.Root.EnsureChild([]string{"n2"})
@ -58,7 +59,17 @@ func TestBasicSequenceDiagram(t *testing.T) {
nEdges := len(g.Edges) nEdges := len(g.Edges)
ctx := log.WithTB(context.Background(), t, nil) ctx := log.WithTB(context.Background(), t, nil)
Layout(ctx, g) Layout(ctx, g, func(ctx context.Context, g *d2graph.Graph) error {
// just set some position as if it had been properly placed
for _, obj := range g.Objects {
obj.TopLeft = geo.NewPoint(0, 0)
}
for _, edge := range g.Edges {
edge.Route = []*geo.Point{geo.NewPoint(1, 1)}
}
return nil
})
// asserts that actors were placed in the expected x order and at y=0 // asserts that actors were placed in the expected x order and at y=0
actors := []*d2graph.Object{ actors := []*d2graph.Object{
@ -158,6 +169,7 @@ func TestSpansSequenceDiagram(t *testing.T) {
// t2 ││ │ // t2 ││ │
// ├┘◄─────────────────────┤ // ├┘◄─────────────────────┤
g := d2graph.NewGraph(nil) g := d2graph.NewGraph(nil)
g.Root.Attributes.Shape = d2graph.Scalar{Value: d2target.ShapeSequenceDiagram}
a := g.Root.EnsureChild([]string{"a"}) a := g.Root.EnsureChild([]string{"a"})
a.Box = geo.NewBox(nil, 100, 100) a.Box = geo.NewBox(nil, 100, 100)
a.Attributes = d2graph.Attributes{ a.Attributes = d2graph.Attributes{
@ -190,7 +202,17 @@ func TestSpansSequenceDiagram(t *testing.T) {
} }
ctx := log.WithTB(context.Background(), t, nil) ctx := log.WithTB(context.Background(), t, nil)
Layout(ctx, g) Layout(ctx, g, func(ctx context.Context, g *d2graph.Graph) error {
// just set some position as if it had been properly placed
for _, obj := range g.Objects {
obj.TopLeft = geo.NewPoint(0, 0)
}
for _, edge := range g.Edges {
edge.Route = []*geo.Point{geo.NewPoint(1, 1)}
}
return nil
})
// check properties // check properties
if a.Attributes.Shape.Value != shape.PERSON_TYPE { if a.Attributes.Shape.Value != shape.PERSON_TYPE {
@ -335,7 +357,7 @@ func TestNestedSequenceDiagrams(t *testing.T) {
} }
ctx := log.WithTB(context.Background(), t, nil) ctx := log.WithTB(context.Background(), t, nil)
if err = Layout2(ctx, g, layoutFn); err != nil { if err = Layout(ctx, g, layoutFn); err != nil {
t.Fatal(err) t.Fatal(err)
} }