Improve the opening error

This commit is contained in:
Kirill Bulatov 2025-08-25 23:29:08 +03:00
parent 54bf4a0e26
commit 169ba2b20f
4 changed files with 48 additions and 42 deletions

View file

@ -1404,7 +1404,7 @@ impl ProjectItem for Editor {
}
fn for_broken_project_item(
abs_path: PathBuf,
abs_path: &Path,
is_local: bool,
e: &anyhow::Error,
window: &mut Window,

View file

@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::Arc};
use std::{path::Path, sync::Arc};
use gpui::{EventEmitter, FocusHandle, Focusable};
use ui::{
@ -12,7 +12,7 @@ use crate::Item;
/// A view to display when a certain buffer fails to open.
pub struct InvalidBufferView {
/// Which path was attempted to open.
pub abs_path: Arc<PathBuf>,
pub abs_path: Arc<Path>,
/// An error message, happened when opening the buffer.
pub error: SharedString,
is_local: bool,
@ -21,7 +21,7 @@ pub struct InvalidBufferView {
impl InvalidBufferView {
pub fn new(
abs_path: PathBuf,
abs_path: &Path,
is_local: bool,
e: &anyhow::Error,
_: &mut Window,
@ -29,7 +29,7 @@ impl InvalidBufferView {
) -> Self {
Self {
is_local,
abs_path: Arc::new(abs_path),
abs_path: Arc::from(abs_path),
error: format!("{e}").into(),
focus_handle: cx.focus_handle(),
}
@ -43,7 +43,7 @@ impl Item for InvalidBufferView {
// Ensure we always render at least the filename.
detail += 1;
let path = self.abs_path.as_path();
let path = self.abs_path.as_ref();
let mut prefix = path;
while detail > 0 {

View file

@ -23,7 +23,7 @@ use std::{
any::{Any, TypeId},
cell::RefCell,
ops::Range,
path::PathBuf,
path::Path,
rc::Rc,
sync::Arc,
time::Duration,
@ -1168,7 +1168,7 @@ pub trait ProjectItem: Item {
/// with the error from that failure as an argument.
/// Allows to open an item that can gracefully display and handle errors.
fn for_broken_project_item(
_abs_path: PathBuf,
_abs_path: &Path,
_is_local: bool,
_e: &anyhow::Error,
_window: &mut Window,

View file

@ -613,53 +613,59 @@ impl ProjectItemRegistry {
self.build_project_item_for_path_fns
.push(|project, project_path, window, cx| {
let project_path = project_path.clone();
let file_abs_path = project
let is_file = project
.read(cx)
.entry_for_path(&project_path, cx)
.is_some_and(|entry| entry.is_file())
.then(|| project.read(cx).absolute_path(&project_path, cx))
.flatten();
.is_some_and(|entry| entry.is_file());
let entry_abs_path = project.read(cx).absolute_path(&project_path, cx);
let is_local = project.read(cx).is_local();
let project_item =
<T::Item as project::ProjectItem>::try_open(project, &project_path, cx)?;
let project = project.clone();
Some(window.spawn(cx, async move |cx| match project_item.await {
Ok(project_item) => {
let project_item = project_item;
let project_entry_id: Option<ProjectEntryId> =
project_item.read_with(cx, project::ProjectItem::entry_id)?;
let build_workspace_item = Box::new(
|pane: &mut Pane, window: &mut Window, cx: &mut Context<Pane>| {
Box::new(cx.new(|cx| {
T::for_project_item(
project,
Some(pane),
project_item,
window,
cx,
)
})) as Box<dyn ItemHandle>
},
) as Box<_>;
Ok((project_entry_id, build_workspace_item))
}
Err(e) => match file_abs_path {
Some(abs_path) => match cx.update(|window, cx| {
T::for_broken_project_item(abs_path, is_local, &e, window, cx)
})? {
Some(broken_project_item_view) => {
let build_workspace_item = Box::new(
Some(window.spawn(cx, async move |cx| {
match project_item.await.with_context(|| {
format!(
"opening project path {:?}",
entry_abs_path.as_deref().unwrap_or(&project_path.path)
)
}) {
Ok(project_item) => {
let project_item = project_item;
let project_entry_id: Option<ProjectEntryId> =
project_item.read_with(cx, project::ProjectItem::entry_id)?;
let build_workspace_item = Box::new(
|pane: &mut Pane, window: &mut Window, cx: &mut Context<Pane>| {
Box::new(cx.new(|cx| {
T::for_project_item(
project,
Some(pane),
project_item,
window,
cx,
)
})) as Box<dyn ItemHandle>
},
) as Box<_>;
Ok((project_entry_id, build_workspace_item))
}
Err(e) => match entry_abs_path.as_deref().filter(|_| is_file) {
Some(abs_path) => match cx.update(|window, cx| {
T::for_broken_project_item(abs_path, is_local, &e, window, cx)
})? {
Some(broken_project_item_view) => {
let build_workspace_item = Box::new(
move |_: &mut Pane, _: &mut Window, cx: &mut Context<Pane>| {
cx.new(|_| broken_project_item_view).boxed_clone()
},
)
as Box<_>;
Ok((None, build_workspace_item))
}
Ok((None, build_workspace_item))
}
None => Err(e)?,
},
None => Err(e)?,
},
None => Err(e)?,
},
}
}))
});
}