From 641a6741ff393bffe39ae1901b4d8daf8ff64233 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sat, 18 Feb 2023 14:54:10 -0800 Subject: [PATCH] add top, left keywords --- d2compiler/compile.go | 22 ++++++++++++++++++++++ d2compiler/compile_test.go | 12 +++++++++++- d2graph/d2graph.go | 6 +++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 3bc0f26d1..df5efd7cd 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -285,6 +285,24 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) { attrs.Height = &d2graph.Scalar{} attrs.Height.Value = scalar.ScalarString() attrs.Height.MapKey = f.LastPrimaryKey() + case "top": + _, err := strconv.Atoi(scalar.ScalarString()) + if err != nil { + c.errorf(scalar, "non-integer top %#v: %s", scalar.ScalarString(), err) + return + } + attrs.Top = &d2graph.Scalar{} + attrs.Top.Value = scalar.ScalarString() + attrs.Top.MapKey = f.LastPrimaryKey() + case "left": + _, err := strconv.Atoi(scalar.ScalarString()) + if err != nil { + c.errorf(scalar, "non-integer left %#v: %s", scalar.ScalarString(), err) + return + } + attrs.Left = &d2graph.Scalar{} + attrs.Left.Value = scalar.ScalarString() + attrs.Left.MapKey = f.LastPrimaryKey() case "link": attrs.Link = scalar.ScalarString() case "direction": @@ -364,6 +382,10 @@ func compileStyleFieldInit(attrs *d2graph.Attributes, f *d2ir.Field) { attrs.Width = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} case "height": attrs.Height = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} + case "top": + attrs.Top = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} + case "left": + attrs.Left = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} case "double-border": attrs.Style.DoubleBorder = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} } diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index fa238a2c5..537cec457 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -86,7 +86,6 @@ x: { } }, }, - { name: "dimensions_on_nonimage", @@ -114,6 +113,17 @@ x: { } }, }, + { + name: "positions", + text: `hey: { + top: 200 + left: 230 +} +`, + assertions: func(t *testing.T, g *d2graph.Graph) { + tassert.Equal(t, "200", g.Objects[0].Attributes.Top.Value) + }, + }, { name: "equal_dimensions_on_circle", diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 1300289d6..30bdedcbf 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -98,10 +98,12 @@ type Attributes struct { Tooltip string `json:"tooltip,omitempty"` Link string `json:"link,omitempty"` - // Only applicable for images right now Width *Scalar `json:"width,omitempty"` Height *Scalar `json:"height,omitempty"` + Top *Scalar `json:"top,omitempty"` + Left *Scalar `json:"left,omitempty"` + // TODO consider separate Attributes struct for shape-specific and edge-specific // Shapes only NearKey *d2ast.KeyPath `json:"near_key"` @@ -1442,6 +1444,8 @@ var SimpleReservedKeywords = map[string]struct{}{ "width": {}, "height": {}, "direction": {}, + "top": {}, + "left": {}, } // ReservedKeywordHolders are reserved keywords that are meaningless on its own and exist solely to hold a set of reserved keywords