Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2oracle: delete nested label/icon fields #1856

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions d2oracle/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,16 +1248,23 @@ func deleteReserved(g *d2graph.Graph, baseAST *d2ast.Map, mk *d2ast.Key) (*d2gra
return recompile(g)
}

isStyleKey := false
isNestedKey := false
imported := false
for _, id := range d2graph.Key(targetKey) {
parts := d2graph.Key(targetKey)
for i, id := range parts {
_, ok := d2graph.ReservedKeywords[id]
if ok {
if id == "style" {
isStyleKey = true
isNestedKey = true
continue
}
if isStyleKey {
if id == "label" || id == "icon" {
if i < len(parts)-1 {
isNestedKey = true
continue
}
}
if isNestedKey {
if imported {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
appendMapKey(baseAST, mk)
Expand Down Expand Up @@ -1306,6 +1313,8 @@ func deleteMapField(m *d2ast.Map, field string) {
if n.MapKey.Key.Path[0].Unbox().ScalarString() == field {
deleteFromMap(m, n.MapKey)
} else if n.MapKey.Key.Path[0].Unbox().ScalarString() == "style" ||
n.MapKey.Key.Path[0].Unbox().ScalarString() == "label" ||
n.MapKey.Key.Path[0].Unbox().ScalarString() == "icon" ||
n.MapKey.Key.Path[0].Unbox().ScalarString() == "source-arrowhead" ||
n.MapKey.Key.Path[0].Unbox().ScalarString() == "target-arrowhead" {
if n.MapKey.Value.Map != nil {
Expand Down Expand Up @@ -1354,7 +1363,9 @@ func deleteObjField(g *d2graph.Graph, obj *d2graph.Object, field string) error {
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == obj.ID) ||
(len(ref.Key.Path) >= 3 &&
ref.Key.Path[len(ref.Key.Path)-1].Unbox().ScalarString() == field &&
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "style" &&
(ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "style" ||
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "label" ||
ref.Key.Path[len(ref.Key.Path)-2].Unbox().ScalarString() == "icon") &&
ref.Key.Path[len(ref.Key.Path)-3].Unbox().ScalarString() == obj.ID) {
tmpNodes := make([]d2ast.MapNodeBox, len(ref.Scope.Nodes))
copy(tmpNodes, ref.Scope.Nodes)
Expand Down
18 changes: 18 additions & 0 deletions d2oracle/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7427,6 +7427,24 @@ a.link: null
key: `a.style.fill`,
exp: `...@meow
a.style.fill: null
`,
},
{
name: "label-near/1",

text: `yes: {label.near: center-center}
`,
key: `yes.label.near`,
exp: `yes
`,
},
{
name: "label-near/2",

text: `yes.label.near: center-center
`,
key: `yes.label.near`,
exp: `yes
`,
},
}
Expand Down
106 changes: 106 additions & 0 deletions testdata/d2oracle/TestDelete/label-near.exp.json

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

106 changes: 106 additions & 0 deletions testdata/d2oracle/TestDelete/label-near/1.exp.json

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

106 changes: 106 additions & 0 deletions testdata/d2oracle/TestDelete/label-near/2.exp.json

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

Loading