From 503a31e03473da2776f3b81fe9c74615fe488c3a Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Fri, 12 May 2023 16:18:50 -0700 Subject: [PATCH] fix --- d2oracle/edit.go | 11 +- d2oracle/edit_test.go | 16 + .../underscore_edge_container_6.exp.json | 422 ++++++++++++++++++ 3 files changed, 448 insertions(+), 1 deletion(-) create mode 100644 testdata/d2oracle/TestMove/underscore_edge_container_6.exp.json diff --git a/d2oracle/edit.go b/d2oracle/edit.go index f8fbbdd91..e431d4b68 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -1683,6 +1683,15 @@ func move(g *d2graph.Graph, key, newKey string, includeDescendants bool) (*d2gra continue } + firstNonUnderscoreIndex := 0 + ida := d2graph.Key(ref.Key) + for i, id := range ida { + if id != "_" { + firstNonUnderscoreIndex = i + break + } + } + if ref.KeyPathIndex != len(ref.Key.Path)-1 { // When moving a node out of an edge, e.g. the `b` out of `a.b.c -> ...`, // The edge needs to continue targeting the same thing (c) @@ -1707,7 +1716,7 @@ func move(g *d2graph.Graph, key, newKey string, includeDescendants bool) (*d2gra diff = len(getUncommonPath(d2graph.Key(&d2ast.KeyPath{Path: oldPath}), d2graph.Key(&d2ast.KeyPath{Path: newPath}))) } // If the old key is longer than the new key, we already know all the diff would be dropped - if diff > 0 { + if diff > 0 && ref.KeyPathIndex != firstNonUnderscoreIndex { detachedMK.Key.Path = append([]*d2ast.StringBox{}, ref.Key.Path[ref.KeyPathIndex-diff:ref.KeyPathIndex]...) appendUniqueMapKey(ref.Scope, detachedMK) } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 873c9c39b..0fbaedd30 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -3315,6 +3315,22 @@ c exp: `a: { _.c.b -> _.c } +`, + }, + { + name: "underscore_edge_container_6", + + text: `x: { + _.y.a -> _.y.b +} +`, + key: `y`, + newKey: `x.y`, + includeDescendants: true, + + exp: `x: { + y.a -> y.b +} `, }, { diff --git a/testdata/d2oracle/TestMove/underscore_edge_container_6.exp.json b/testdata/d2oracle/TestMove/underscore_edge_container_6.exp.json new file mode 100644 index 000000000..d8fc3cf89 --- /dev/null +++ b/testdata/d2oracle/TestMove/underscore_edge_container_6.exp.json @@ -0,0 +1,422 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,0:0:0-3:0:20", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,0:0:0-2:1:19", + "key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,0:3:3-2:0:18", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:12:17", + "edges": [ + { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:12:17", + "src": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:5:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:3:8", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:4:9-1:5:10", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:9:14-1:12:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:9:14-1:10:15", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:11:16-1:12:17", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": [ + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "dst_arrow": true, + "references": [ + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ], + "objects": [ + { + "id": "x", + "id_val": "x", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "y", + "id_val": "y", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:5:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:3:8", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:4:9-1:5:10", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:9:14-1:12:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:9:14-1:10:15", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:11:16-1:12:17", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "y" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:5:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:2:7-1:3:8", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:4:9-1:5:10", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:9:14-1:12:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:9:14-1:10:15", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/underscore_edge_container_6.d2,1:11:16-1:12:17", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}