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
90 let cursor_buffer = project
91 .update(cx, |project, cx| project.open_buffer(cursor_path, cx))?
92 .await?;
93
94 let (cursor_excerpt, cursor_offset_within_excerpt) = example.spec.cursor_excerpt()?;
95
96 let excerpt_offset = cursor_buffer.read_with(cx, |buffer, _cx| {
97 let text = buffer.text();
98
99 let mut matches = text.match_indices(&cursor_excerpt);
100 let (excerpt_offset, _) = matches.next().with_context(|| {
101 format!(
102 "\nExcerpt:\n\n{cursor_excerpt}\nBuffer text:\n{text}\n.Example: {}\nCursor excerpt did not exist in buffer.",
103 example.spec.name
104 )
105 })?;
106 anyhow::ensure!(
107 matches.next().is_none(),
108 "More than one cursor position match found for {}",
109 &example.spec.name
110 );
111 Ok(excerpt_offset)
112 })??;
113
114 let cursor_offset = excerpt_offset + cursor_offset_within_excerpt;
115 let cursor_anchor =
116 cursor_buffer.read_with(cx, |buffer, _| buffer.anchor_after(cursor_offset))?;
117
118 Ok((cursor_buffer, cursor_anchor))
119}
120
121async fn setup_project(
122 example: &mut Example,
123 app_state: &Arc<EpAppState>,
124 step_progress: &StepProgress,
125 cx: &mut AsyncApp,
126) -> Result<Entity<Project>> {
127 let ep_store = cx
128 .update(|cx| EditPredictionStore::try_global(cx))?
129 .context("Store should be initialized at init")?;
130
131 let worktree_path = setup_worktree(example, step_progress).await?;
132
133 if let Some(project) = app_state.project_cache.get(&example.spec.repository_url) {
134 ep_store.update(cx, |ep_store, _| {
135 ep_store.clear_history_for_project(&project);
136 })?;
137 let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
138 let buffers = buffer_store.read_with(cx, |buffer_store, _| {
139 buffer_store.buffers().collect::<Vec<_>>()
140 })?;
141 for buffer in buffers {
142 buffer
143 .update(cx, |buffer, cx| buffer.reload(cx))?
144 .await
145 .ok();
146 }
147 return Ok(project);
148 }
149
150 let project = cx.update(|cx| {
151 Project::local(
152 app_state.client.clone(),
153 app_state.node_runtime.clone(),
154 app_state.user_store.clone(),
155 app_state.languages.clone(),
156 app_state.fs.clone(),
157 None,
158 false,
159 cx,
160 )
161 })?;
162
163 project
164 .update(cx, |project, cx| {
165 project.disable_worktree_scanner(cx);
166 project.create_worktree(&worktree_path, true, cx)
167 })?
168 .await?;
169
170 app_state
171 .project_cache
172 .insert(example.spec.repository_url.clone(), project.clone());
173
174 let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
175 cx.subscribe(&buffer_store, {
176 let project = project.clone();
177 move |_, event, cx| match event {
178 BufferStoreEvent::BufferAdded(buffer) => {
179 ep_store.update(cx, |store, cx| store.register_buffer(&buffer, &project, cx));
180 }
181 _ => {}
182 }
183 })?
184 .detach();
185
186 Ok(project)
187}
188
189async fn setup_worktree(example: &Example, step_progress: &StepProgress) -> Result<PathBuf> {
190 let (repo_owner, repo_name) = example.repo_name().context("failed to get repo name")?;
191 let repo_dir = git::repo_path_for_url(&example.spec.repository_url)?;
192 let worktree_path = WORKTREES_DIR
193 .join(repo_owner.as_ref())
194 .join(repo_name.as_ref());
195 let repo_lock = git::lock_repo(&repo_dir).await;
196
197 if !repo_dir.is_dir() {
198 step_progress.set_substatus(format!("cloning {}", repo_name));
199 fs::create_dir_all(&repo_dir)?;
200 git::run_git(&repo_dir, &["init"]).await?;
201 git::run_git(
202 &repo_dir,
203 &["remote", "add", "origin", &example.spec.repository_url],
204 )
205 .await?;
206 }
207
208 // Resolve the example to a revision, fetching it if needed.
209 step_progress.set_substatus("fetching");
210 let revision = git::fetch_if_needed(&repo_dir, &example.spec.revision).await?;
211
212 // Create the worktree for this example if needed.
213 step_progress.set_substatus("preparing worktree");
214 if worktree_path.is_dir() {
215 git::run_git(&worktree_path, &["clean", "--force", "-d"]).await?;
216 git::run_git(&worktree_path, &["reset", "--hard", "HEAD"]).await?;
217 git::run_git(&worktree_path, &["checkout", revision.as_str()]).await?;
218 } else {
219 let worktree_path_string = worktree_path.to_string_lossy();
220 let branch_name = example.spec.filename();
221 git::run_git(
222 &repo_dir,
223 &["branch", "-f", &branch_name, revision.as_str()],
224 )
225 .await?;
226 git::run_git(
227 &repo_dir,
228 &["worktree", "add", "-f", &worktree_path_string, &branch_name],
229 )
230 .await?;
231 }
232 drop(repo_lock);
233
234 // Apply the uncommitted diff for this example.
235 if !example.spec.uncommitted_diff.is_empty() {
236 step_progress.set_substatus("applying diff");
237 let mut apply_process = smol::process::Command::new("git")
238 .current_dir(&worktree_path)
239 .args(&["apply", "-"])
240 .stdin(std::process::Stdio::piped())
241 .spawn()?;
242
243 let mut stdin = apply_process.stdin.take().context("Failed to get stdin")?;
244 stdin
245 .write_all(example.spec.uncommitted_diff.as_bytes())
246 .await?;
247 stdin.close().await?;
248 drop(stdin);
249
250 let apply_result = apply_process.output().await?;
251 anyhow::ensure!(
252 apply_result.status.success(),
253 "Failed to apply uncommitted diff patch with status: {}\nstderr:\n{}\nstdout:\n{}",
254 apply_result.status,
255 String::from_utf8_lossy(&apply_result.stderr),
256 String::from_utf8_lossy(&apply_result.stdout),
257 );
258 }
259
260 step_progress.clear_substatus();
261 Ok(worktree_path)
262}
263
264async fn apply_edit_history(
265 example: &Example,
266 project: &Entity<Project>,
267 cx: &mut AsyncApp,
268) -> Result<OpenedBuffers> {
269 edit_prediction::udiff::apply_diff(&example.spec.edit_history, project, cx).await
270}