lib.rs

  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};
 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, 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                        Flex::column()
179                            .with_child(
180                                Label::new(file_name.to_string(), style.label.clone())
181                                    .with_highlights(file_name_positions)
182                                    .boxed(),
183                            )
184                            .with_child(
185                                Label::new(full_path, style.label.clone())
186                                    .with_highlights(full_path_positions)
187                                    .boxed(),
188                            )
189                            .boxed(),
190                    )
191                    .boxed(),
192                )
193                .boxed(),
194        )
195        .with_style(style.container);
196
197        let action = Select(ProjectPath {
198            worktree_id: path_match.worktree_id,
199            path: path_match.path.clone(),
200        });
201        EventHandler::new(container.boxed())
202            .on_mouse_down(move |cx| {
203                cx.dispatch_action(action.clone());
204                true
205            })
206            .named("match")
207    }
208
209    fn labels_for_match(&self, path_match: &PathMatch) -> (String, Vec<usize>, String, Vec<usize>) {
210        let path_string = path_match.path.to_string_lossy();
211        let full_path = [path_match.path_prefix.as_ref(), path_string.as_ref()].join("");
212        let path_positions = path_match.positions.clone();
213
214        let file_name = path_match.path.file_name().map_or_else(
215            || path_match.path_prefix.to_string(),
216            |file_name| file_name.to_string_lossy().to_string(),
217        );
218        let file_name_start = path_match.path_prefix.chars().count() + path_string.chars().count()
219            - file_name.chars().count();
220        let file_name_positions = path_positions
221            .iter()
222            .filter_map(|pos| {
223                if pos >= &file_name_start {
224                    Some(pos - file_name_start)
225                } else {
226                    None
227                }
228            })
229            .collect();
230
231        (file_name, file_name_positions, full_path, path_positions)
232    }
233
234    fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
235        workspace.toggle_modal(cx, |cx, workspace| {
236            let project = workspace.project().clone();
237            let finder = cx.add_view(|cx| Self::new(workspace.settings.clone(), project, cx));
238            cx.subscribe(&finder, Self::on_event).detach();
239            finder
240        });
241    }
242
243    fn on_event(
244        workspace: &mut Workspace,
245        _: ViewHandle<FileFinder>,
246        event: &Event,
247        cx: &mut ViewContext<Workspace>,
248    ) {
249        match event {
250            Event::Selected(project_path) => {
251                workspace
252                    .open_entry(project_path.clone(), cx)
253                    .map(|d| d.detach());
254                workspace.dismiss_modal(cx);
255            }
256            Event::Dismissed => {
257                workspace.dismiss_modal(cx);
258            }
259        }
260    }
261
262    pub fn new(
263        settings: watch::Receiver<Settings>,
264        project: ModelHandle<Project>,
265        cx: &mut ViewContext<Self>,
266    ) -> Self {
267        cx.observe(&project, Self::project_updated).detach();
268
269        let query_editor = cx.add_view(|cx| {
270            Editor::single_line(
271                {
272                    let settings = settings.clone();
273                    move |_| {
274                        let settings = settings.borrow();
275                        EditorSettings {
276                            style: settings.theme.selector.input_editor.as_editor(),
277                            tab_size: settings.tab_size,
278                            soft_wrap: editor::SoftWrap::None,
279                        }
280                    }
281                },
282                cx,
283            )
284        });
285        cx.subscribe(&query_editor, Self::on_query_editor_event)
286            .detach();
287
288        Self {
289            handle: cx.weak_handle(),
290            settings,
291            project,
292            query_editor,
293            search_count: 0,
294            latest_search_id: 0,
295            latest_search_did_cancel: false,
296            latest_search_query: String::new(),
297            matches: Vec::new(),
298            selected: None,
299            cancel_flag: Arc::new(AtomicBool::new(false)),
300            list_state: Default::default(),
301        }
302    }
303
304    fn project_updated(&mut self, _: ModelHandle<Project>, cx: &mut ViewContext<Self>) {
305        let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
306        if let Some(task) = self.spawn_search(query, cx) {
307            task.detach();
308        }
309    }
310
311    fn on_query_editor_event(
312        &mut self,
313        _: ViewHandle<Editor>,
314        event: &editor::Event,
315        cx: &mut ViewContext<Self>,
316    ) {
317        match event {
318            editor::Event::Edited => {
319                let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
320                if query.is_empty() {
321                    self.latest_search_id = post_inc(&mut self.search_count);
322                    self.matches.clear();
323                    cx.notify();
324                } else {
325                    if let Some(task) = self.spawn_search(query, cx) {
326                        task.detach();
327                    }
328                }
329            }
330            editor::Event::Blurred => cx.emit(Event::Dismissed),
331            _ => {}
332        }
333    }
334
335    fn selected_index(&self) -> usize {
336        if let Some(selected) = self.selected.as_ref() {
337            for (ix, path_match) in self.matches.iter().enumerate() {
338                if (path_match.worktree_id, path_match.path.as_ref())
339                    == (selected.0, selected.1.as_ref())
340                {
341                    return ix;
342                }
343            }
344        }
345        0
346    }
347
348    fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
349        let mut selected_index = self.selected_index();
350        if selected_index > 0 {
351            selected_index -= 1;
352            let mat = &self.matches[selected_index];
353            self.selected = Some((mat.worktree_id, mat.path.clone()));
354        }
355        self.list_state.scroll_to(selected_index);
356        cx.notify();
357    }
358
359    fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
360        let mut selected_index = self.selected_index();
361        if selected_index + 1 < self.matches.len() {
362            selected_index += 1;
363            let mat = &self.matches[selected_index];
364            self.selected = Some((mat.worktree_id, mat.path.clone()));
365        }
366        self.list_state.scroll_to(selected_index);
367        cx.notify();
368    }
369
370    fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
371        if let Some(m) = self.matches.get(self.selected_index()) {
372            cx.emit(Event::Selected(ProjectPath {
373                worktree_id: m.worktree_id,
374                path: m.path.clone(),
375            }));
376        }
377    }
378
379    fn select(&mut self, Select(project_path): &Select, cx: &mut ViewContext<Self>) {
380        cx.emit(Event::Selected(project_path.clone()));
381    }
382
383    #[must_use]
384    fn spawn_search(&mut self, query: String, cx: &mut ViewContext<Self>) -> Option<Task<()>> {
385        let search_id = util::post_inc(&mut self.search_count);
386        self.cancel_flag.store(true, atomic::Ordering::Relaxed);
387        self.cancel_flag = Arc::new(AtomicBool::new(false));
388        let cancel_flag = self.cancel_flag.clone();
389        let project = self.project.clone();
390        Some(cx.spawn(|this, mut cx| async move {
391            let matches = project
392                .read_with(&cx, |project, cx| {
393                    project.match_paths(&query, false, false, 100, cancel_flag.as_ref(), cx)
394                })
395                .await;
396            let did_cancel = cancel_flag.load(atomic::Ordering::Relaxed);
397            this.update(&mut cx, |this, cx| {
398                this.update_matches((search_id, did_cancel, query, matches), cx)
399            });
400        }))
401    }
402
403    fn update_matches(
404        &mut self,
405        (search_id, did_cancel, query, matches): (usize, bool, String, Vec<PathMatch>),
406        cx: &mut ViewContext<Self>,
407    ) {
408        if search_id >= self.latest_search_id {
409            self.latest_search_id = search_id;
410            if self.latest_search_did_cancel && query == self.latest_search_query {
411                util::extend_sorted(&mut self.matches, matches.into_iter(), 100, |a, b| b.cmp(a));
412            } else {
413                self.matches = matches;
414            }
415            self.latest_search_query = query;
416            self.latest_search_did_cancel = did_cancel;
417            self.list_state.scroll_to(self.selected_index());
418            cx.notify();
419        }
420    }
421}
422
423#[cfg(test)]
424mod tests {
425    use super::*;
426    use editor::Input;
427    use serde_json::json;
428    use std::path::PathBuf;
429    use workspace::{Workspace, WorkspaceParams};
430
431    #[gpui::test]
432    async fn test_matching_paths(mut cx: gpui::TestAppContext) {
433        let mut entry_openers = Vec::new();
434        cx.update(|cx| {
435            super::init(cx);
436            editor::init(cx, &mut entry_openers);
437        });
438
439        let mut params = cx.update(WorkspaceParams::test);
440        params.entry_openers = Arc::from(entry_openers);
441        params
442            .fs
443            .as_fake()
444            .insert_tree(
445                "/root",
446                json!({
447                    "a": {
448                        "banana": "",
449                        "bandana": "",
450                    }
451                }),
452            )
453            .await;
454
455        let (window_id, workspace) = cx.add_window(|cx| Workspace::new(&params, cx));
456        workspace
457            .update(&mut cx, |workspace, cx| {
458                workspace.add_worktree(Path::new("/root"), cx)
459            })
460            .await
461            .unwrap();
462        cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
463            .await;
464        cx.dispatch_action(window_id, vec![workspace.id()], Toggle);
465
466        let finder = cx.read(|cx| {
467            workspace
468                .read(cx)
469                .modal()
470                .cloned()
471                .unwrap()
472                .downcast::<FileFinder>()
473                .unwrap()
474        });
475        let query_buffer = cx.read(|cx| finder.read(cx).query_editor.clone());
476
477        let chain = vec![finder.id(), query_buffer.id()];
478        cx.dispatch_action(window_id, chain.clone(), Input("b".into()));
479        cx.dispatch_action(window_id, chain.clone(), Input("n".into()));
480        cx.dispatch_action(window_id, chain.clone(), Input("a".into()));
481        finder
482            .condition(&cx, |finder, _| finder.matches.len() == 2)
483            .await;
484
485        let active_pane = cx.read(|cx| workspace.read(cx).active_pane().clone());
486        cx.dispatch_action(window_id, vec![workspace.id(), finder.id()], SelectNext);
487        cx.dispatch_action(window_id, vec![workspace.id(), finder.id()], Confirm);
488        active_pane
489            .condition(&cx, |pane, _| pane.active_item().is_some())
490            .await;
491        cx.read(|cx| {
492            let active_item = active_pane.read(cx).active_item().unwrap();
493            assert_eq!(active_item.title(cx), "bandana");
494        });
495    }
496
497    #[gpui::test]
498    async fn test_matching_cancellation(mut cx: gpui::TestAppContext) {
499        let params = cx.update(WorkspaceParams::test);
500        let fs = params.fs.as_fake();
501        fs.insert_tree(
502            "/dir",
503            json!({
504                "hello": "",
505                "goodbye": "",
506                "halogen-light": "",
507                "happiness": "",
508                "height": "",
509                "hi": "",
510                "hiccup": "",
511            }),
512        )
513        .await;
514
515        let (_, workspace) = cx.add_window(|cx| Workspace::new(&params, cx));
516        workspace
517            .update(&mut cx, |workspace, cx| {
518                workspace.add_worktree("/dir".as_ref(), cx)
519            })
520            .await
521            .unwrap();
522        cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
523            .await;
524        let (_, finder) = cx.add_window(|cx| {
525            FileFinder::new(
526                params.settings.clone(),
527                workspace.read(cx).project().clone(),
528                cx,
529            )
530        });
531
532        let query = "hi".to_string();
533        finder
534            .update(&mut cx, |f, cx| f.spawn_search(query.clone(), cx))
535            .unwrap()
536            .await;
537        finder.read_with(&cx, |f, _| assert_eq!(f.matches.len(), 5));
538
539        finder.update(&mut cx, |finder, cx| {
540            let matches = finder.matches.clone();
541
542            // Simulate a search being cancelled after the time limit,
543            // returning only a subset of the matches that would have been found.
544            finder.spawn_search(query.clone(), cx).unwrap().detach();
545            finder.update_matches(
546                (
547                    finder.latest_search_id,
548                    true, // did-cancel
549                    query.clone(),
550                    vec![matches[1].clone(), matches[3].clone()],
551                ),
552                cx,
553            );
554
555            // Simulate another cancellation.
556            finder.spawn_search(query.clone(), cx).unwrap().detach();
557            finder.update_matches(
558                (
559                    finder.latest_search_id,
560                    true, // did-cancel
561                    query.clone(),
562                    vec![matches[0].clone(), matches[2].clone(), matches[3].clone()],
563                ),
564                cx,
565            );
566
567            assert_eq!(finder.matches, matches[0..4])
568        });
569    }
570
571    #[gpui::test]
572    async fn test_single_file_worktrees(mut cx: gpui::TestAppContext) {
573        let params = cx.update(WorkspaceParams::test);
574        params
575            .fs
576            .as_fake()
577            .insert_tree("/root", json!({ "the-parent-dir": { "the-file": "" } }))
578            .await;
579
580        let (_, workspace) = cx.add_window(|cx| Workspace::new(&params, cx));
581        workspace
582            .update(&mut cx, |workspace, cx| {
583                workspace.add_worktree(Path::new("/root/the-parent-dir/the-file"), cx)
584            })
585            .await
586            .unwrap();
587        cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
588            .await;
589        let (_, finder) = cx.add_window(|cx| {
590            FileFinder::new(
591                params.settings.clone(),
592                workspace.read(cx).project().clone(),
593                cx,
594            )
595        });
596
597        // Even though there is only one worktree, that worktree's filename
598        // is included in the matching, because the worktree is a single file.
599        finder
600            .update(&mut cx, |f, cx| f.spawn_search("thf".into(), cx))
601            .unwrap()
602            .await;
603        cx.read(|cx| {
604            let finder = finder.read(cx);
605            assert_eq!(finder.matches.len(), 1);
606
607            let (file_name, file_name_positions, full_path, full_path_positions) =
608                finder.labels_for_match(&finder.matches[0]);
609            assert_eq!(file_name, "the-file");
610            assert_eq!(file_name_positions, &[0, 1, 4]);
611            assert_eq!(full_path, "the-file");
612            assert_eq!(full_path_positions, &[0, 1, 4]);
613        });
614
615        // Since the worktree root is a file, searching for its name followed by a slash does
616        // not match anything.
617        finder
618            .update(&mut cx, |f, cx| f.spawn_search("thf/".into(), cx))
619            .unwrap()
620            .await;
621        finder.read_with(&cx, |f, _| assert_eq!(f.matches.len(), 0));
622    }
623
624    #[gpui::test(retries = 5)]
625    async fn test_multiple_matches_with_same_relative_path(mut cx: gpui::TestAppContext) {
626        let params = cx.update(WorkspaceParams::test);
627        params
628            .fs
629            .as_fake()
630            .insert_tree(
631                "/root",
632                json!({
633                    "dir1": { "a.txt": "" },
634                    "dir2": { "a.txt": "" }
635                }),
636            )
637            .await;
638
639        let (_, workspace) = cx.add_window(|cx| Workspace::new(&params, cx));
640
641        workspace
642            .update(&mut cx, |workspace, cx| {
643                workspace.open_paths(
644                    &[PathBuf::from("/root/dir1"), PathBuf::from("/root/dir2")],
645                    cx,
646                )
647            })
648            .await;
649        cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
650            .await;
651
652        let (_, finder) = cx.add_window(|cx| {
653            FileFinder::new(
654                params.settings.clone(),
655                workspace.read(cx).project().clone(),
656                cx,
657            )
658        });
659
660        // Run a search that matches two files with the same relative path.
661        finder
662            .update(&mut cx, |f, cx| f.spawn_search("a.t".into(), cx))
663            .unwrap()
664            .await;
665
666        // Can switch between different matches with the same relative path.
667        finder.update(&mut cx, |f, cx| {
668            assert_eq!(f.matches.len(), 2);
669            assert_eq!(f.selected_index(), 0);
670            f.select_next(&SelectNext, cx);
671            assert_eq!(f.selected_index(), 1);
672            f.select_prev(&SelectPrev, cx);
673            assert_eq!(f.selected_index(), 0);
674        });
675    }
676}