diff --git a/d2oracle/edit.go b/d2oracle/edit.go index a06784ef3..bdb548548 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -3406,11 +3406,35 @@ func _updateImport(m *d2ast.Map, oldPath string, newPath *string) { } func updateImportPath(imp *d2ast.Import, newPath string) { - if len(imp.Path) > 0 { - imp.Path[0] = d2ast.MakeValueBox(d2ast.RawString(newPath, true)).StringBox() - } else { + var pre string + pathPart := newPath + + for i, r := range newPath { + if r != '.' && r != '/' { + pre = newPath[:i] + pathPart = newPath[i:] + break + } + } + + if pre == "" && len(newPath) > 0 && (newPath[0] == '.' || newPath[0] == '/') { + pre = newPath + pathPart = "" + } + + imp.Pre = pre + + if pathPart != "" { + if len(imp.Path) > 0 { + imp.Path[0] = d2ast.MakeValueBox(d2ast.RawString(pathPart, true)).StringBox() + } else { + imp.Path = []*d2ast.StringBox{ + d2ast.MakeValueBox(d2ast.RawString(pathPart, true)).StringBox(), + } + } + } else if len(imp.Path) == 0 { imp.Path = []*d2ast.StringBox{ - d2ast.MakeValueBox(d2ast.RawString(newPath, true)).StringBox(), + d2ast.MakeValueBox(d2ast.RawString("", true)).StringBox(), } } } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 275e34bab..c7e8db0ec 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -9819,6 +9819,50 @@ z exp: `x: @woof/bar/baz y: @woof/qux/quux z +`, + }, + { + name: "update_relative_import-1", + text: `x: @../meow +y +`, + path: "../meow", + newPath: go2.Pointer("../woof"), + exp: `x: @../woof +y +`, + }, + { + name: "update_relative_import-2", + text: `x: @../meow +y +`, + path: "../meow", + newPath: go2.Pointer("woof"), + exp: `x: @woof +y +`, + }, + { + name: "update_relative_import-3", + text: `x: @../meow +y +`, + path: "../meow", + newPath: go2.Pointer("../meow/woof"), + exp: `x: @../meow/woof +y +`, + }, + { + name: "update_relative_import-4", + text: `x: @../meow +y +`, + path: "../meow", + newPath: go2.Pointer("../g/woof"), + exp: `x: @../g/woof +y `, }, }