1use editor::{Editor, EditorSettings};
2use fuzzy::PathMatch;
3use gpui::{
4 action,
5 elements::*,
6 keymap::{
7 self,
8 menu::{SelectNext, SelectPrev},
9 Binding,
10 },
11 AppContext, Axis, Entity, ModelHandle, MutableAppContext, RenderContext, Task, View,
12 ViewContext, ViewHandle, WeakViewHandle,
13};
14use postage::watch;
15use project::{Project, ProjectPath, WorktreeId};
16use std::{
17 cmp,
18 path::Path,
19 sync::{
20 atomic::{self, AtomicBool},
21 Arc,
22 },
23};
24use util::post_inc;
25use workspace::{Settings, Workspace};
26
27pub struct FileFinder {
28 handle: WeakViewHandle<Self>,
29 settings: watch::Receiver<Settings>,
30 project: ModelHandle<Project>,
31 query_editor: ViewHandle<Editor>,
32 search_count: usize,
33 latest_search_id: usize,
34 latest_search_did_cancel: bool,
35 latest_search_query: String,
36 matches: Vec<PathMatch>,
37 selected: Option<(usize, Arc<Path>)>,
38 cancel_flag: Arc<AtomicBool>,
39 list_state: UniformListState,
40}
41
42action!(Toggle);
43action!(Confirm);
44action!(Select, ProjectPath);
45
46pub fn init(cx: &mut MutableAppContext) {
47 cx.add_action(FileFinder::toggle);
48 cx.add_action(FileFinder::confirm);
49 cx.add_action(FileFinder::select);
50 cx.add_action(FileFinder::select_prev);
51 cx.add_action(FileFinder::select_next);
52
53 cx.add_bindings(vec![
54 Binding::new("cmd-p", Toggle, None),
55 Binding::new("escape", Toggle, Some("FileFinder")),
56 Binding::new("enter", Confirm, Some("FileFinder")),
57 ]);
58}
59
60pub enum Event {
61 Selected(ProjectPath),
62 Dismissed,
63}
64
65impl Entity for FileFinder {
66 type Event = Event;
67}
68
69impl View for FileFinder {
70 fn ui_name() -> &'static str {
71 "FileFinder"
72 }
73
74 fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
75 let settings = self.settings.borrow();
76
77 Align::new(
78 ConstrainedBox::new(
79 Container::new(
80 Flex::new(Axis::Vertical)
81 .with_child(
82 Container::new(ChildView::new(self.query_editor.id()).boxed())
83 .with_style(settings.theme.selector.input_editor.container)
84 .boxed(),
85 )
86 .with_child(Flexible::new(1.0, false, self.render_matches()).boxed())
87 .boxed(),
88 )
89 .with_style(settings.theme.selector.container)
90 .boxed(),
91 )
92 .with_max_width(500.0)
93 .with_max_height(420.0)
94 .boxed(),
95 )
96 .top()
97 .named("file finder")
98 }
99
100 fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
101 cx.focus(&self.query_editor);
102 }
103
104 fn keymap_context(&self, _: &AppContext) -> keymap::Context {
105 let mut cx = Self::default_keymap_context();
106 cx.set.insert("menu".into());
107 cx
108 }
109}
110
111impl FileFinder {
112 fn render_matches(&self) -> ElementBox {
113 if self.matches.is_empty() {
114 let settings = self.settings.borrow();
115 return Container::new(
116 Label::new(
117 "No matches".into(),
118 settings.theme.selector.empty.label.clone(),
119 )
120 .boxed(),
121 )
122 .with_style(settings.theme.selector.empty.container)
123 .named("empty matches");
124 }
125
126 let handle = self.handle.clone();
127 let list = UniformList::new(
128 self.list_state.clone(),
129 self.matches.len(),
130 move |mut range, items, cx| {
131 let cx = cx.as_ref();
132 let finder = handle.upgrade(cx).unwrap();
133 let finder = finder.read(cx);
134 let start = range.start;
135 range.end = cmp::min(range.end, finder.matches.len());
136 items.extend(
137 finder.matches[range]
138 .iter()
139 .enumerate()
140 .map(move |(i, path_match)| finder.render_match(path_match, start + i)),
141 );
142 },
143 );
144
145 Container::new(list.boxed())
146 .with_margin_top(6.0)
147 .named("matches")
148 }
149
150 fn render_match(&self, path_match: &PathMatch, index: usize) -> ElementBox {
151 let selected_index = self.selected_index();
152 let settings = self.settings.borrow();
153 let style = if index == selected_index {
154 &settings.theme.selector.active_item
155 } else {
156 &settings.theme.selector.item
157 };
158 let (file_name, file_name_positions, full_path, full_path_positions) =
159 self.labels_for_match(path_match);
160 let container = Container::new(
161 Flex::row()
162 // .with_child(
163 // Container::new(
164 // LineBox::new(
165 // Svg::new("icons/file-16.svg")
166 // .with_color(style.label.text.color)
167 // .boxed(),
168 // style.label.text.clone(),
169 // )
170 // .boxed(),
171 // )
172 // .with_padding_right(6.0)
173 // .boxed(),
174 // )
175 .with_child(
176 Flexible::new(
177 1.0,
178 false,
179 Flex::column()
180 .with_child(
181 Label::new(file_name.to_string(), style.label.clone())
182 .with_highlights(file_name_positions)
183 .boxed(),
184 )
185 .with_child(
186 Label::new(full_path, style.label.clone())
187 .with_highlights(full_path_positions)
188 .boxed(),
189 )
190 .boxed(),
191 )
192 .boxed(),
193 )
194 .boxed(),
195 )
196 .with_style(style.container);
197
198 let action = Select(ProjectPath {
199 worktree_id: WorktreeId::from_usize(path_match.worktree_id),
200 path: path_match.path.clone(),
201 });
202 EventHandler::new(container.boxed())
203 .on_mouse_down(move |cx| {
204 cx.dispatch_action(action.clone());
205 true
206 })
207 .named("match")
208 }
209
210 fn labels_for_match(&self, path_match: &PathMatch) -> (String, Vec<usize>, String, Vec<usize>) {
211 let path_string = path_match.path.to_string_lossy();
212 let full_path = [path_match.path_prefix.as_ref(), path_string.as_ref()].join("");
213 let path_positions = path_match.positions.clone();
214
215 let file_name = path_match.path.file_name().map_or_else(
216 || path_match.path_prefix.to_string(),
217 |file_name| file_name.to_string_lossy().to_string(),
218 );
219 let file_name_start = path_match.path_prefix.chars().count() + path_string.chars().count()
220 - file_name.chars().count();
221 let file_name_positions = path_positions
222 .iter()
223 .filter_map(|pos| {
224 if pos >= &file_name_start {
225 Some(pos - file_name_start)
226 } else {
227 None
228 }
229 })
230 .collect();
231
232 (file_name, file_name_positions, full_path, path_positions)
233 }
234
235 fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
236 workspace.toggle_modal(cx, |cx, workspace| {
237 let project = workspace.project().clone();
238 let finder = cx.add_view(|cx| Self::new(workspace.settings.clone(), project, cx));
239 cx.subscribe(&finder, Self::on_event).detach();
240 finder
241 });
242 }
243
244 fn on_event(
245 workspace: &mut Workspace,
246 _: ViewHandle<FileFinder>,
247 event: &Event,
248 cx: &mut ViewContext<Workspace>,
249 ) {
250 match event {
251 Event::Selected(project_path) => {
252 workspace
253 .open_path(project_path.clone(), cx)
254 .detach_and_log_err(cx);
255 workspace.dismiss_modal(cx);
256 }
257 Event::Dismissed => {
258 workspace.dismiss_modal(cx);
259 }
260 }
261 }
262
263 pub fn new(
264 settings: watch::Receiver<Settings>,
265 project: ModelHandle<Project>,
266 cx: &mut ViewContext<Self>,
267 ) -> Self {
268 cx.observe(&project, Self::project_updated).detach();
269
270 let query_editor = cx.add_view(|cx| {
271 Editor::single_line(
272 {
273 let settings = settings.clone();
274 Arc::new(move |_| {
275 let settings = settings.borrow();
276 EditorSettings {
277 style: settings.theme.selector.input_editor.as_editor(),
278 tab_size: settings.tab_size,
279 soft_wrap: editor::SoftWrap::None,
280 }
281 })
282 },
283 cx,
284 )
285 });
286 cx.subscribe(&query_editor, Self::on_query_editor_event)
287 .detach();
288
289 Self {
290 handle: cx.weak_handle(),
291 settings,
292 project,
293 query_editor,
294 search_count: 0,
295 latest_search_id: 0,
296 latest_search_did_cancel: false,
297 latest_search_query: String::new(),
298 matches: Vec::new(),
299 selected: None,
300 cancel_flag: Arc::new(AtomicBool::new(false)),
301 list_state: Default::default(),
302 }
303 }
304
305 fn project_updated(&mut self, _: ModelHandle<Project>, cx: &mut ViewContext<Self>) {
306 let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
307 if let Some(task) = self.spawn_search(query, cx) {
308 task.detach();
309 }
310 }
311
312 fn on_query_editor_event(
313 &mut self,
314 _: ViewHandle<Editor>,
315 event: &editor::Event,
316 cx: &mut ViewContext<Self>,
317 ) {
318 match event {
319 editor::Event::Edited => {
320 let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
321 if query.is_empty() {
322 self.latest_search_id = post_inc(&mut self.search_count);
323 self.matches.clear();
324 cx.notify();
325 } else {
326 if let Some(task) = self.spawn_search(query, cx) {
327 task.detach();
328 }
329 }
330 }
331 editor::Event::Blurred => cx.emit(Event::Dismissed),
332 _ => {}
333 }
334 }
335
336 fn selected_index(&self) -> usize {
337 if let Some(selected) = self.selected.as_ref() {
338 for (ix, path_match) in self.matches.iter().enumerate() {
339 if (path_match.worktree_id, path_match.path.as_ref())
340 == (selected.0, selected.1.as_ref())
341 {
342 return ix;
343 }
344 }
345 }
346 0
347 }
348
349 fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
350 let mut selected_index = self.selected_index();
351 if selected_index > 0 {
352 selected_index -= 1;
353 let mat = &self.matches[selected_index];
354 self.selected = Some((mat.worktree_id, mat.path.clone()));
355 }
356 self.list_state.scroll_to(selected_index);
357 cx.notify();
358 }
359
360 fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
361 let mut selected_index = self.selected_index();
362 if selected_index + 1 < self.matches.len() {
363 selected_index += 1;
364 let mat = &self.matches[selected_index];
365 self.selected = Some((mat.worktree_id, mat.path.clone()));
366 }
367 self.list_state.scroll_to(selected_index);
368 cx.notify();
369 }
370
371 fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
372 if let Some(m) = self.matches.get(self.selected_index()) {
373 cx.emit(Event::Selected(ProjectPath {
374 worktree_id: WorktreeId::from_usize(m.worktree_id),
375 path: m.path.clone(),
376 }));
377 }
378 }
379
380 fn select(&mut self, Select(project_path): &Select, cx: &mut ViewContext<Self>) {
381 cx.emit(Event::Selected(project_path.clone()));
382 }
383
384 #[must_use]
385 fn spawn_search(&mut self, query: String, cx: &mut ViewContext<Self>) -> Option<Task<()>> {
386 let search_id = util::post_inc(&mut self.search_count);
387 self.cancel_flag.store(true, atomic::Ordering::Relaxed);
388 self.cancel_flag = Arc::new(AtomicBool::new(false));
389 let cancel_flag = self.cancel_flag.clone();
390 let project = self.project.clone();
391 Some(cx.spawn(|this, mut cx| async move {
392 let matches = project
393 .read_with(&cx, |project, cx| {
394 project.match_paths(&query, false, false, 100, cancel_flag.as_ref(), cx)
395 })
396 .await;
397 let did_cancel = cancel_flag.load(atomic::Ordering::Relaxed);
398 this.update(&mut cx, |this, cx| {
399 this.update_matches((search_id, did_cancel, query, matches), cx)
400 });
401 }))
402 }
403
404 fn update_matches(
405 &mut self,
406 (search_id, did_cancel, query, matches): (usize, bool, String, Vec<PathMatch>),
407 cx: &mut ViewContext<Self>,
408 ) {
409 if search_id >= self.latest_search_id {
410 self.latest_search_id = search_id;
411 if self.latest_search_did_cancel && query == self.latest_search_query {
412 util::extend_sorted(&mut self.matches, matches.into_iter(), 100, |a, b| b.cmp(a));
413 } else {
414 self.matches = matches;
415 }
416 self.latest_search_query = query;
417 self.latest_search_did_cancel = did_cancel;
418 self.list_state.scroll_to(self.selected_index());
419 cx.notify();
420 }
421 }
422}
423
424#[cfg(test)]
425mod tests {
426 use super::*;
427 use editor::Input;
428 use serde_json::json;
429 use std::path::PathBuf;
430 use workspace::{Workspace, WorkspaceParams};
431
432 #[gpui::test]
433 async fn test_matching_paths(mut cx: gpui::TestAppContext) {
434 let mut path_openers = Vec::new();
435 cx.update(|cx| {
436 super::init(cx);
437 editor::init(cx, &mut path_openers);
438 });
439
440 let mut params = cx.update(WorkspaceParams::test);
441 params.path_openers = Arc::from(path_openers);
442 params
443 .fs
444 .as_fake()
445 .insert_tree(
446 "/root",
447 json!({
448 "a": {
449 "banana": "",
450 "bandana": "",
451 }
452 }),
453 )
454 .await;
455
456 let (window_id, workspace) = cx.add_window(|cx| Workspace::new(¶ms, cx));
457 workspace
458 .update(&mut cx, |workspace, cx| {
459 workspace.add_worktree(Path::new("/root"), cx)
460 })
461 .await
462 .unwrap();
463 cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
464 .await;
465 cx.dispatch_action(window_id, vec![workspace.id()], Toggle);
466
467 let finder = cx.read(|cx| {
468 workspace
469 .read(cx)
470 .modal()
471 .cloned()
472 .unwrap()
473 .downcast::<FileFinder>()
474 .unwrap()
475 });
476 let query_buffer = cx.read(|cx| finder.read(cx).query_editor.clone());
477
478 let chain = vec![finder.id(), query_buffer.id()];
479 cx.dispatch_action(window_id, chain.clone(), Input("b".into()));
480 cx.dispatch_action(window_id, chain.clone(), Input("n".into()));
481 cx.dispatch_action(window_id, chain.clone(), Input("a".into()));
482 finder
483 .condition(&cx, |finder, _| finder.matches.len() == 2)
484 .await;
485
486 let active_pane = cx.read(|cx| workspace.read(cx).active_pane().clone());
487 cx.dispatch_action(window_id, vec![workspace.id(), finder.id()], SelectNext);
488 cx.dispatch_action(window_id, vec![workspace.id(), finder.id()], Confirm);
489 active_pane
490 .condition(&cx, |pane, _| pane.active_item().is_some())
491 .await;
492 cx.read(|cx| {
493 let active_item = active_pane.read(cx).active_item().unwrap();
494 assert_eq!(active_item.title(cx), "bandana");
495 });
496 }
497
498 #[gpui::test]
499 async fn test_matching_cancellation(mut cx: gpui::TestAppContext) {
500 let params = cx.update(WorkspaceParams::test);
501 let fs = params.fs.as_fake();
502 fs.insert_tree(
503 "/dir",
504 json!({
505 "hello": "",
506 "goodbye": "",
507 "halogen-light": "",
508 "happiness": "",
509 "height": "",
510 "hi": "",
511 "hiccup": "",
512 }),
513 )
514 .await;
515
516 let (_, workspace) = cx.add_window(|cx| Workspace::new(¶ms, cx));
517 workspace
518 .update(&mut cx, |workspace, cx| {
519 workspace.add_worktree("/dir".as_ref(), cx)
520 })
521 .await
522 .unwrap();
523 cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
524 .await;
525 let (_, finder) = cx.add_window(|cx| {
526 FileFinder::new(
527 params.settings.clone(),
528 workspace.read(cx).project().clone(),
529 cx,
530 )
531 });
532
533 let query = "hi".to_string();
534 finder
535 .update(&mut cx, |f, cx| f.spawn_search(query.clone(), cx))
536 .unwrap()
537 .await;
538 finder.read_with(&cx, |f, _| assert_eq!(f.matches.len(), 5));
539
540 finder.update(&mut cx, |finder, cx| {
541 let matches = finder.matches.clone();
542
543 // Simulate a search being cancelled after the time limit,
544 // returning only a subset of the matches that would have been found.
545 finder.spawn_search(query.clone(), cx).unwrap().detach();
546 finder.update_matches(
547 (
548 finder.latest_search_id,
549 true, // did-cancel
550 query.clone(),
551 vec![matches[1].clone(), matches[3].clone()],
552 ),
553 cx,
554 );
555
556 // Simulate another cancellation.
557 finder.spawn_search(query.clone(), cx).unwrap().detach();
558 finder.update_matches(
559 (
560 finder.latest_search_id,
561 true, // did-cancel
562 query.clone(),
563 vec![matches[0].clone(), matches[2].clone(), matches[3].clone()],
564 ),
565 cx,
566 );
567
568 assert_eq!(finder.matches, matches[0..4])
569 });
570 }
571
572 #[gpui::test]
573 async fn test_single_file_worktrees(mut cx: gpui::TestAppContext) {
574 let params = cx.update(WorkspaceParams::test);
575 params
576 .fs
577 .as_fake()
578 .insert_tree("/root", json!({ "the-parent-dir": { "the-file": "" } }))
579 .await;
580
581 let (_, workspace) = cx.add_window(|cx| Workspace::new(¶ms, cx));
582 workspace
583 .update(&mut cx, |workspace, cx| {
584 workspace.add_worktree(Path::new("/root/the-parent-dir/the-file"), cx)
585 })
586 .await
587 .unwrap();
588 cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
589 .await;
590 let (_, finder) = cx.add_window(|cx| {
591 FileFinder::new(
592 params.settings.clone(),
593 workspace.read(cx).project().clone(),
594 cx,
595 )
596 });
597
598 // Even though there is only one worktree, that worktree's filename
599 // is included in the matching, because the worktree is a single file.
600 finder
601 .update(&mut cx, |f, cx| f.spawn_search("thf".into(), cx))
602 .unwrap()
603 .await;
604 cx.read(|cx| {
605 let finder = finder.read(cx);
606 assert_eq!(finder.matches.len(), 1);
607
608 let (file_name, file_name_positions, full_path, full_path_positions) =
609 finder.labels_for_match(&finder.matches[0]);
610 assert_eq!(file_name, "the-file");
611 assert_eq!(file_name_positions, &[0, 1, 4]);
612 assert_eq!(full_path, "the-file");
613 assert_eq!(full_path_positions, &[0, 1, 4]);
614 });
615
616 // Since the worktree root is a file, searching for its name followed by a slash does
617 // not match anything.
618 finder
619 .update(&mut cx, |f, cx| f.spawn_search("thf/".into(), cx))
620 .unwrap()
621 .await;
622 finder.read_with(&cx, |f, _| assert_eq!(f.matches.len(), 0));
623 }
624
625 #[gpui::test(retries = 5)]
626 async fn test_multiple_matches_with_same_relative_path(mut cx: gpui::TestAppContext) {
627 let params = cx.update(WorkspaceParams::test);
628 params
629 .fs
630 .as_fake()
631 .insert_tree(
632 "/root",
633 json!({
634 "dir1": { "a.txt": "" },
635 "dir2": { "a.txt": "" }
636 }),
637 )
638 .await;
639
640 let (_, workspace) = cx.add_window(|cx| Workspace::new(¶ms, cx));
641
642 workspace
643 .update(&mut cx, |workspace, cx| {
644 workspace.open_paths(
645 &[PathBuf::from("/root/dir1"), PathBuf::from("/root/dir2")],
646 cx,
647 )
648 })
649 .await;
650 cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
651 .await;
652
653 let (_, finder) = cx.add_window(|cx| {
654 FileFinder::new(
655 params.settings.clone(),
656 workspace.read(cx).project().clone(),
657 cx,
658 )
659 });
660
661 // Run a search that matches two files with the same relative path.
662 finder
663 .update(&mut cx, |f, cx| f.spawn_search("a.t".into(), cx))
664 .unwrap()
665 .await;
666
667 // Can switch between different matches with the same relative path.
668 finder.update(&mut cx, |f, cx| {
669 assert_eq!(f.matches.len(), 2);
670 assert_eq!(f.selected_index(), 0);
671 f.select_next(&SelectNext, cx);
672 assert_eq!(f.selected_index(), 1);
673 f.select_prev(&SelectPrev, cx);
674 assert_eq!(f.selected_index(), 0);
675 });
676 }
677}