project_search.rs

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