Skip to content

Commit

Permalink
Merge pull request #1810 from alixander/d2oracle-import-board
Browse files Browse the repository at this point in the history
fix d2oracle editing an imported board
  • Loading branch information
alixander authored Jan 21, 2024
2 parents 7f8d196 + 35dd25c commit 1e5d8fa
Show file tree
Hide file tree
Showing 3 changed files with 500 additions and 0 deletions.
21 changes: 21 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ func findFieldAST(ast *d2ast.Map, f *d2ir.Field) *d2ast.Map {
head2 := n.MapKey.Key.Path[0].Unbox().ScalarString()
if head == head2 {
currAST = n.MapKey.Value.Map
// The BaseAST is only used for making edits to the AST (through d2oracle)
// If there's no Map for a given board, either it's an empty layer or set to an import
// Either way, in order to make edits, it needs to be expanded into a Map to add lines to
if currAST == nil {
n.MapKey.Value.Map = &d2ast.Map{
Range: d2ast.MakeRange(",1:0:0-1:0:0"),
}
if n.MapKey.Value.Import != nil {
imp := &d2ast.Import{
Range: d2ast.MakeRange(",1:0:0-1:0:0"),
Spread: true,
Pre: n.MapKey.Value.Import.Pre,
Path: n.MapKey.Value.Import.Path,
}
n.MapKey.Value.Map.Nodes = append(n.MapKey.Value.Map.Nodes, d2ast.MapNodeBox{
Import: imp,
})

}
currAST = n.MapKey.Value.Map
}
found = true
break
}
Expand Down
24 changes: 24 additions & 0 deletions d2oracle/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,30 @@ b.style.fill: red`,
exp: `...@yo
b.style.fill: red
b.style.opacity: 0.5
`,
},
{
name: "import/8",

text: `a
layers: {
x: @yo
}`,
boardPath: []string{"x"},
fsTexts: map[string]string{
"yo.d2": `b`,
},
key: `b.style.fill`,
value: go2.Pointer(`red`),
exp: `a
layers: {
x: {
...@yo
b.style.fill: red
}
}
`,
},
}
Expand Down
Loading

0 comments on commit 1e5d8fa

Please sign in to comment.