Allow creating directories from the project panel

This commit is contained in:
Max Brunsfeld 2022-05-04 16:47:11 -07:00
parent a2c22a5e43
commit 40e0f10195
4 changed files with 166 additions and 57 deletions

View file

@ -690,33 +690,31 @@ impl Project {
.map(|worktree| worktree.read(cx).id())
}
pub fn create_file(
pub fn create_entry(
&mut self,
project_path: impl Into<ProjectPath>,
is_directory: bool,
cx: &mut ModelContext<Self>,
) -> Option<Task<Result<Entry>>> {
let project_path = project_path.into();
let worktree = self.worktree_for_id(project_path.worktree_id, cx)?;
if self.is_local() {
Some(worktree.update(cx, |worktree, cx| {
worktree.as_local_mut().unwrap().write_file(
project_path.path,
Default::default(),
cx,
)
worktree
.as_local_mut()
.unwrap()
.create_entry(project_path.path, is_directory, cx)
}))
} else {
let client = self.client.clone();
let project_id = self.remote_id().unwrap();
Some(cx.spawn_weak(|_, mut cx| async move {
let response = client
.request(proto::CreateProjectEntry {
worktree_id: project_path.worktree_id.to_proto(),
project_id,
path: project_path.path.as_os_str().as_bytes().to_vec(),
is_directory: false,
is_directory,
})
.await?;
let entry = response

View file

@ -686,32 +686,30 @@ impl LocalWorktree {
})
}
pub fn create_entry(
&self,
path: impl Into<Arc<Path>>,
is_dir: bool,
cx: &mut ModelContext<Worktree>,
) -> Task<Result<Entry>> {
self.write_entry_internal(
path,
if is_dir {
None
} else {
Some(Default::default())
},
cx,
)
}
pub fn write_file(
&self,
path: impl Into<Arc<Path>>,
text: Rope,
cx: &mut ModelContext<Worktree>,
) -> Task<Result<Entry>> {
let path = path.into();
let abs_path = self.absolutize(&path);
let save = cx.background().spawn({
let fs = self.fs.clone();
let abs_path = abs_path.clone();
async move { fs.save(&abs_path, &text).await }
});
cx.spawn(|this, mut cx| async move {
save.await?;
let entry = this
.update(&mut cx, |this, _| {
this.as_local_mut()
.unwrap()
.refresh_entry(path, abs_path, None)
})
.await?;
this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
Ok(entry)
})
self.write_entry_internal(path, Some(text), cx)
}
pub fn rename_entry(
@ -749,6 +747,40 @@ impl LocalWorktree {
}))
}
fn write_entry_internal(
&self,
path: impl Into<Arc<Path>>,
text_if_file: Option<Rope>,
cx: &mut ModelContext<Worktree>,
) -> Task<Result<Entry>> {
let path = path.into();
let abs_path = self.absolutize(&path);
let write = cx.background().spawn({
let fs = self.fs.clone();
let abs_path = abs_path.clone();
async move {
if let Some(text) = text_if_file {
fs.save(&abs_path, &text).await
} else {
fs.create_dir(&abs_path).await
}
}
});
cx.spawn(|this, mut cx| async move {
write.await?;
let entry = this
.update(&mut cx, |this, _| {
this.as_local_mut()
.unwrap()
.refresh_entry(path, abs_path, None)
})
.await?;
this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
Ok(entry)
})
}
fn refresh_entry(
&self,
path: Arc<Path>,