1use futures::channel::oneshot;
2use fuzzy::PathMatch;
3use gpui::{HighlightStyle, Model, StyledText};
4use picker::{Picker, PickerDelegate};
5use project::{Entry, PathMatchCandidateSet, Project, ProjectPath, WorktreeId};
6use std::{
7 path::PathBuf,
8 sync::{
9 atomic::{self, AtomicBool},
10 Arc,
11 },
12};
13use ui::{highlight_ranges, prelude::*, LabelLike, ListItemSpacing};
14use ui::{ListItem, ViewContext};
15use util::ResultExt;
16use workspace::Workspace;
17
18pub(crate) struct NewPathPrompt;
19
20#[derive(Debug, Clone)]
21struct Match {
22 path_match: Option<PathMatch>,
23 suffix: Option<String>,
24}
25
26impl Match {
27 fn entry<'a>(&'a self, project: &'a Project, cx: &'a WindowContext) -> Option<&'a Entry> {
28 if let Some(suffix) = &self.suffix {
29 let (worktree, path) = if let Some(path_match) = &self.path_match {
30 (
31 project.worktree_for_id(WorktreeId::from_usize(path_match.worktree_id), cx),
32 path_match.path.join(suffix),
33 )
34 } else {
35 (project.worktrees(cx).next(), PathBuf::from(suffix))
36 };
37
38 worktree.and_then(|worktree| worktree.read(cx).entry_for_path(path))
39 } else if let Some(path_match) = &self.path_match {
40 let worktree =
41 project.worktree_for_id(WorktreeId::from_usize(path_match.worktree_id), cx)?;
42 worktree.read(cx).entry_for_path(path_match.path.as_ref())
43 } else {
44 None
45 }
46 }
47
48 fn is_dir(&self, project: &Project, cx: &WindowContext) -> bool {
49 self.entry(project, cx).is_some_and(|e| e.is_dir())
50 || self.suffix.as_ref().is_some_and(|s| s.ends_with('/'))
51 }
52
53 fn relative_path(&self) -> String {
54 if let Some(path_match) = &self.path_match {
55 if let Some(suffix) = &self.suffix {
56 format!(
57 "{}/{}",
58 path_match.path.to_string_lossy(),
59 suffix.trim_end_matches('/')
60 )
61 } else {
62 path_match.path.to_string_lossy().to_string()
63 }
64 } else if let Some(suffix) = &self.suffix {
65 suffix.trim_end_matches('/').to_string()
66 } else {
67 "".to_string()
68 }
69 }
70
71 fn project_path(&self, project: &Project, cx: &WindowContext) -> Option<ProjectPath> {
72 let worktree_id = if let Some(path_match) = &self.path_match {
73 WorktreeId::from_usize(path_match.worktree_id)
74 } else {
75 project.worktrees(cx).next()?.read(cx).id()
76 };
77
78 let path = PathBuf::from(self.relative_path());
79
80 Some(ProjectPath {
81 worktree_id,
82 path: Arc::from(path),
83 })
84 }
85
86 fn existing_prefix(&self, project: &Project, cx: &WindowContext) -> Option<PathBuf> {
87 let worktree = project.worktrees(cx).next()?.read(cx);
88 let mut prefix = PathBuf::new();
89 let parts = self.suffix.as_ref()?.split('/');
90 for part in parts {
91 if worktree.entry_for_path(prefix.join(&part)).is_none() {
92 return Some(prefix);
93 }
94 prefix = prefix.join(part);
95 }
96
97 None
98 }
99
100 fn styled_text(&self, project: &Project, cx: &WindowContext) -> StyledText {
101 let mut text = "./".to_string();
102 let mut highlights = Vec::new();
103 let mut offset = text.as_bytes().len();
104
105 let separator = '/';
106 let dir_indicator = "[…]";
107
108 if let Some(path_match) = &self.path_match {
109 text.push_str(&path_match.path.to_string_lossy());
110 let mut whole_path = PathBuf::from(path_match.path_prefix.to_string());
111 whole_path = whole_path.join(path_match.path.clone());
112 for (range, style) in highlight_ranges(
113 &whole_path.to_string_lossy(),
114 &path_match.positions,
115 gpui::HighlightStyle::color(Color::Accent.color(cx)),
116 ) {
117 highlights.push((range.start + offset..range.end + offset, style))
118 }
119 text.push(separator);
120 offset = text.as_bytes().len();
121
122 if let Some(suffix) = &self.suffix {
123 text.push_str(suffix);
124 let entry = self.entry(project, cx);
125 let color = if let Some(entry) = entry {
126 if entry.is_dir() {
127 Color::Accent
128 } else {
129 Color::Conflict
130 }
131 } else {
132 Color::Created
133 };
134 highlights.push((
135 offset..offset + suffix.as_bytes().len(),
136 HighlightStyle::color(color.color(cx)),
137 ));
138 offset += suffix.as_bytes().len();
139 if entry.is_some_and(|e| e.is_dir()) {
140 text.push(separator);
141 offset += separator.len_utf8();
142
143 text.push_str(dir_indicator);
144 highlights.push((
145 offset..offset + dir_indicator.bytes().len(),
146 HighlightStyle::color(Color::Muted.color(cx)),
147 ));
148 }
149 } else {
150 text.push_str(dir_indicator);
151 highlights.push((
152 offset..offset + dir_indicator.bytes().len(),
153 HighlightStyle::color(Color::Muted.color(cx)),
154 ))
155 }
156 } else if let Some(suffix) = &self.suffix {
157 text.push_str(suffix);
158 let existing_prefix_len = self
159 .existing_prefix(project, cx)
160 .map(|prefix| prefix.to_string_lossy().as_bytes().len())
161 .unwrap_or(0);
162
163 if existing_prefix_len > 0 {
164 highlights.push((
165 offset..offset + existing_prefix_len,
166 HighlightStyle::color(Color::Accent.color(cx)),
167 ));
168 }
169 highlights.push((
170 offset + existing_prefix_len..offset + suffix.as_bytes().len(),
171 HighlightStyle::color(if self.entry(project, cx).is_some() {
172 Color::Conflict.color(cx)
173 } else {
174 Color::Created.color(cx)
175 }),
176 ));
177 offset += suffix.as_bytes().len();
178 if suffix.ends_with('/') {
179 text.push_str(dir_indicator);
180 highlights.push((
181 offset..offset + dir_indicator.bytes().len(),
182 HighlightStyle::color(Color::Muted.color(cx)),
183 ));
184 }
185 }
186
187 StyledText::new(text).with_highlights(&cx.text_style().clone(), highlights)
188 }
189}
190
191pub struct NewPathDelegate {
192 project: Model<Project>,
193 tx: Option<oneshot::Sender<Option<ProjectPath>>>,
194 selected_index: usize,
195 matches: Vec<Match>,
196 last_selected_dir: Option<String>,
197 cancel_flag: Arc<AtomicBool>,
198 should_dismiss: bool,
199}
200
201impl NewPathPrompt {
202 pub(crate) fn register(workspace: &mut Workspace, _cx: &mut ViewContext<Workspace>) {
203 workspace.set_prompt_for_new_path(Box::new(|workspace, cx| {
204 let (tx, rx) = futures::channel::oneshot::channel();
205 Self::prompt_for_new_path(workspace, tx, cx);
206 rx
207 }));
208 }
209
210 fn prompt_for_new_path(
211 workspace: &mut Workspace,
212 tx: oneshot::Sender<Option<ProjectPath>>,
213 cx: &mut ViewContext<Workspace>,
214 ) {
215 let project = workspace.project().clone();
216 workspace.toggle_modal(cx, |cx| {
217 let delegate = NewPathDelegate {
218 project,
219 tx: Some(tx),
220 selected_index: 0,
221 matches: vec![],
222 cancel_flag: Arc::new(AtomicBool::new(false)),
223 last_selected_dir: None,
224 should_dismiss: true,
225 };
226
227 Picker::uniform_list(delegate, cx).width(rems(34.))
228 });
229 }
230}
231
232impl PickerDelegate for NewPathDelegate {
233 type ListItem = ui::ListItem;
234
235 fn match_count(&self) -> usize {
236 self.matches.len()
237 }
238
239 fn selected_index(&self) -> usize {
240 self.selected_index
241 }
242
243 fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<picker::Picker<Self>>) {
244 self.selected_index = ix;
245 cx.notify();
246 }
247
248 fn update_matches(
249 &mut self,
250 query: String,
251 cx: &mut ViewContext<picker::Picker<Self>>,
252 ) -> gpui::Task<()> {
253 let query = query
254 .trim()
255 .trim_start_matches("./")
256 .trim_start_matches('/');
257 let (dir, suffix) = if let Some(index) = query.rfind('/') {
258 let suffix = if index + 1 < query.len() {
259 Some(query[index + 1..].to_string())
260 } else {
261 None
262 };
263 (query[0..index].to_string(), suffix)
264 } else {
265 (query.to_string(), None)
266 };
267
268 let worktrees = self
269 .project
270 .read(cx)
271 .visible_worktrees(cx)
272 .collect::<Vec<_>>();
273 let include_root_name = worktrees.len() > 1;
274 let candidate_sets = worktrees
275 .into_iter()
276 .map(|worktree| {
277 let worktree = worktree.read(cx);
278 PathMatchCandidateSet {
279 snapshot: worktree.snapshot(),
280 include_ignored: worktree
281 .root_entry()
282 .map_or(false, |entry| entry.is_ignored),
283 include_root_name,
284 candidates: project::Candidates::Directories,
285 }
286 })
287 .collect::<Vec<_>>();
288
289 self.cancel_flag.store(true, atomic::Ordering::Relaxed);
290 self.cancel_flag = Arc::new(AtomicBool::new(false));
291
292 let cancel_flag = self.cancel_flag.clone();
293 let query = query.to_string();
294 let prefix = dir.clone();
295 cx.spawn(|picker, mut cx| async move {
296 let matches = fuzzy::match_path_sets(
297 candidate_sets.as_slice(),
298 &dir,
299 None,
300 false,
301 100,
302 &cancel_flag,
303 cx.background_executor().clone(),
304 )
305 .await;
306 let did_cancel = cancel_flag.load(atomic::Ordering::Relaxed);
307 if did_cancel {
308 return;
309 }
310 picker
311 .update(&mut cx, |picker, cx| {
312 picker
313 .delegate
314 .set_search_matches(query, prefix, suffix, matches, cx)
315 })
316 .log_err();
317 })
318 }
319
320 fn confirm_update_query(&mut self, cx: &mut ViewContext<Picker<Self>>) -> Option<String> {
321 let m = self.matches.get(self.selected_index)?;
322 if m.is_dir(self.project.read(cx), cx) {
323 let path = m.relative_path();
324 self.last_selected_dir = Some(path.clone());
325 Some(format!("{}/", path))
326 } else {
327 None
328 }
329 }
330
331 fn confirm(&mut self, _: bool, cx: &mut ViewContext<picker::Picker<Self>>) {
332 let Some(m) = self.matches.get(self.selected_index) else {
333 return;
334 };
335
336 let exists = m.entry(self.project.read(cx), cx).is_some();
337 if exists {
338 self.should_dismiss = false;
339 let answer = cx.prompt(
340 gpui::PromptLevel::Critical,
341 &format!("{} already exists. Do you want to replace it?", m.relative_path()),
342 Some(
343 "A file or folder with the same name already exists. Replacing it will overwrite its current contents.",
344 ),
345 &["Replace", "Cancel"],
346 );
347 let m = m.clone();
348 cx.spawn(|picker, mut cx| async move {
349 let answer = answer.await.ok();
350 picker
351 .update(&mut cx, |picker, cx| {
352 picker.delegate.should_dismiss = true;
353 if answer != Some(0) {
354 return;
355 }
356 if let Some(path) = m.project_path(picker.delegate.project.read(cx), cx) {
357 if let Some(tx) = picker.delegate.tx.take() {
358 tx.send(Some(path)).ok();
359 }
360 }
361 cx.emit(gpui::DismissEvent);
362 })
363 .ok();
364 })
365 .detach();
366 return;
367 }
368
369 if let Some(path) = m.project_path(self.project.read(cx), cx) {
370 if let Some(tx) = self.tx.take() {
371 tx.send(Some(path)).ok();
372 }
373 }
374 cx.emit(gpui::DismissEvent);
375 }
376
377 fn should_dismiss(&self) -> bool {
378 self.should_dismiss
379 }
380
381 fn dismissed(&mut self, cx: &mut ViewContext<picker::Picker<Self>>) {
382 if let Some(tx) = self.tx.take() {
383 tx.send(None).ok();
384 }
385 cx.emit(gpui::DismissEvent)
386 }
387
388 fn render_match(
389 &self,
390 ix: usize,
391 selected: bool,
392 cx: &mut ViewContext<picker::Picker<Self>>,
393 ) -> Option<Self::ListItem> {
394 let m = self.matches.get(ix)?;
395
396 Some(
397 ListItem::new(ix)
398 .spacing(ListItemSpacing::Sparse)
399 .inset(true)
400 .selected(selected)
401 .child(LabelLike::new().child(m.styled_text(self.project.read(cx), cx))),
402 )
403 }
404
405 fn no_matches_text(&self, _cx: &mut WindowContext) -> SharedString {
406 "Type a path...".into()
407 }
408
409 fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
410 Arc::from("[directory/]filename.ext")
411 }
412}
413
414impl NewPathDelegate {
415 fn set_search_matches(
416 &mut self,
417 query: String,
418 prefix: String,
419 suffix: Option<String>,
420 matches: Vec<PathMatch>,
421 cx: &mut ViewContext<Picker<Self>>,
422 ) {
423 cx.notify();
424 if query.is_empty() {
425 self.matches = vec![];
426 return;
427 }
428
429 let mut directory_exists = false;
430
431 self.matches = matches
432 .into_iter()
433 .map(|m| {
434 if m.path.as_ref().to_string_lossy() == prefix {
435 directory_exists = true
436 }
437 Match {
438 path_match: Some(m),
439 suffix: suffix.clone(),
440 }
441 })
442 .collect();
443
444 if !directory_exists {
445 if suffix.is_none()
446 || self
447 .last_selected_dir
448 .as_ref()
449 .is_some_and(|d| query.starts_with(d))
450 {
451 self.matches.insert(
452 0,
453 Match {
454 path_match: None,
455 suffix: Some(query.clone()),
456 },
457 )
458 } else {
459 self.matches.push(Match {
460 path_match: None,
461 suffix: Some(query.clone()),
462 })
463 }
464 }
465 }
466}