This commit is contained in:
Alexander Wang 2023-05-12 16:18:50 -07:00
parent ece56bc556
commit 503a31e034
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 448 additions and 1 deletions

View file

@ -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)
}

View file

@ -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
}
`,
},
{

View file

@ -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": "<nil>"
}