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