project_search.rs

   1use crate::{
   2    SearchOption, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex,
   3    ToggleWholeWord,
   4};
   5use collections::HashMap;
   6use editor::{
   7    items::active_match_index, scroll::autoscroll::Autoscroll, Anchor, Editor, MultiBuffer,
   8    SelectAll, MAX_TAB_TITLE_LEN,
   9};
  10use futures::StreamExt;
  11use gpui::{
  12    actions, elements::*, platform::CursorStyle, Action, AnyViewHandle, AppContext, ElementBox,
  13    Entity, ModelContext, ModelHandle, MouseButton, MutableAppContext, RenderContext, Subscription,
  14    Task, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
  15};
  16use menu::Confirm;
  17use project::{search::SearchQuery, Project};
  18use settings::Settings;
  19use smallvec::SmallVec;
  20use std::{
  21    any::{Any, TypeId},
  22    mem,
  23    ops::Range,
  24    path::PathBuf,
  25    sync::Arc,
  26};
  27use util::ResultExt as _;
  28use workspace::{
  29    item::{Item, ItemEvent, ItemHandle},
  30    searchable::{Direction, SearchableItem, SearchableItemHandle},
  31    ItemNavHistory, Pane, ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceId,
  32};
  33
  34actions!(project_search, [SearchInNew, ToggleFocus]);
  35
  36#[derive(Default)]
  37struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
  38
  39pub fn init(cx: &mut MutableAppContext) {
  40    cx.set_global(ActiveSearches::default());
  41    cx.add_action(ProjectSearchView::deploy);
  42    cx.add_action(ProjectSearchBar::search);
  43    cx.add_action(ProjectSearchBar::search_in_new);
  44    cx.add_action(ProjectSearchBar::select_next_match);
  45    cx.add_action(ProjectSearchBar::select_prev_match);
  46    cx.add_action(ProjectSearchBar::toggle_focus);
  47    cx.capture_action(ProjectSearchBar::tab);
  48    add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);
  49    add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);
  50    add_toggle_option_action::<ToggleRegex>(SearchOption::Regex, cx);
  51}
  52
  53fn add_toggle_option_action<A: Action>(option: SearchOption, cx: &mut MutableAppContext) {
  54    cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext<Pane>| {
  55        if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<ProjectSearchBar>() {
  56            if search_bar.update(cx, |search_bar, cx| {
  57                search_bar.toggle_search_option(option, cx)
  58            }) {
  59                return;
  60            }
  61        }
  62        cx.propagate_action();
  63    });
  64}
  65
  66struct ProjectSearch {
  67    project: ModelHandle<Project>,
  68    excerpts: ModelHandle<MultiBuffer>,
  69    pending_search: Option<Task<Option<()>>>,
  70    match_ranges: Vec<Range<Anchor>>,
  71    active_query: Option<SearchQuery>,
  72    search_id: usize,
  73}
  74
  75pub struct ProjectSearchView {
  76    model: ModelHandle<ProjectSearch>,
  77    query_editor: ViewHandle<Editor>,
  78    results_editor: ViewHandle<Editor>,
  79    case_sensitive: bool,
  80    whole_word: bool,
  81    regex: bool,
  82    query_contains_error: bool,
  83    active_match_index: Option<usize>,
  84    search_id: usize,
  85}
  86
  87pub struct ProjectSearchBar {
  88    active_project_search: Option<ViewHandle<ProjectSearchView>>,
  89    subscription: Option<Subscription>,
  90}
  91
  92impl Entity for ProjectSearch {
  93    type Event = ();
  94}
  95
  96impl ProjectSearch {
  97    fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
  98        let replica_id = project.read(cx).replica_id();
  99        Self {
 100            project,
 101            excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
 102            pending_search: Default::default(),
 103            match_ranges: Default::default(),
 104            active_query: None,
 105            search_id: 0,
 106        }
 107    }
 108
 109    fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
 110        cx.add_model(|cx| Self {
 111            project: self.project.clone(),
 112            excerpts: self
 113                .excerpts
 114                .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
 115            pending_search: Default::default(),
 116            match_ranges: self.match_ranges.clone(),
 117            active_query: self.active_query.clone(),
 118            search_id: self.search_id,
 119        })
 120    }
 121
 122    fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
 123        let search = self
 124            .project
 125            .update(cx, |project, cx| project.search(query.clone(), cx));
 126        self.search_id += 1;
 127        self.active_query = Some(query);
 128        self.match_ranges.clear();
 129        self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
 130            let matches = search.await.log_err()?;
 131            let this = this.upgrade(&cx)?;
 132            let mut matches = matches.into_iter().collect::<Vec<_>>();
 133            let (_task, mut match_ranges) = this.update(&mut cx, |this, cx| {
 134                this.match_ranges.clear();
 135                matches.sort_by_key(|(buffer, _)| buffer.read(cx).file().map(|file| file.path()));
 136                this.excerpts.update(cx, |excerpts, cx| {
 137                    excerpts.clear(cx);
 138                    excerpts.stream_excerpts_with_context_lines(matches, 1, cx)
 139                })
 140            });
 141
 142            while let Some(match_range) = match_ranges.next().await {
 143                this.update(&mut cx, |this, cx| {
 144                    this.match_ranges.push(match_range);
 145                    while let Ok(Some(match_range)) = match_ranges.try_next() {
 146                        this.match_ranges.push(match_range);
 147                    }
 148                    cx.notify();
 149                });
 150            }
 151
 152            this.update(&mut cx, |this, cx| {
 153                this.pending_search.take();
 154                cx.notify();
 155            });
 156
 157            None
 158        }));
 159        cx.notify();
 160    }
 161}
 162
 163pub enum ViewEvent {
 164    UpdateTab,
 165    Activate,
 166    EditorEvent(editor::Event),
 167}
 168
 169impl Entity for ProjectSearchView {
 170    type Event = ViewEvent;
 171}
 172
 173impl View for ProjectSearchView {
 174    fn ui_name() -> &'static str {
 175        "ProjectSearchView"
 176    }
 177
 178    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
 179        let model = &self.model.read(cx);
 180        if model.match_ranges.is_empty() {
 181            enum Status {}
 182
 183            let theme = cx.global::<Settings>().theme.clone();
 184            let text = if self.query_editor.read(cx).text(cx).is_empty() {
 185                ""
 186            } else if model.pending_search.is_some() {
 187                "Searching..."
 188            } else {
 189                "No results"
 190            };
 191            MouseEventHandler::<Status>::new(0, cx, |_, _| {
 192                Label::new(text, theme.search.results_status.clone())
 193                    .aligned()
 194                    .contained()
 195                    .with_background_color(theme.editor.background)
 196                    .flex(1., true)
 197                    .boxed()
 198            })
 199            .on_down(MouseButton::Left, |_, cx| {
 200                cx.focus_parent_view();
 201            })
 202            .boxed()
 203        } else {
 204            ChildView::new(&self.results_editor, cx)
 205                .flex(1., true)
 206                .boxed()
 207        }
 208    }
 209
 210    fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
 211        let handle = cx.weak_handle();
 212        cx.update_global(|state: &mut ActiveSearches, cx| {
 213            state
 214                .0
 215                .insert(self.model.read(cx).project.downgrade(), handle)
 216        });
 217
 218        if cx.is_self_focused() {
 219            self.focus_query_editor(cx);
 220        }
 221    }
 222}
 223
 224impl Item for ProjectSearchView {
 225    fn act_as_type(
 226        &self,
 227        type_id: TypeId,
 228        self_handle: &ViewHandle<Self>,
 229        _: &gpui::AppContext,
 230    ) -> Option<gpui::AnyViewHandle> {
 231        if type_id == TypeId::of::<Self>() {
 232            Some(self_handle.into())
 233        } else if type_id == TypeId::of::<Editor>() {
 234            Some((&self.results_editor).into())
 235        } else {
 236            None
 237        }
 238    }
 239
 240    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
 241        self.results_editor
 242            .update(cx, |editor, cx| editor.deactivated(cx));
 243    }
 244
 245    fn tab_content(
 246        &self,
 247        _detail: Option<usize>,
 248        tab_theme: &theme::Tab,
 249        cx: &gpui::AppContext,
 250    ) -> ElementBox {
 251        let settings = cx.global::<Settings>();
 252        let search_theme = &settings.theme.search;
 253        Flex::row()
 254            .with_child(
 255                Svg::new("icons/magnifying_glass_12.svg")
 256                    .with_color(tab_theme.label.text.color)
 257                    .constrained()
 258                    .with_width(search_theme.tab_icon_width)
 259                    .aligned()
 260                    .boxed(),
 261            )
 262            .with_children(self.model.read(cx).active_query.as_ref().map(|query| {
 263                let query_text = util::truncate_and_trailoff(query.as_str(), MAX_TAB_TITLE_LEN);
 264
 265                Label::new(query_text, tab_theme.label.clone())
 266                    .aligned()
 267                    .contained()
 268                    .with_margin_left(search_theme.tab_icon_spacing)
 269                    .boxed()
 270            }))
 271            .boxed()
 272    }
 273
 274    fn for_each_project_item(&self, cx: &AppContext, f: &mut dyn FnMut(usize, &dyn project::Item)) {
 275        self.results_editor.for_each_project_item(cx, f)
 276    }
 277
 278    fn is_singleton(&self, _: &AppContext) -> bool {
 279        false
 280    }
 281
 282    fn can_save(&self, _: &gpui::AppContext) -> bool {
 283        true
 284    }
 285
 286    fn is_dirty(&self, cx: &AppContext) -> bool {
 287        self.results_editor.read(cx).is_dirty(cx)
 288    }
 289
 290    fn has_conflict(&self, cx: &AppContext) -> bool {
 291        self.results_editor.read(cx).has_conflict(cx)
 292    }
 293
 294    fn save(
 295        &mut self,
 296        project: ModelHandle<Project>,
 297        cx: &mut ViewContext<Self>,
 298    ) -> Task<anyhow::Result<()>> {
 299        self.results_editor
 300            .update(cx, |editor, cx| editor.save(project, cx))
 301    }
 302
 303    fn save_as(
 304        &mut self,
 305        _: ModelHandle<Project>,
 306        _: PathBuf,
 307        _: &mut ViewContext<Self>,
 308    ) -> Task<anyhow::Result<()>> {
 309        unreachable!("save_as should not have been called")
 310    }
 311
 312    fn reload(
 313        &mut self,
 314        project: ModelHandle<Project>,
 315        cx: &mut ViewContext<Self>,
 316    ) -> Task<anyhow::Result<()>> {
 317        self.results_editor
 318            .update(cx, |editor, cx| editor.reload(project, cx))
 319    }
 320
 321    fn clone_on_split(&self, _workspace_id: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self>
 322    where
 323        Self: Sized,
 324    {
 325        let model = self.model.update(cx, |model, cx| model.clone(cx));
 326        Some(Self::new(model, cx))
 327    }
 328
 329    fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
 330        self.results_editor.update(cx, |editor, _| {
 331            editor.set_nav_history(Some(nav_history));
 332        });
 333    }
 334
 335    fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
 336        self.results_editor
 337            .update(cx, |editor, cx| editor.navigate(data, cx))
 338    }
 339
 340    fn git_diff_recalc(
 341        &mut self,
 342        project: ModelHandle<Project>,
 343        cx: &mut ViewContext<Self>,
 344    ) -> Task<anyhow::Result<()>> {
 345        self.results_editor
 346            .update(cx, |editor, cx| editor.git_diff_recalc(project, cx))
 347    }
 348
 349    fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
 350        match event {
 351            ViewEvent::UpdateTab => {
 352                smallvec::smallvec![ItemEvent::UpdateBreadcrumbs, ItemEvent::UpdateTab]
 353            }
 354            ViewEvent::EditorEvent(editor_event) => Editor::to_item_events(editor_event),
 355            _ => SmallVec::new(),
 356        }
 357    }
 358
 359    fn breadcrumb_location(&self) -> ToolbarItemLocation {
 360        if self.has_matches() {
 361            ToolbarItemLocation::Secondary
 362        } else {
 363            ToolbarItemLocation::Hidden
 364        }
 365    }
 366
 367    fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> {
 368        self.results_editor.breadcrumbs(theme, cx)
 369    }
 370
 371    fn serialized_item_kind() -> Option<&'static str> {
 372        None
 373    }
 374
 375    fn deserialize(
 376        _project: ModelHandle<Project>,
 377        _workspace: WeakViewHandle<Workspace>,
 378        _workspace_id: workspace::WorkspaceId,
 379        _item_id: workspace::ItemId,
 380        _cx: &mut ViewContext<Pane>,
 381    ) -> Task<anyhow::Result<ViewHandle<Self>>> {
 382        unimplemented!()
 383    }
 384}
 385
 386impl ProjectSearchView {
 387    fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
 388        let project;
 389        let excerpts;
 390        let mut query_text = String::new();
 391        let mut regex = false;
 392        let mut case_sensitive = false;
 393        let mut whole_word = false;
 394
 395        {
 396            let model = model.read(cx);
 397            project = model.project.clone();
 398            excerpts = model.excerpts.clone();
 399            if let Some(active_query) = model.active_query.as_ref() {
 400                query_text = active_query.as_str().to_string();
 401                regex = active_query.is_regex();
 402                case_sensitive = active_query.case_sensitive();
 403                whole_word = active_query.whole_word();
 404            }
 405        }
 406        cx.observe(&model, |this, _, cx| this.model_changed(cx))
 407            .detach();
 408
 409        let query_editor = cx.add_view(|cx| {
 410            let mut editor = Editor::single_line(
 411                Some(Arc::new(|theme| theme.search.editor.input.clone())),
 412                cx,
 413            );
 414            editor.set_text(query_text, cx);
 415            editor
 416        });
 417        // Subcribe to query_editor in order to reraise editor events for workspace item activation purposes
 418        cx.subscribe(&query_editor, |_, _, event, cx| {
 419            cx.emit(ViewEvent::EditorEvent(event.clone()))
 420        })
 421        .detach();
 422
 423        let results_editor = cx.add_view(|cx| {
 424            let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
 425            editor.set_searchable(false);
 426            editor
 427        });
 428        cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
 429            .detach();
 430
 431        cx.subscribe(&results_editor, |this, _, event, cx| {
 432            if matches!(event, editor::Event::SelectionsChanged { .. }) {
 433                this.update_match_index(cx);
 434            }
 435            // Reraise editor events for workspace item activation purposes
 436            cx.emit(ViewEvent::EditorEvent(event.clone()));
 437        })
 438        .detach();
 439
 440        let mut this = ProjectSearchView {
 441            search_id: model.read(cx).search_id,
 442            model,
 443            query_editor,
 444            results_editor,
 445            case_sensitive,
 446            whole_word,
 447            regex,
 448            query_contains_error: false,
 449            active_match_index: None,
 450        };
 451        this.model_changed(cx);
 452        this
 453    }
 454
 455    // Re-activate the most recently activated search or the most recent if it has been closed.
 456    // If no search exists in the workspace, create a new one.
 457    fn deploy(
 458        workspace: &mut Workspace,
 459        _: &workspace::NewSearch,
 460        cx: &mut ViewContext<Workspace>,
 461    ) {
 462        // Clean up entries for dropped projects
 463        cx.update_global(|state: &mut ActiveSearches, cx| {
 464            state.0.retain(|project, _| project.is_upgradable(cx))
 465        });
 466
 467        let active_search = cx
 468            .global::<ActiveSearches>()
 469            .0
 470            .get(&workspace.project().downgrade());
 471
 472        let existing = active_search
 473            .and_then(|active_search| {
 474                workspace
 475                    .items_of_type::<ProjectSearchView>(cx)
 476                    .find(|search| search == active_search)
 477            })
 478            .or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
 479
 480        let query = workspace.active_item(cx).and_then(|item| {
 481            let editor = item.act_as::<Editor>(cx)?;
 482            let query = editor.query_suggestion(cx);
 483            if query.is_empty() {
 484                None
 485            } else {
 486                Some(query)
 487            }
 488        });
 489
 490        let search = if let Some(existing) = existing {
 491            workspace.activate_item(&existing, cx);
 492            existing
 493        } else {
 494            let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
 495            let view = cx.add_view(|cx| ProjectSearchView::new(model, cx));
 496            workspace.add_item(Box::new(view.clone()), cx);
 497            view
 498        };
 499
 500        search.update(cx, |search, cx| {
 501            if let Some(query) = query {
 502                search.set_query(&query, cx);
 503            }
 504            search.focus_query_editor(cx)
 505        });
 506    }
 507
 508    fn search(&mut self, cx: &mut ViewContext<Self>) {
 509        if let Some(query) = self.build_search_query(cx) {
 510            self.model.update(cx, |model, cx| model.search(query, cx));
 511        }
 512    }
 513
 514    fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
 515        let text = self.query_editor.read(cx).text(cx);
 516        if self.regex {
 517            match SearchQuery::regex(text, self.whole_word, self.case_sensitive) {
 518                Ok(query) => Some(query),
 519                Err(_) => {
 520                    self.query_contains_error = true;
 521                    cx.notify();
 522                    None
 523                }
 524            }
 525        } else {
 526            Some(SearchQuery::text(
 527                text,
 528                self.whole_word,
 529                self.case_sensitive,
 530            ))
 531        }
 532    }
 533
 534    fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
 535        if let Some(index) = self.active_match_index {
 536            let match_ranges = self.model.read(cx).match_ranges.clone();
 537            let new_index = self.results_editor.update(cx, |editor, cx| {
 538                editor.match_index_for_direction(&match_ranges, index, direction, cx)
 539            });
 540
 541            let range_to_select = match_ranges[new_index].clone();
 542            self.results_editor.update(cx, |editor, cx| {
 543                editor.unfold_ranges([range_to_select.clone()], false, cx);
 544                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 545                    s.select_ranges([range_to_select])
 546                });
 547            });
 548        }
 549    }
 550
 551    fn focus_query_editor(&self, cx: &mut ViewContext<Self>) {
 552        self.query_editor.update(cx, |query_editor, cx| {
 553            query_editor.select_all(&SelectAll, cx);
 554        });
 555        cx.focus(&self.query_editor);
 556    }
 557
 558    fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
 559        self.query_editor
 560            .update(cx, |query_editor, cx| query_editor.set_text(query, cx));
 561    }
 562
 563    fn focus_results_editor(&self, cx: &mut ViewContext<Self>) {
 564        self.query_editor.update(cx, |query_editor, cx| {
 565            let cursor = query_editor.selections.newest_anchor().head();
 566            query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor]));
 567        });
 568        cx.focus(&self.results_editor);
 569    }
 570
 571    fn model_changed(&mut self, cx: &mut ViewContext<Self>) {
 572        let match_ranges = self.model.read(cx).match_ranges.clone();
 573        if match_ranges.is_empty() {
 574            self.active_match_index = None;
 575        } else {
 576            let prev_search_id = mem::replace(&mut self.search_id, self.model.read(cx).search_id);
 577            let is_new_search = self.search_id != prev_search_id;
 578            self.results_editor.update(cx, |editor, cx| {
 579                if is_new_search {
 580                    editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 581                        s.select_ranges(match_ranges.first().cloned())
 582                    });
 583                }
 584                editor.highlight_background::<Self>(
 585                    match_ranges,
 586                    |theme| theme.search.match_background,
 587                    cx,
 588                );
 589            });
 590            if is_new_search && self.query_editor.is_focused(cx) {
 591                self.focus_results_editor(cx);
 592            }
 593        }
 594
 595        cx.emit(ViewEvent::UpdateTab);
 596        cx.notify();
 597    }
 598
 599    fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
 600        let results_editor = self.results_editor.read(cx);
 601        let new_index = active_match_index(
 602            &self.model.read(cx).match_ranges,
 603            &results_editor.selections.newest_anchor().head(),
 604            &results_editor.buffer().read(cx).snapshot(cx),
 605        );
 606        if self.active_match_index != new_index {
 607            self.active_match_index = new_index;
 608            cx.notify();
 609        }
 610    }
 611
 612    pub fn has_matches(&self) -> bool {
 613        self.active_match_index.is_some()
 614    }
 615}
 616
 617impl Default for ProjectSearchBar {
 618    fn default() -> Self {
 619        Self::new()
 620    }
 621}
 622
 623impl ProjectSearchBar {
 624    pub fn new() -> Self {
 625        Self {
 626            active_project_search: Default::default(),
 627            subscription: Default::default(),
 628        }
 629    }
 630
 631    fn search(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
 632        if let Some(search_view) = self.active_project_search.as_ref() {
 633            search_view.update(cx, |search_view, cx| search_view.search(cx));
 634        }
 635    }
 636
 637    fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
 638        if let Some(search_view) = workspace
 639            .active_item(cx)
 640            .and_then(|item| item.downcast::<ProjectSearchView>())
 641        {
 642            let new_query = search_view.update(cx, |search_view, cx| {
 643                let new_query = search_view.build_search_query(cx);
 644                if new_query.is_some() {
 645                    if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
 646                        search_view.query_editor.update(cx, |editor, cx| {
 647                            editor.set_text(old_query.as_str(), cx);
 648                        });
 649                        search_view.regex = old_query.is_regex();
 650                        search_view.whole_word = old_query.whole_word();
 651                        search_view.case_sensitive = old_query.case_sensitive();
 652                    }
 653                }
 654                new_query
 655            });
 656            if let Some(new_query) = new_query {
 657                let model = cx.add_model(|cx| {
 658                    let mut model = ProjectSearch::new(workspace.project().clone(), cx);
 659                    model.search(new_query, cx);
 660                    model
 661                });
 662                workspace.add_item(
 663                    Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
 664                    cx,
 665                );
 666            }
 667        }
 668    }
 669
 670    fn select_next_match(pane: &mut Pane, _: &SelectNextMatch, cx: &mut ViewContext<Pane>) {
 671        if let Some(search_view) = pane
 672            .active_item()
 673            .and_then(|item| item.downcast::<ProjectSearchView>())
 674        {
 675            search_view.update(cx, |view, cx| view.select_match(Direction::Next, cx));
 676        } else {
 677            cx.propagate_action();
 678        }
 679    }
 680
 681    fn select_prev_match(pane: &mut Pane, _: &SelectPrevMatch, cx: &mut ViewContext<Pane>) {
 682        if let Some(search_view) = pane
 683            .active_item()
 684            .and_then(|item| item.downcast::<ProjectSearchView>())
 685        {
 686            search_view.update(cx, |view, cx| view.select_match(Direction::Prev, cx));
 687        } else {
 688            cx.propagate_action();
 689        }
 690    }
 691
 692    fn toggle_focus(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext<Pane>) {
 693        if let Some(search_view) = pane
 694            .active_item()
 695            .and_then(|item| item.downcast::<ProjectSearchView>())
 696        {
 697            search_view.update(cx, |search_view, cx| {
 698                if search_view.query_editor.is_focused(cx) {
 699                    if !search_view.model.read(cx).match_ranges.is_empty() {
 700                        search_view.focus_results_editor(cx);
 701                    }
 702                } else {
 703                    search_view.focus_query_editor(cx);
 704                }
 705            });
 706        } else {
 707            cx.propagate_action();
 708        }
 709    }
 710
 711    fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
 712        if let Some(search_view) = self.active_project_search.as_ref() {
 713            search_view.update(cx, |search_view, cx| {
 714                if search_view.query_editor.is_focused(cx) {
 715                    if !search_view.model.read(cx).match_ranges.is_empty() {
 716                        search_view.focus_results_editor(cx);
 717                    }
 718                } else {
 719                    cx.propagate_action();
 720                }
 721            });
 722        } else {
 723            cx.propagate_action();
 724        }
 725    }
 726
 727    fn toggle_search_option(&mut self, option: SearchOption, cx: &mut ViewContext<Self>) -> bool {
 728        if let Some(search_view) = self.active_project_search.as_ref() {
 729            search_view.update(cx, |search_view, cx| {
 730                let value = match option {
 731                    SearchOption::WholeWord => &mut search_view.whole_word,
 732                    SearchOption::CaseSensitive => &mut search_view.case_sensitive,
 733                    SearchOption::Regex => &mut search_view.regex,
 734                };
 735                *value = !*value;
 736                search_view.search(cx);
 737            });
 738            cx.notify();
 739            true
 740        } else {
 741            false
 742        }
 743    }
 744
 745    fn render_nav_button(
 746        &self,
 747        icon: &'static str,
 748        direction: Direction,
 749        cx: &mut RenderContext<Self>,
 750    ) -> ElementBox {
 751        let action: Box<dyn Action>;
 752        let tooltip;
 753        match direction {
 754            Direction::Prev => {
 755                action = Box::new(SelectPrevMatch);
 756                tooltip = "Select Previous Match";
 757            }
 758            Direction::Next => {
 759                action = Box::new(SelectNextMatch);
 760                tooltip = "Select Next Match";
 761            }
 762        };
 763        let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
 764
 765        enum NavButton {}
 766        MouseEventHandler::<NavButton>::new(direction as usize, cx, |state, cx| {
 767            let style = &cx
 768                .global::<Settings>()
 769                .theme
 770                .search
 771                .option_button
 772                .style_for(state, false);
 773            Label::new(icon, style.text.clone())
 774                .contained()
 775                .with_style(style.container)
 776                .boxed()
 777        })
 778        .on_click(MouseButton::Left, {
 779            let action = action.boxed_clone();
 780            move |_, cx| cx.dispatch_any_action(action.boxed_clone())
 781        })
 782        .with_cursor_style(CursorStyle::PointingHand)
 783        .with_tooltip::<NavButton, _>(
 784            direction as usize,
 785            tooltip.to_string(),
 786            Some(action),
 787            tooltip_style,
 788            cx,
 789        )
 790        .boxed()
 791    }
 792
 793    fn render_option_button(
 794        &self,
 795        icon: &'static str,
 796        option: SearchOption,
 797        cx: &mut RenderContext<Self>,
 798    ) -> ElementBox {
 799        let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
 800        let is_active = self.is_option_enabled(option, cx);
 801        MouseEventHandler::<Self>::new(option as usize, cx, |state, cx| {
 802            let style = &cx
 803                .global::<Settings>()
 804                .theme
 805                .search
 806                .option_button
 807                .style_for(state, is_active);
 808            Label::new(icon, style.text.clone())
 809                .contained()
 810                .with_style(style.container)
 811                .boxed()
 812        })
 813        .on_click(MouseButton::Left, move |_, cx| {
 814            cx.dispatch_any_action(option.to_toggle_action())
 815        })
 816        .with_cursor_style(CursorStyle::PointingHand)
 817        .with_tooltip::<Self, _>(
 818            option as usize,
 819            format!("Toggle {}", option.label()),
 820            Some(option.to_toggle_action()),
 821            tooltip_style,
 822            cx,
 823        )
 824        .boxed()
 825    }
 826
 827    fn is_option_enabled(&self, option: SearchOption, cx: &AppContext) -> bool {
 828        if let Some(search) = self.active_project_search.as_ref() {
 829            let search = search.read(cx);
 830            match option {
 831                SearchOption::WholeWord => search.whole_word,
 832                SearchOption::CaseSensitive => search.case_sensitive,
 833                SearchOption::Regex => search.regex,
 834            }
 835        } else {
 836            false
 837        }
 838    }
 839}
 840
 841impl Entity for ProjectSearchBar {
 842    type Event = ();
 843}
 844
 845impl View for ProjectSearchBar {
 846    fn ui_name() -> &'static str {
 847        "ProjectSearchBar"
 848    }
 849
 850    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
 851        if let Some(search) = self.active_project_search.as_ref() {
 852            let search = search.read(cx);
 853            let theme = cx.global::<Settings>().theme.clone();
 854            let editor_container = if search.query_contains_error {
 855                theme.search.invalid_editor
 856            } else {
 857                theme.search.editor.input.container
 858            };
 859            Flex::row()
 860                .with_child(
 861                    Flex::row()
 862                        .with_child(
 863                            ChildView::new(&search.query_editor, cx)
 864                                .aligned()
 865                                .left()
 866                                .flex(1., true)
 867                                .boxed(),
 868                        )
 869                        .with_children(search.active_match_index.map(|match_ix| {
 870                            Label::new(
 871                                format!(
 872                                    "{}/{}",
 873                                    match_ix + 1,
 874                                    search.model.read(cx).match_ranges.len()
 875                                ),
 876                                theme.search.match_index.text.clone(),
 877                            )
 878                            .contained()
 879                            .with_style(theme.search.match_index.container)
 880                            .aligned()
 881                            .boxed()
 882                        }))
 883                        .contained()
 884                        .with_style(editor_container)
 885                        .aligned()
 886                        .constrained()
 887                        .with_min_width(theme.search.editor.min_width)
 888                        .with_max_width(theme.search.editor.max_width)
 889                        .flex(1., false)
 890                        .boxed(),
 891                )
 892                .with_child(
 893                    Flex::row()
 894                        .with_child(self.render_nav_button("<", Direction::Prev, cx))
 895                        .with_child(self.render_nav_button(">", Direction::Next, cx))
 896                        .aligned()
 897                        .boxed(),
 898                )
 899                .with_child(
 900                    Flex::row()
 901                        .with_child(self.render_option_button(
 902                            "Case",
 903                            SearchOption::CaseSensitive,
 904                            cx,
 905                        ))
 906                        .with_child(self.render_option_button("Word", SearchOption::WholeWord, cx))
 907                        .with_child(self.render_option_button("Regex", SearchOption::Regex, cx))
 908                        .contained()
 909                        .with_style(theme.search.option_button_group)
 910                        .aligned()
 911                        .boxed(),
 912                )
 913                .contained()
 914                .with_style(theme.search.container)
 915                .aligned()
 916                .left()
 917                .named("project search")
 918        } else {
 919            Empty::new().boxed()
 920        }
 921    }
 922}
 923
 924impl ToolbarItemView for ProjectSearchBar {
 925    fn set_active_pane_item(
 926        &mut self,
 927        active_pane_item: Option<&dyn ItemHandle>,
 928        cx: &mut ViewContext<Self>,
 929    ) -> ToolbarItemLocation {
 930        cx.notify();
 931        self.subscription = None;
 932        self.active_project_search = None;
 933        if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
 934            let query_editor = search.read(cx).query_editor.clone();
 935            cx.reparent(query_editor);
 936            self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
 937            self.active_project_search = Some(search);
 938            ToolbarItemLocation::PrimaryLeft {
 939                flex: Some((1., false)),
 940            }
 941        } else {
 942            ToolbarItemLocation::Hidden
 943        }
 944    }
 945}
 946
 947#[cfg(test)]
 948mod tests {
 949    use super::*;
 950    use editor::DisplayPoint;
 951    use gpui::{color::Color, executor::Deterministic, TestAppContext};
 952    use project::FakeFs;
 953    use serde_json::json;
 954    use std::sync::Arc;
 955
 956    #[gpui::test]
 957    async fn test_project_search(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
 958        let fonts = cx.font_cache();
 959        let mut theme = gpui::fonts::with_font_cache(fonts.clone(), theme::Theme::default);
 960        theme.search.match_background = Color::red();
 961        cx.update(|cx| {
 962            let mut settings = Settings::test(cx);
 963            settings.theme = Arc::new(theme);
 964            cx.set_global(settings);
 965            cx.set_global(ActiveSearches::default());
 966        });
 967
 968        let fs = FakeFs::new(cx.background());
 969        fs.insert_tree(
 970            "/dir",
 971            json!({
 972                "one.rs": "const ONE: usize = 1;",
 973                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
 974                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
 975                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
 976            }),
 977        )
 978        .await;
 979        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
 980        let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
 981        let (_, search_view) = cx.add_window(|cx| ProjectSearchView::new(search.clone(), cx));
 982
 983        search_view.update(cx, |search_view, cx| {
 984            search_view
 985                .query_editor
 986                .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
 987            search_view.search(cx);
 988        });
 989        deterministic.run_until_parked();
 990        search_view.update(cx, |search_view, cx| {
 991            assert_eq!(
 992                search_view
 993                    .results_editor
 994                    .update(cx, |editor, cx| editor.display_text(cx)),
 995                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
 996            );
 997            assert_eq!(
 998                search_view
 999                    .results_editor
1000                    .update(cx, |editor, cx| editor.all_background_highlights(cx)),
1001                &[
1002                    (
1003                        DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
1004                        Color::red()
1005                    ),
1006                    (
1007                        DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
1008                        Color::red()
1009                    ),
1010                    (
1011                        DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
1012                        Color::red()
1013                    )
1014                ]
1015            );
1016            assert_eq!(search_view.active_match_index, Some(0));
1017            assert_eq!(
1018                search_view
1019                    .results_editor
1020                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1021                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1022            );
1023
1024            search_view.select_match(Direction::Next, cx);
1025        });
1026
1027        search_view.update(cx, |search_view, cx| {
1028            assert_eq!(search_view.active_match_index, Some(1));
1029            assert_eq!(
1030                search_view
1031                    .results_editor
1032                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1033                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1034            );
1035            search_view.select_match(Direction::Next, cx);
1036        });
1037
1038        search_view.update(cx, |search_view, cx| {
1039            assert_eq!(search_view.active_match_index, Some(2));
1040            assert_eq!(
1041                search_view
1042                    .results_editor
1043                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1044                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1045            );
1046            search_view.select_match(Direction::Next, cx);
1047        });
1048
1049        search_view.update(cx, |search_view, cx| {
1050            assert_eq!(search_view.active_match_index, Some(0));
1051            assert_eq!(
1052                search_view
1053                    .results_editor
1054                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1055                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1056            );
1057            search_view.select_match(Direction::Prev, cx);
1058        });
1059
1060        search_view.update(cx, |search_view, cx| {
1061            assert_eq!(search_view.active_match_index, Some(2));
1062            assert_eq!(
1063                search_view
1064                    .results_editor
1065                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1066                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1067            );
1068            search_view.select_match(Direction::Prev, cx);
1069        });
1070
1071        search_view.update(cx, |search_view, cx| {
1072            assert_eq!(search_view.active_match_index, Some(1));
1073            assert_eq!(
1074                search_view
1075                    .results_editor
1076                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1077                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1078            );
1079        });
1080    }
1081}