Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/assistant_text_thread/src/text_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,7 @@ impl TextThread {
RenameOptions {
overwrite: true,
ignore_if_exists: true,
create_parents: false,
},
)
.await?;
Expand Down
73 changes: 73 additions & 0 deletions crates/fs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ pub struct CopyOptions {
pub struct RenameOptions {
pub overwrite: bool,
pub ignore_if_exists: bool,
/// Whether to create parent directories if they do not exist.
pub create_parents: bool,
}

#[derive(Copy, Clone, Default)]
Expand Down Expand Up @@ -577,6 +579,12 @@ impl Fs for RealFs {
}
}

if options.create_parents {
if let Some(parent) = target.parent() {
self.create_dir(parent).await?;
}
}

smol::fs::rename(source, target).await?;
Ok(())
}
Expand Down Expand Up @@ -2348,6 +2356,12 @@ impl Fs for FakeFs {
let old_path = normalize_path(old_path);
let new_path = normalize_path(new_path);

if options.create_parents {
if let Some(parent) = new_path.parent() {
self.create_dir(parent).await?;
}
}

let mut state = self.state.lock();
let moved_entry = state.write_path(&old_path, |e| {
if let btree_map::Entry::Occupied(e) = e {
Expand Down Expand Up @@ -3385,4 +3399,63 @@ mod tests {
let content = std::fs::read_to_string(&file_to_be_replaced).unwrap();
assert_eq!(content, "Hello");
}

#[gpui::test]
async fn test_rename(executor: BackgroundExecutor) {
let fs = FakeFs::new(executor.clone());
fs.insert_tree(
path!("/root"),
json!({
"src": {
"file_a.txt": "content a",
"file_b.txt": "content b"
}
}),
)
.await;

fs.rename(
Path::new(path!("/root/src/file_a.txt")),
Path::new(path!("/root/src/new/renamed_a.txt")),
RenameOptions {
create_parents: true,
..Default::default()
},
)
.await
.unwrap();

// Assert that the `file_a.txt` file was being renamed and moved to a
// different directory that did not exist before.
assert_eq!(
fs.files(),
vec![
PathBuf::from(path!("/root/src/file_b.txt")),
PathBuf::from(path!("/root/src/new/renamed_a.txt")),
]
);

let result = fs
.rename(
Path::new(path!("/root/src/file_b.txt")),
Path::new(path!("/root/src/old/renamed_b.txt")),
RenameOptions {
create_parents: false,
..Default::default()
},
)
.await;

// Assert that the `file_b.txt` file was not renamed nor moved, as
// `create_parents` was set to `false`.
// different directory that did not exist before.
assert!(result.is_err());
assert_eq!(
fs.files(),
vec![
PathBuf::from(path!("/root/src/file_b.txt")),
PathBuf::from(path!("/root/src/new/renamed_a.txt")),
]
);
}
}
1 change: 1 addition & 0 deletions crates/project/src/agent_server_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,7 @@ async fn download_latest_version(
RenameOptions {
ignore_if_exists: true,
overwrite: true,
create_parents: false,
},
)
.await?;
Expand Down
28 changes: 17 additions & 11 deletions crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3021,17 +3021,23 @@ impl LocalLspStore {
.new_uri
.to_file_path()
.map_err(|()| anyhow!("can't convert URI to path"))?;
fs.rename(
&source_abs_path,
&target_abs_path,
op.options
.map(|options| fs::RenameOptions {
overwrite: options.overwrite.unwrap_or(false),
ignore_if_exists: options.ignore_if_exists.unwrap_or(false),
})
.unwrap_or_default(),
)
.await?;

let options = fs::RenameOptions {
overwrite: op
.options
.as_ref()
.and_then(|options| options.overwrite)
.unwrap_or(false),
ignore_if_exists: op
.options
.as_ref()
.and_then(|options| options.ignore_if_exists)
.unwrap_or(false),
create_parents: true,
};

fs.rename(&source_abs_path, &target_abs_path, options)
.await?;
}

lsp::DocumentChangeOperation::Op(lsp::ResourceOp::Delete(op)) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/worktree/src/worktree_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ async fn test_renaming_case_only(cx: &mut TestAppContext) {
fs::RenameOptions {
overwrite: true,
ignore_if_exists: true,
create_parents: false,
},
)
.await
Expand Down Expand Up @@ -1986,6 +1987,7 @@ async fn randomly_mutate_fs(
fs::RenameOptions {
overwrite: true,
ignore_if_exists: true,
create_parents: false,
},
)
.await
Expand Down
Loading