diff --git a/d2oracle/edit.go b/d2oracle/edit.go index e590d3c2d..e5273dfe8 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -1436,16 +1436,12 @@ func Rename(g *d2graph.Graph, boardPath []string, key, newName string) (_ *d2gra } boardG := g - // baseAST := g.AST if len(boardPath) > 0 { - // When compiling a nested board, we can read from boardG but only write to baseBoardG boardG = GetBoardGraph(g, boardPath) if boardG == nil { return nil, "", fmt.Errorf("board %v not found", boardPath) } - // TODO beter name - // baseAST = boardG.BaseAST } if len(mk.Edges) > 0 && mk.EdgeKey == nil { @@ -2845,7 +2841,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err return deltas, nil } -func RenameIDDeltas(g *d2graph.Graph, key, newName string) (deltas map[string]string, err error) { +func RenameIDDeltas(g *d2graph.Graph, boardPath []string, key, newName string) (deltas map[string]string, err error) { defer xdefer.Errorf(&err, "failed to get deltas for renaming of %#v to %#v", key, newName) deltas = make(map[string]string) @@ -2854,11 +2850,19 @@ func RenameIDDeltas(g *d2graph.Graph, key, newName string) (deltas map[string]st return nil, err } + boardG := g + if len(boardPath) > 0 { + boardG = GetBoardGraph(g, boardPath) + if boardG == nil { + return nil, fmt.Errorf("board %v not found", boardPath) + } + } + edgeTrimCommon(mk) - obj := g.Root + obj := boardG.Root if mk.Key != nil { var ok bool - obj, ok = g.Root.HasChild(d2graph.Key(mk.Key)) + obj, ok = boardG.Root.HasChild(d2graph.Key(mk.Key)) if !ok { return nil, nil } @@ -2896,7 +2900,7 @@ func RenameIDDeltas(g *d2graph.Graph, key, newName string) (deltas map[string]st } mk.Key.Path[len(mk.Key.Path)-1].Unbox().SetString(newName) - uniqueKeyStr, _, err := generateUniqueKey(g, strings.Join(d2graph.Key(mk.Key), "."), obj, nil) + uniqueKeyStr, _, err := generateUniqueKey(boardG, strings.Join(d2graph.Key(mk.Key), "."), obj, nil) if err != nil { return nil, err } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index d7e37b231..9acab23eb 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -7933,9 +7933,10 @@ func TestRenameIDDeltas(t *testing.T) { testCases := []struct { name string - text string - key string - newName string + boardPath []string + text string + key string + newName string exp string expErr string @@ -8061,6 +8062,44 @@ x.y.z.w.e.p.l -> x.y.z.1.2.3.4 exp: `{ "x.y.z.(w.e.p.l -> 1.2.3.4)[1]": "x.y.z.(w.e.p.l <-> 1.2.3.4)[1]" +}`, + }, + { + name: "layers-basic", + + text: `x + +layers: { + y: { + a + } +} +`, + boardPath: []string{"y"}, + key: "a", + newName: "b", + + exp: `{ + "a": "b" +}`, + }, + { + name: "scenarios-conflict", + + text: `x + +scenarios: { + y: { + a + } +} +`, + boardPath: []string{"y"}, + key: "a", + newName: "x", + + exp: `{ + "a": "x 2" }`, }, } @@ -8076,7 +8115,7 @@ x.y.z.w.e.p.l -> x.y.z.1.2.3.4 t.Fatal(err) } - deltas, err := d2oracle.RenameIDDeltas(g, tc.key, tc.newName) + deltas, err := d2oracle.RenameIDDeltas(g, tc.boardPath, tc.key, tc.newName) if tc.expErr != "" { if err == nil { t.Fatalf("expected error with: %q", tc.expErr)