1use crate::{
2 example::{Example, ExampleBuffer, ExampleState},
3 git,
4 headless::EpAppState,
5 paths::WORKTREES_DIR,
6 progress::{InfoStyle, Progress, Step, StepProgress},
7};
8use anyhow::{Context as _, Result};
9use edit_prediction::EditPredictionStore;
10use edit_prediction::udiff::OpenedBuffers;
11use futures::AsyncWriteExt as _;
12use gpui::{AsyncApp, Entity};
13use language::{Anchor, Buffer, LanguageNotFound, ToOffset, ToPoint};
14use project::Project;
15use project::buffer_store::BufferStoreEvent;
16use std::{fs, path::PathBuf, sync::Arc};
17
18pub async fn run_load_project(
19 example: &mut Example,
20 app_state: Arc<EpAppState>,
21 mut cx: AsyncApp,
22) -> Result<()> {
23 if example.state.is_some() {
24 return Ok(());
25 }
26
27 let progress = Progress::global().start(Step::LoadProject, &example.spec.name);
28
29 let project = setup_project(example, &app_state, &progress, &mut cx).await?;
30
31 let _open_buffers = apply_edit_history(example, &project, &mut cx).await?;
32
33 let (buffer, cursor_position) = cursor_position(example, &project, &mut cx).await?;
34 let (example_buffer, language_name) = buffer.read_with(&cx, |buffer, _cx| {
35 let cursor_point = cursor_position.to_point(&buffer);
36 let language_name = buffer
37 .language()
38 .map(|l| l.name().to_string())
39 .unwrap_or_else(|| "Unknown".to_string());
40 (
41 ExampleBuffer {
42 content: buffer.text(),
43 cursor_row: cursor_point.row,
44 cursor_column: cursor_point.column,
45 cursor_offset: cursor_position.to_offset(&buffer),
46 },
47 language_name,
48 )
49 })?;
50
51 progress.set_info(language_name, InfoStyle::Normal);
52
53 example.buffer = Some(example_buffer);
54 example.state = Some(ExampleState {
55 buffer,
56 project,
57 cursor_position,
58 _open_buffers,
59 });
60 Ok(())
61}
62
63async fn cursor_position(
64 example: &Example,
65 project: &Entity<Project>,
66 cx: &mut AsyncApp,
67) -> Result<(Entity<Buffer>, Anchor)> {
68 let language_registry = project.read_with(cx, |project, _| project.languages().clone())?;
69 let result = language_registry
70 .load_language_for_file_path(&example.spec.cursor_path)
71 .await;
72
73 if let Err(error) = result
74 && !error.is::<LanguageNotFound>()
75 {
76 return Err(error);
77 }
78
79 let cursor_path = project
80 .read_with(cx, |project, cx| {
81 project.find_project_path(&example.spec.cursor_path, cx)
82 })?
83 .with_context(|| {
84 format!(
85 "failed to find cursor path {}",
86 example.spec.cursor_path.display()
87 )
88 })?;
89 let cursor_buffer = project
90 .update(cx, |project, cx| project.open_buffer(cursor_path, cx))?
91 .await?;
92
93 let (cursor_excerpt, cursor_offset_within_excerpt) = example.spec.cursor_excerpt()?;
94
95 let excerpt_offset = cursor_buffer.read_with(cx, |buffer, _cx| {
96 let text = buffer.text();
97
98 let mut matches = text.match_indices(&cursor_excerpt);
99 let (excerpt_offset, _) = matches.next().with_context(|| {
100 format!(
101 "\nExcerpt:\n\n{cursor_excerpt}\nBuffer text:\n{text}\n.Example: {}\nCursor excerpt did not exist in buffer.",
102 example.spec.name
103 )
104 })?;
105 anyhow::ensure!(
106 matches.next().is_none(),
107 "More than one cursor position match found for {}",
108 &example.spec.name
109 );
110 Ok(excerpt_offset)
111 })??;
112
113 let cursor_offset = excerpt_offset + cursor_offset_within_excerpt;
114 let cursor_anchor =
115 cursor_buffer.read_with(cx, |buffer, _| buffer.anchor_after(cursor_offset))?;
116
117 Ok((cursor_buffer, cursor_anchor))
118}
119
120async fn setup_project(
121 example: &mut Example,
122 app_state: &Arc<EpAppState>,
123 step_progress: &StepProgress,
124 cx: &mut AsyncApp,
125) -> Result<Entity<Project>> {
126 let ep_store = cx
127 .update(|cx| EditPredictionStore::try_global(cx))?
128 .context("Store should be initialized at init")?;
129
130 let worktree_path = setup_worktree(example, step_progress).await?;
131
132 if let Some(project) = app_state.project_cache.get(&example.spec.repository_url) {
133 ep_store.update(cx, |ep_store, _| {
134 ep_store.clear_history_for_project(&project);
135 })?;
136 let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
137 let buffers = buffer_store.read_with(cx, |buffer_store, _| {
138 buffer_store.buffers().collect::<Vec<_>>()
139 })?;
140 for buffer in buffers {
141 buffer
142 .update(cx, |buffer, cx| buffer.reload(cx))?
143 .await
144 .ok();
145 }
146 return Ok(project);
147 }
148
149 let project = cx.update(|cx| {
150 Project::local(
151 app_state.client.clone(),
152 app_state.node_runtime.clone(),
153 app_state.user_store.clone(),
154 app_state.languages.clone(),
155 app_state.fs.clone(),
156 None,
157 false,
158 cx,
159 )
160 })?;
161
162 project
163 .update(cx, |project, cx| {
164 project.disable_worktree_scanner(cx);
165 project.create_worktree(&worktree_path, true, cx)
166 })?
167 .await?;
168
169 app_state
170 .project_cache
171 .insert(example.spec.repository_url.clone(), project.clone());
172
173 let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
174 cx.subscribe(&buffer_store, {
175 let project = project.clone();
176 move |_, event, cx| match event {
177 BufferStoreEvent::BufferAdded(buffer) => {
178 ep_store.update(cx, |store, cx| store.register_buffer(&buffer, &project, cx));
179 }
180 _ => {}
181 }
182 })?
183 .detach();
184
185 Ok(project)
186}
187
188async fn setup_worktree(example: &Example, step_progress: &StepProgress) -> Result<PathBuf> {
189 let (repo_owner, repo_name) = example.repo_name().context("failed to get repo name")?;
190 let repo_dir = git::repo_path_for_url(&example.spec.repository_url)?;
191 let worktree_path = WORKTREES_DIR
192 .join(repo_owner.as_ref())
193 .join(repo_name.as_ref());
194 let repo_lock = git::lock_repo(&repo_dir).await;
195
196 if !repo_dir.is_dir() {
197 step_progress.set_substatus(format!("cloning {}", repo_name));
198 fs::create_dir_all(&repo_dir)?;
199 git::run_git(&repo_dir, &["init"]).await?;
200 git::run_git(
201 &repo_dir,
202 &["remote", "add", "origin", &example.spec.repository_url],
203 )
204 .await?;
205 }
206
207 // Resolve the example to a revision, fetching it if needed.
208 step_progress.set_substatus("fetching");
209 let revision = git::fetch_if_needed(&repo_dir, &example.spec.revision).await?;
210
211 // Create the worktree for this example if needed.
212 step_progress.set_substatus("preparing worktree");
213 if worktree_path.is_dir() {
214 git::run_git(&worktree_path, &["clean", "--force", "-d"]).await?;
215 git::run_git(&worktree_path, &["reset", "--hard", "HEAD"]).await?;
216 git::run_git(&worktree_path, &["checkout", revision.as_str()]).await?;
217 } else {
218 let worktree_path_string = worktree_path.to_string_lossy();
219 let branch_name = example.spec.filename();
220 git::run_git(
221 &repo_dir,
222 &["branch", "-f", &branch_name, revision.as_str()],
223 )
224 .await?;
225 git::run_git(
226 &repo_dir,
227 &["worktree", "add", "-f", &worktree_path_string, &branch_name],
228 )
229 .await?;
230 }
231 drop(repo_lock);
232
233 // Apply the uncommitted diff for this example.
234 if !example.spec.uncommitted_diff.is_empty() {
235 step_progress.set_substatus("applying diff");
236 let mut apply_process = smol::process::Command::new("git")
237 .current_dir(&worktree_path)
238 .args(&["apply", "-"])
239 .stdin(std::process::Stdio::piped())
240 .spawn()?;
241
242 let mut stdin = apply_process.stdin.take().context("Failed to get stdin")?;
243 stdin
244 .write_all(example.spec.uncommitted_diff.as_bytes())
245 .await?;
246 stdin.close().await?;
247 drop(stdin);
248
249 let apply_result = apply_process.output().await?;
250 anyhow::ensure!(
251 apply_result.status.success(),
252 "Failed to apply uncommitted diff patch with status: {}\nstderr:\n{}\nstdout:\n{}",
253 apply_result.status,
254 String::from_utf8_lossy(&apply_result.stderr),
255 String::from_utf8_lossy(&apply_result.stdout),
256 );
257 }
258
259 step_progress.clear_substatus();
260 Ok(worktree_path)
261}
262
263async fn apply_edit_history(
264 example: &Example,
265 project: &Entity<Project>,
266 cx: &mut AsyncApp,
267) -> Result<OpenedBuffers> {
268 edit_prediction::udiff::apply_diff(&example.spec.edit_history, project, cx).await
269}