d2ir: Add IDA support for edges

Not perfectly robust but good enough for now without massive refactor.
This commit is contained in:
Anmol Sethi 2023-08-15 18:39:14 -07:00
parent bb6b176dee
commit 507b2d622e
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB

View file

@ -1151,7 +1151,7 @@ func (m *Map) createEdge2(eid *EdgeID, refctx *RefContext, gctx *globContext, sr
} }
if gctx != nil { if gctx != nil {
ks := e.String() ks := d2format.Format(d2ast.MakeKeyPath(BoardIDA(e)))
if _, ok := gctx.appliedEdges[ks]; ok { if _, ok := gctx.appliedEdges[ks]; ok {
return nil, nil return nil, nil
} }
@ -1389,60 +1389,65 @@ func parentPrimaryKey(n Node) *d2ast.Key {
// BoardIDA returns the absolute path to n from the nearest board root. // BoardIDA returns the absolute path to n from the nearest board root.
func BoardIDA(n Node) (ida []string) { func BoardIDA(n Node) (ida []string) {
for { for {
f, ok := n.(*Field) switch n := n.(type) {
if ok { case *Field:
if f.Root() || NodeBoardKind(f) != "" { if n.Root() || NodeBoardKind(n) != "" {
reverseIDA(ida) reverseIDA(ida)
return ida return ida
} }
ida = append(ida, f.Name) ida = append(ida, n.Name)
case *Edge:
ida = append(ida, n.String())
} }
f = ParentField(n) n = n.Parent()
if f == nil { if n == nil {
reverseIDA(ida) reverseIDA(ida)
return ida return ida
} }
n = f
} }
} }
// IDA returns the absolute path to n. // IDA returns the absolute path to n.
func IDA(n Node) (ida []string) { func IDA(n Node) (ida []string) {
for { for {
f, ok := n.(*Field) switch n := n.(type) {
if ok { case *Field:
ida = append(ida, f.Name) ida = append(ida, n.Name)
if f.Root() { if n.Root() {
reverseIDA(ida) reverseIDA(ida)
return ida return ida
} }
case *Edge:
ida = append(ida, n.String())
} }
f = ParentField(n) n = n.Parent()
if f == nil { if n == nil {
reverseIDA(ida) reverseIDA(ida)
return ida return ida
} }
n = f
} }
} }
// RelIDA returns the path to n relative to p. // RelIDA returns the path to n relative to p.
func RelIDA(p, n Node) (ida []string) { func RelIDA(p, n Node) (ida []string) {
for { for {
f, ok := n.(*Field) switch n := n.(type) {
if ok { case *Field:
ida = append(ida, f.Name) ida = append(ida, n.Name)
if f.Root() { if n.Root() {
reverseIDA(ida) reverseIDA(ida)
return ida return ida
} }
case *Edge:
ida = append(ida, n.String())
} }
f = ParentField(n) n = ParentField(n)
if f == nil || f.Root() || f == p || f.Composite == p { f, fok := n.(*Field)
e, eok := n.(*Edge)
if n == nil || (fok && (f.Root() || f == p || f.Composite == p)) || (eok && (e == p || e.Map_ == p)) {
reverseIDA(ida) reverseIDA(ida)
return ida return ida
} }
n = f
} }
} }