diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 0063b8552e..5b314470fd 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -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 } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index f12d108b70..c409cbd96e 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -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 + } +} `, }, } diff --git a/testdata/d2oracle/TestSet/import/8.exp.json b/testdata/d2oracle/TestSet/import/8.exp.json new file mode 100644 index 0000000000..6c3a722e83 --- /dev/null +++ b/testdata/d2oracle/TestSet/import/8.exp.json @@ -0,0 +1,455 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "index.d2,0:0:0-8:0:59", + "nodes": [ + { + "map_key": { + "range": "index.d2,0:0:0-0:1:1", + "key": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "index.d2,2:0:3-7:1:58", + "key": { + "range": "index.d2,2:0:3-2:6:9", + "path": [ + { + "unquoted_string": { + "range": "index.d2,2:0:3-2:6:9", + "value": [ + { + "string": "layers", + "raw_string": "layers" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "index.d2,2:8:11-7:1:58", + "nodes": [ + { + "map_key": { + "range": "index.d2,3:2:15-6:3:56", + "key": { + "range": "index.d2,3:2:15-3:3:16", + "path": [ + { + "unquoted_string": { + "range": "index.d2,3:2:15-3:3:16", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "index.d2,3:5:18-6:3:56", + "nodes": [ + { + "import": { + "range": "index.d2,4:4:24-4:10:30", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "index.d2,4:8:28-4:10:30", + "value": [ + { + "string": "yo", + "raw_string": "yo" + } + ] + } + } + ] + } + }, + { + "map_key": { + "range": "index.d2,5:4:35-5:21:52", + "key": { + "range": "index.d2,5:4:35-5:16:47", + "path": [ + { + "unquoted_string": { + "range": "index.d2,5:4:35-5:5:36", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,5:6:37-5:11:42", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,5:12:43-5:16:47", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "index.d2,5:18:49-5:21:52", + "value": [ + { + "string": "red", + "raw_string": "red" + } + ] + } + } + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ], + "layers": [ + { + "name": "x", + "isFolderOnly": false, + "ast": { + "range": ",1:0:0-2:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "b" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": ",1:0:0-2:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "style" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": ",1:0:0-2:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "fill" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": "index.d2,5:18:49-5:21:52", + "value": [ + { + "string": "red", + "raw_string": "red" + } + ] + } + }, + "value": {} + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "yo.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "yo.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + }, + { + "key": { + "range": "index.d2,5:4:35-5:16:47", + "path": [ + { + "unquoted_string": { + "range": "index.d2,5:4:35-5:5:36", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,5:6:37-5:11:42", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,5:12:43-5:16:47", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": { + "fill": { + "value": "red" + } + }, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + } + ] + }, + "err": "" +}