Merge pull request #1886 from alixander/fix-edge-set

d2oracle: fix setting edge in an edge case
This commit is contained in:
Alexander Wang 2024-03-29 00:26:34 -07:00 committed by GitHub
commit a0a0ac211c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 3295 additions and 2 deletions

View file

@ -500,7 +500,11 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
refs = GetWriteableEdgeRefs(edge, baseAST) refs = GetWriteableEdgeRefs(edge, baseAST)
} }
onlyInChain := true onlyInChain := true
for _, ref := range refs { var earliestRef *d2graph.EdgeReference
for i, ref := range refs {
if earliestRef == nil || ref.MapKey.Range.Before(earliestRef.MapKey.Range) {
earliestRef = &refs[i]
}
// TODO merge flat edgekeys // TODO merge flat edgekeys
// E.g. this can group into a map // E.g. this can group into a map
// (y -> z)[0].style.opacity: 0.4 // (y -> z)[0].style.opacity: 0.4
@ -526,7 +530,15 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
} }
} }
if onlyInChain { if onlyInChain {
if earliestRef != nil && scope.Range.Before(earliestRef.MapKey.Range) {
// Since the original mk was trimmed to common, we set to the edge that
// the ref's scope is in
mk.Edges[0] = earliestRef.Edge
// We can't reference an edge before it's been defined
earliestRef.Scope.InsertAfter(earliestRef.MapKey, mk)
} else {
appendMapKey(scope, mk) appendMapKey(scope, mk)
}
return nil return nil
} }
attrs = edge.Attributes attrs = edge.Attributes

View file

@ -2454,6 +2454,54 @@ a -> b
exp: `(* -> *)[*].style.stroke: red exp: `(* -> *)[*].style.stroke: red
a -> b: {style.stroke: green} a -> b: {style.stroke: green}
a -> b a -> b
`,
},
{
name: "nested-edge-chained/1",
text: `a: {
b: {
c
}
}
x -> a.b -> a.b.c
`,
key: `(a.b -> a.b.c)[0].style.stroke`,
value: go2.Pointer(`green`),
exp: `a: {
b: {
c
}
}
x -> a.b -> a.b.c
(a.b -> a.b.c)[0].style.stroke: green
`,
},
{
name: "nested-edge-chained/2",
text: `z: {
a: {
b: {
c
}
}
x -> a.b -> a.b.c
}
`,
key: `(z.a.b -> z.a.b.c)[0].style.stroke`,
value: go2.Pointer(`green`),
exp: `z: {
a: {
b: {
c
}
}
x -> a.b -> a.b.c
(a.b -> a.b.c)[0].style.stroke: green
}
`, `,
}, },
} }

1053
testdata/d2oracle/TestSet/nested-edge-chained.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff

1053
testdata/d2oracle/TestSet/nested-edge-chained/1.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff

1127
testdata/d2oracle/TestSet/nested-edge-chained/2.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff