diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 7b48a4997..f8fbbdd91 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -1543,7 +1543,14 @@ 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 + } + } resolvedObj, resolvedIDA, err := d2graph.ResolveUnderscoreKey(ida, ref.ScopeObj) if err != nil { return nil, err @@ -1568,7 +1575,7 @@ func move(g *d2graph.Graph, key, newKey string, includeDescendants bool) (*d2gra // 4. Slice. // -- The key is moving from its current scope out to a less nested scope if isCrossScope { - if (!includeDescendants && len(ida) == 1) || (includeDescendants && ref.KeyPathIndex == 0) { + if (!includeDescendants && len(ida) == 1) || (includeDescendants && ref.KeyPathIndex == firstNonUnderscoreIndex) { // 1. Transplant absKey, err := d2parser.ParseKey(ref.ScopeObj.AbsID()) if err != nil { diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index c0314a2e0..873c9c39b 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -4342,6 +4342,21 @@ a exp: `x: { a } +`, + }, + { + name: "include_descendants_underscore_regression_2", + text: `x: { + _.a.b +} +`, + key: `a`, + newKey: `x.a`, + includeDescendants: true, + + exp: `x: { + a.b +} `, }, } diff --git a/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.exp.json b/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.exp.json new file mode 100644 index 000000000..153524784 --- /dev/null +++ b/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.exp.json @@ -0,0 +1,266 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,0:0:0-3:0:13", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,0:0:0-2:1:12", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,0:3:3-2:0:11", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:2:7-1:5:10", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:2:7-1:5:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:2:7-1:3:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:4:9-1:5:10", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "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": null, + "objects": [ + { + "id": "x", + "id_val": "x", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.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": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:2:7-1:5:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:2:7-1:3:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:4:9-1:5:10", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "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": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:2:7-1:5:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:2:7-1:3:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore_regression_2.d2,1:4:9-1:5:10", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}