assistant2: Handle non-text files in context pickers (#22795)
We'll now show an error message if the user tries to add a directory that contains no text files or when they try to add a single non-text file. Release Notes: - N/A --------- Co-authored-by: Danilo <danilo@zed.dev>
This commit is contained in:
parent
52f29b4a1f
commit
36301442dd
2 changed files with 47 additions and 25 deletions
|
@ -231,27 +231,36 @@ impl PickerDelegate for DirectoryContextPickerDelegate {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let open_all_buffers_tasks = cx.background_executor().spawn(async move {
|
let buffers = futures::future::join_all(open_buffer_tasks).await;
|
||||||
let mut buffers = Vec::with_capacity(open_buffer_tasks.len());
|
|
||||||
|
|
||||||
for open_buffer_task in open_buffer_tasks {
|
|
||||||
let buffer = open_buffer_task.await?;
|
|
||||||
|
|
||||||
buffers.push(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
anyhow::Ok(buffers)
|
|
||||||
});
|
|
||||||
|
|
||||||
let buffers = open_all_buffers_tasks.await?;
|
|
||||||
|
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
let mut text = String::new();
|
let mut text = String::new();
|
||||||
|
|
||||||
for buffer in buffers {
|
let mut ok_count = 0;
|
||||||
|
|
||||||
|
for buffer in buffers.into_iter().flatten() {
|
||||||
let buffer = buffer.read(cx);
|
let buffer = buffer.read(cx);
|
||||||
let path = buffer.file().map_or(&path, |file| file.path());
|
let path = buffer.file().map_or(&path, |file| file.path());
|
||||||
push_fenced_codeblock(&path, buffer.text(), &mut text);
|
push_fenced_codeblock(&path, buffer.text(), &mut text);
|
||||||
|
ok_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok_count == 0 {
|
||||||
|
let Some(workspace) = workspace.upgrade() else {
|
||||||
|
return anyhow::Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
workspace.update(cx, |workspace, cx| {
|
||||||
|
workspace.show_error(
|
||||||
|
&anyhow::anyhow!(
|
||||||
|
"Could not read any text files from {}",
|
||||||
|
path.display()
|
||||||
|
),
|
||||||
|
cx,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return anyhow::Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
this.delegate
|
this.delegate
|
||||||
|
|
|
@ -239,21 +239,34 @@ impl PickerDelegate for FileContextPickerDelegate {
|
||||||
return anyhow::Ok(());
|
return anyhow::Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let buffer = open_buffer_task.await?;
|
let result = open_buffer_task.await;
|
||||||
|
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| match result {
|
||||||
this.delegate
|
Ok(buffer) => {
|
||||||
.context_store
|
this.delegate
|
||||||
.update(cx, |context_store, cx| {
|
.context_store
|
||||||
context_store.insert_file(buffer.read(cx));
|
.update(cx, |context_store, cx| {
|
||||||
})?;
|
context_store.insert_file(buffer.read(cx));
|
||||||
|
})?;
|
||||||
|
|
||||||
match confirm_behavior {
|
match confirm_behavior {
|
||||||
ConfirmBehavior::KeepOpen => {}
|
ConfirmBehavior::KeepOpen => {}
|
||||||
ConfirmBehavior::Close => this.delegate.dismissed(cx),
|
ConfirmBehavior::Close => this.delegate.dismissed(cx),
|
||||||
|
}
|
||||||
|
|
||||||
|
anyhow::Ok(())
|
||||||
}
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let Some(workspace) = workspace.upgrade() else {
|
||||||
|
return anyhow::Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
anyhow::Ok(())
|
workspace.update(cx, |workspace, cx| {
|
||||||
|
workspace.show_error(&err, cx);
|
||||||
|
});
|
||||||
|
|
||||||
|
anyhow::Ok(())
|
||||||
|
}
|
||||||
})??;
|
})??;
|
||||||
|
|
||||||
anyhow::Ok(())
|
anyhow::Ok(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue