fix underscore

This commit is contained in:
Alexander Wang 2023-05-10 16:27:08 -07:00
parent c6820c89cc
commit 677a8daa13
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 289 additions and 1 deletions

View file

@ -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 {

View file

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

View file

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