Skip to content

Commit

Permalink
d2ir: Review fixes #714
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Jan 28, 2023
1 parent 654475f commit 30b5b64
Show file tree
Hide file tree
Showing 358 changed files with 3,195 additions and 11,264 deletions.
8 changes: 8 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (r *Range) UnmarshalText(b []byte) (err error) {
return r.End.UnmarshalText(end)
}

func (r Range) Before(r2 Range) bool {
return r.Start.Before(r2.Start)
}

// Position represents a line:column and byte position in a file.
//
// note: Line and Column are zero indexed.
Expand Down Expand Up @@ -259,6 +263,10 @@ func (p Position) SubtractString(s string, byUTF16 bool) Position {
return p
}

func (p Position) Before(p2 Position) bool {
return p.Byte < p2.Byte
}

// MapNode is implemented by nodes that may be children of Maps.
type MapNode interface {
Node
Expand Down
4 changes: 3 additions & 1 deletion d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func Compile(path string, r io.RuneReader, opts *CompileOptions) (*d2graph.Graph
if err != nil {
return nil, err
}
return g, err
g = g.SortEdgesByAST()
return g, nil
}

func compileIR(ast *d2ast.Map, m *d2ir.Map) (*d2graph.Graph, error) {
Expand Down Expand Up @@ -203,6 +204,7 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
} else {
attrs.Shape.Value = d2target.ShapeCode
}
attrs.Label.Value = scalar.ScalarString()
default:
attrs.Label.Value = scalar.ScalarString()
}
Expand Down
16 changes: 16 additions & 0 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"net/url"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -1436,3 +1437,18 @@ func (g *Graph) GetBoard(name string) *Graph {
}
return nil
}

func (g *Graph) SortEdgesByAST() *Graph {
edges := append([]*Edge(nil), g.Edges...)
sort.Slice(edges, func(i, j int) bool {
e1 := edges[i]
e2 := edges[j]
if len(e1.References) == 0 || len(e2.References) == 0 {
return i < j
}
return e1.References[0].Edge.Range.Before(edges[j].References[0].Edge.Range)
})
g2 := *g
g2.Edges = edges
return &g2
}
4 changes: 3 additions & 1 deletion d2layouts/d2sequence/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"sort"
"strings"

"oss.terrastruct.com/util-go/go2"

"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
"oss.terrastruct.com/util-go/go2"
)

func WithoutSequenceDiagrams(ctx context.Context, g *d2graph.Graph) (map[string]*sequenceDiagram, map[string]int, map[string]int, error) {
Expand Down Expand Up @@ -113,6 +114,7 @@ func layoutSequenceDiagram(g *d2graph.Graph, obj *d2graph.Object) (*sequenceDiag
func getLayoutEdges(g *d2graph.Graph, toRemove map[*d2graph.Edge]struct{}) ([]*d2graph.Edge, map[string]int) {
edgeOrder := make(map[string]int)
layoutEdges := make([]*d2graph.Edge, 0, len(g.Edges)-len(toRemove))

for i, edge := range g.Edges {
edgeOrder[edge.AbsID()] = i
if _, exists := toRemove[edge]; !exists {
Expand Down
3 changes: 0 additions & 3 deletions d2lib/d2.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,20 @@ func compile(ctx context.Context, g *d2graph.Graph, opts *CompileOptions) (*d2ta
if err != nil {
return nil, err
}
ld.Type = "layer"
d.Layers = append(d.Layers, ld)
}
for _, l := range g.Scenarios {
ld, err := compile(ctx, l, opts)
if err != nil {
return nil, err
}
ld.Type = "scenario"
d.Scenarios = append(d.Scenarios, ld)
}
for _, l := range g.Steps {
ld, err := compile(ctx, l, opts)
if err != nil {
return nil, err
}
ld.Type = "step"
d.Steps = append(d.Steps, ld)
}
return d, nil
Expand Down
12 changes: 6 additions & 6 deletions d2renderers/d2sketch/testdata/opacity/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 18 additions & 11 deletions d2renderers/d2sketch/testdata/twitter/sketch.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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions d2target/d2target.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ var BorderOffset = geo.NewVector(5, 5)

type Diagram struct {
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description,omitempty"`
FontFamily *d2fonts.FontFamily `json:"fontFamily,omitempty"`

Shapes []Shape `json:"shapes"`
Connections []Connection `json:"connections"`

Layers []*Diagram `json:"layers"`
Scenarios []*Diagram `json:"scenarios"`
Steps []*Diagram `json:"steps"`
Layers []*Diagram `json:"layers,omitempty"`
Scenarios []*Diagram `json:"scenarios,omitempty"`
Steps []*Diagram `json:"steps,omitempty"`
}

func (diagram Diagram) HashID() (string, error) {
Expand Down
6 changes: 1 addition & 5 deletions e2etests/testdata/measured/empty-class/dagre/board.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions e2etests/testdata/measured/empty-shape/dagre/board.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 30b5b64

Please sign in to comment.