Skip to content

Commit

Permalink
Merge pull request #1802 from alixander/d2oracle-set-imports-again
Browse files Browse the repository at this point in the history
D2oracle set imports again
  • Loading branch information
alixander authored Jan 4, 2024
2 parents d15d5b1 + a6bfd1b commit 7f8d196
Show file tree
Hide file tree
Showing 5 changed files with 1,009 additions and 11 deletions.
21 changes: 14 additions & 7 deletions d2oracle/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
toSkip := 1

reserved := false
imported := false

// If you're setting `(x -> y)[0].style.opacity`
// There's 3 cases you need to handle:
Expand Down Expand Up @@ -382,18 +383,16 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
break
}
obj = o
imported = IsImported(baseAST, obj)

var maybeNewScope *d2ast.Map
if baseAST != g.AST {
if baseAST != g.AST || imported {
writeableRefs := getWriteableRefs(obj, baseAST)
for _, ref := range writeableRefs {
if ref.MapKey != nil && ref.MapKey.Value.Map != nil {
maybeNewScope = ref.MapKey.Value.Map
}
}
} else if IsImported(g, obj) {
appendMapKey(scope, mk)
return nil
} else {
maybeNewScope = obj.Map
}
Expand All @@ -412,8 +411,9 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
}
}

writeableLabelMK := true
var objK *d2ast.Key
if baseAST != g.AST {
if baseAST != g.AST || imported {
writeableRefs := getWriteableRefs(obj, baseAST)
if len(writeableRefs) > 0 {
objK = writeableRefs[0].MapKey
Expand All @@ -422,6 +422,13 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
appendMapKey(scope, mk)
return nil
}
writeableLabelMK = false
for _, ref := range writeableRefs {
if ref.MapKey == obj.Label.MapKey {
writeableLabelMK = true
break
}
}
}
var m *d2ast.Map
if objK != nil {
Expand All @@ -430,7 +437,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
m = obj.Map
}

if obj.Label.MapKey != nil && m == nil && (!found || reserved || len(mk.Edges) > 0) {
if (obj.Label.MapKey != nil && writeableLabelMK) && m == nil && (!found || reserved || len(mk.Edges) > 0) {
m2 := &d2ast.Map{
Range: d2ast.MakeRange(",1:0:0-1:0:0"),
}
Expand Down Expand Up @@ -3107,7 +3114,7 @@ func filterReservedPath(path []*d2ast.StringBox) (filtered []*d2ast.StringBox) {

func getWriteableRefs(obj *d2graph.Object, writeableAST *d2ast.Map) (out []d2graph.Reference) {
for i, ref := range obj.References {
if ref.ScopeAST == writeableAST {
if ref.ScopeAST == writeableAST && ref.Key.Range.Path == writeableAST.Range.Path {
out = append(out, obj.References[i])
}
}
Expand Down
42 changes: 42 additions & 0 deletions d2oracle/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,33 @@ scenarios: {
a: inner {style.opacity: 0.2}
}
}
`,
},
{
name: "scenarios-multiple",

text: `a
scenarios: {
x: {
b
a.style.fill: red
}
}
`,
key: `a.style.opacity`,
value: go2.Pointer(`0.2`),
boardPath: []string{"x"},

exp: `a
scenarios: {
x: {
b
a.style.fill: red
a.style.opacity: 0.2
}
}
`,
},
{
Expand Down Expand Up @@ -2105,6 +2132,21 @@ x: @yo`,
exp: `a
x: @yo
x.b.style.fill: red
`,
},
{
name: "import/7",

text: `...@yo
b.style.fill: red`,
fsTexts: map[string]string{
"yo.d2": `b`,
},
key: `b.style.opacity`,
value: go2.Pointer("0.5"),
exp: `...@yo
b.style.fill: red
b.style.opacity: 0.5
`,
},
}
Expand Down
8 changes: 4 additions & 4 deletions d2oracle/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, er
return obj.Parent.AbsID(), nil
}

func IsImported(g *d2graph.Graph, obj *d2graph.Object) bool {
func IsImported(ast *d2ast.Map, obj *d2graph.Object) bool {
for _, ref := range obj.References {
if ref.MapKey.Range.Path == g.AST.Range.Path {
return false
if ref.Key.Range.Path != ast.Range.Path {
return true
}
}

return true
return false
}

func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object {
Expand Down
Loading

0 comments on commit 7f8d196

Please sign in to comment.