Skip to content

Commit ff9150a

Browse files
authored
Merge pull request #1856 from alixander/delete-label-near
d2oracle: delete nested label/icon fields
2 parents 82070f7 + 113db2f commit ff9150a

File tree

5 files changed

+352
-5
lines changed

5 files changed

+352
-5
lines changed

d2oracle/edit.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,16 +1248,23 @@ func deleteReserved(g *d2graph.Graph, baseAST *d2ast.Map, mk *d2ast.Key) (*d2gra
12481248
return recompile(g)
12491249
}
12501250

1251-
isStyleKey := false
1251+
isNestedKey := false
12521252
imported := false
1253-
for _, id := range d2graph.Key(targetKey) {
1253+
parts := d2graph.Key(targetKey)
1254+
for i, id := range parts {
12541255
_, ok := d2graph.ReservedKeywords[id]
12551256
if ok {
12561257
if id == "style" {
1257-
isStyleKey = true
1258+
isNestedKey = true
12581259
continue
12591260
}
1260-
if isStyleKey {
1261+
if id == "label" || id == "icon" {
1262+
if i < len(parts)-1 {
1263+
isNestedKey = true
1264+
continue
1265+
}
1266+
}
1267+
if isNestedKey {
12611268
if imported {
12621269
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
12631270
appendMapKey(baseAST, mk)
@@ -1306,6 +1313,8 @@ func deleteMapField(m *d2ast.Map, field string) {
13061313
if n.MapKey.Key.Path[0].Unbox().ScalarString() == field {
13071314
deleteFromMap(m, n.MapKey)
13081315
} else if n.MapKey.Key.Path[0].Unbox().ScalarString() == "style" ||
1316+
n.MapKey.Key.Path[0].Unbox().ScalarString() == "label" ||
1317+
n.MapKey.Key.Path[0].Unbox().ScalarString() == "icon" ||
13091318
n.MapKey.Key.Path[0].Unbox().ScalarString() == "source-arrowhead" ||
13101319
n.MapKey.Key.Path[0].Unbox().ScalarString() == "target-arrowhead" {
13111320
if n.MapKey.Value.Map != nil {
@@ -1354,7 +1363,9 @@ func deleteObjField(g *d2graph.Graph, obj *d2graph.Object, field string) error {
13541363
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == obj.ID) ||
13551364
(len(ref.Key.Path) >= 3 &&
13561365
ref.Key.Path[len(ref.Key.Path)-1].Unbox().ScalarString() == field &&
1357-
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "style" &&
1366+
(ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "style" ||
1367+
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "label" ||
1368+
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "icon") &&
13581369
ref.Key.Path[len(ref.Key.Path)-3].Unbox().ScalarString() == obj.ID) {
13591370
tmpNodes := make([]d2ast.MapNodeBox, len(ref.Scope.Nodes))
13601371
copy(tmpNodes, ref.Scope.Nodes)

d2oracle/edit_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7427,6 +7427,24 @@ a.link: null
74277427
key: `a.style.fill`,
74287428
exp: `...@meow
74297429
a.style.fill: null
7430+
`,
7431+
},
7432+
{
7433+
name: "label-near/1",
7434+
7435+
text: `yes: {label.near: center-center}
7436+
`,
7437+
key: `yes.label.near`,
7438+
exp: `yes
7439+
`,
7440+
},
7441+
{
7442+
name: "label-near/2",
7443+
7444+
text: `yes.label.near: center-center
7445+
`,
7446+
key: `yes.label.near`,
7447+
exp: `yes
74307448
`,
74317449
},
74327450
}

testdata/d2oracle/TestDelete/label-near.exp.json

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/d2oracle/TestDelete/label-near/1.exp.json

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/d2oracle/TestDelete/label-near/2.exp.json

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)