ep cli: Create entries from new files in edit history (#46196)

Agus Zubiaga created

Release Notes:

- N/A

Change summary

crates/edit_prediction/src/udiff.rs | 35 ++++++++++++++++++++++++------
1 file changed, 28 insertions(+), 7 deletions(-)

Detailed changes

crates/edit_prediction/src/udiff.rs 🔗

@@ -12,7 +12,7 @@ use collections::HashMap;
 use gpui::{AsyncApp, Entity};
 use language::{Anchor, Buffer, OffsetRangeExt as _, TextBufferSnapshot, text_diff};
 use postage::stream::Stream as _;
-use project::Project;
+use project::{Project, ProjectPath};
 use util::{paths::PathStyle, rel_path::RelPath};
 use worktree::Worktree;
 
@@ -56,14 +56,35 @@ pub async fn apply_diff(
                 hunk,
                 is_new_file,
             } => {
-                let project_path = project
-                    .update(cx, |project, cx| {
-                        project.find_project_path(path.as_ref(), cx)
-                    })?
-                    .context("no such path")?;
-
                 let buffer = match current_file {
                     None => {
+                        if is_new_file {
+                            let rel_path = RelPath::new(Path::new(path.as_ref()), PathStyle::Posix)
+                                .unwrap()
+                                .into_arc();
+
+                            project
+                                .update(cx, |project, cx| {
+                                    project.create_entry(
+                                        ProjectPath {
+                                            worktree_id: worktree.read(cx).id(),
+                                            path: rel_path,
+                                        },
+                                        false,
+                                        cx,
+                                    )
+                                })?
+                                .await?;
+                        }
+
+                        let project_path = project
+                            .update(cx, |project, cx| {
+                                project.find_project_path(path.as_ref(), cx)
+                            })?
+                            .with_context(|| {
+                                format!("Failed to find project path in diff: {}", path)
+                            })?;
+
                         let buffer = project
                             .update(cx, |project, cx| project.open_buffer(project_path, cx))?
                             .await?;