project_search.rs

   1use crate::{
   2    SearchOption, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex,
   3    ToggleWholeWord,
   4};
   5use anyhow::Result;
   6use collections::HashMap;
   7use editor::{
   8    items::active_match_index, scroll::autoscroll::Autoscroll, Anchor, Editor, MultiBuffer,
   9    SelectAll, MAX_TAB_TITLE_LEN,
  10};
  11use futures::StreamExt;
  12use globset::{Glob, GlobMatcher};
  13use gpui::{
  14    actions,
  15    elements::*,
  16    platform::{CursorStyle, MouseButton},
  17    Action, AnyElement, AnyViewHandle, AppContext, Entity, ModelContext, ModelHandle, Subscription,
  18    Task, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
  19};
  20use menu::Confirm;
  21use project::{search::SearchQuery, Project};
  22use smallvec::SmallVec;
  23use std::{
  24    any::{Any, TypeId},
  25    borrow::Cow,
  26    collections::HashSet,
  27    mem,
  28    ops::{Not, Range},
  29    path::PathBuf,
  30    sync::Arc,
  31};
  32use util::ResultExt as _;
  33use workspace::{
  34    item::{BreadcrumbText, Item, ItemEvent, ItemHandle},
  35    searchable::{Direction, SearchableItem, SearchableItemHandle},
  36    ItemNavHistory, Pane, ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceId,
  37};
  38
  39actions!(project_search, [SearchInNew, ToggleFocus, NextField]);
  40
  41#[derive(Default)]
  42struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
  43
  44pub fn init(cx: &mut AppContext) {
  45    cx.set_global(ActiveSearches::default());
  46    cx.add_action(ProjectSearchView::deploy);
  47    cx.add_action(ProjectSearchView::move_focus_to_results);
  48    cx.add_action(ProjectSearchBar::search);
  49    cx.add_action(ProjectSearchBar::search_in_new);
  50    cx.add_action(ProjectSearchBar::select_next_match);
  51    cx.add_action(ProjectSearchBar::select_prev_match);
  52    cx.capture_action(ProjectSearchBar::tab);
  53    cx.capture_action(ProjectSearchBar::tab_previous);
  54    add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);
  55    add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);
  56    add_toggle_option_action::<ToggleRegex>(SearchOption::Regex, cx);
  57}
  58
  59fn add_toggle_option_action<A: Action>(option: SearchOption, cx: &mut AppContext) {
  60    cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext<Pane>| {
  61        if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<ProjectSearchBar>() {
  62            if search_bar.update(cx, |search_bar, cx| {
  63                search_bar.toggle_search_option(option, cx)
  64            }) {
  65                return;
  66            }
  67        }
  68        cx.propagate_action();
  69    });
  70}
  71
  72struct ProjectSearch {
  73    project: ModelHandle<Project>,
  74    excerpts: ModelHandle<MultiBuffer>,
  75    pending_search: Option<Task<Option<()>>>,
  76    match_ranges: Vec<Range<Anchor>>,
  77    active_query: Option<SearchQuery>,
  78    search_id: usize,
  79}
  80
  81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  82enum InputPanel {
  83    Query,
  84    Exclude,
  85    Include,
  86}
  87
  88pub struct ProjectSearchView {
  89    model: ModelHandle<ProjectSearch>,
  90    query_editor: ViewHandle<Editor>,
  91    results_editor: ViewHandle<Editor>,
  92    case_sensitive: bool,
  93    whole_word: bool,
  94    regex: bool,
  95    panels_with_errors: HashSet<InputPanel>,
  96    active_match_index: Option<usize>,
  97    search_id: usize,
  98    query_editor_was_focused: bool,
  99    included_files_editor: ViewHandle<Editor>,
 100    excluded_files_editor: ViewHandle<Editor>,
 101}
 102
 103pub struct ProjectSearchBar {
 104    active_project_search: Option<ViewHandle<ProjectSearchView>>,
 105    subscription: Option<Subscription>,
 106}
 107
 108impl Entity for ProjectSearch {
 109    type Event = ();
 110}
 111
 112impl ProjectSearch {
 113    fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
 114        let replica_id = project.read(cx).replica_id();
 115        Self {
 116            project,
 117            excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
 118            pending_search: Default::default(),
 119            match_ranges: Default::default(),
 120            active_query: None,
 121            search_id: 0,
 122        }
 123    }
 124
 125    fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
 126        cx.add_model(|cx| Self {
 127            project: self.project.clone(),
 128            excerpts: self
 129                .excerpts
 130                .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
 131            pending_search: Default::default(),
 132            match_ranges: self.match_ranges.clone(),
 133            active_query: self.active_query.clone(),
 134            search_id: self.search_id,
 135        })
 136    }
 137
 138    fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
 139        let search = self
 140            .project
 141            .update(cx, |project, cx| project.search(query.clone(), cx));
 142        self.search_id += 1;
 143        self.active_query = Some(query);
 144        self.match_ranges.clear();
 145        self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
 146            let matches = search.await.log_err()?;
 147            let this = this.upgrade(&cx)?;
 148            let mut matches = matches.into_iter().collect::<Vec<_>>();
 149            let (_task, mut match_ranges) = this.update(&mut cx, |this, cx| {
 150                this.match_ranges.clear();
 151                matches.sort_by_key(|(buffer, _)| buffer.read(cx).file().map(|file| file.path()));
 152                this.excerpts.update(cx, |excerpts, cx| {
 153                    excerpts.clear(cx);
 154                    excerpts.stream_excerpts_with_context_lines(matches, 1, cx)
 155                })
 156            });
 157
 158            while let Some(match_range) = match_ranges.next().await {
 159                this.update(&mut cx, |this, cx| {
 160                    this.match_ranges.push(match_range);
 161                    while let Ok(Some(match_range)) = match_ranges.try_next() {
 162                        this.match_ranges.push(match_range);
 163                    }
 164                    cx.notify();
 165                });
 166            }
 167
 168            this.update(&mut cx, |this, cx| {
 169                this.pending_search.take();
 170                cx.notify();
 171            });
 172
 173            None
 174        }));
 175        cx.notify();
 176    }
 177}
 178
 179pub enum ViewEvent {
 180    UpdateTab,
 181    Activate,
 182    EditorEvent(editor::Event),
 183}
 184
 185impl Entity for ProjectSearchView {
 186    type Event = ViewEvent;
 187}
 188
 189impl View for ProjectSearchView {
 190    fn ui_name() -> &'static str {
 191        "ProjectSearchView"
 192    }
 193
 194    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
 195        let model = &self.model.read(cx);
 196        if model.match_ranges.is_empty() {
 197            enum Status {}
 198
 199            let theme = theme::current(cx).clone();
 200            let text = if self.query_editor.read(cx).text(cx).is_empty() {
 201                ""
 202            } else if model.pending_search.is_some() {
 203                "Searching..."
 204            } else {
 205                "No results"
 206            };
 207            MouseEventHandler::<Status, _>::new(0, cx, |_, _| {
 208                Label::new(text, theme.search.results_status.clone())
 209                    .aligned()
 210                    .contained()
 211                    .with_background_color(theme.editor.background)
 212                    .flex(1., true)
 213            })
 214            .on_down(MouseButton::Left, |_, _, cx| {
 215                cx.focus_parent();
 216            })
 217            .into_any_named("project search view")
 218        } else {
 219            ChildView::new(&self.results_editor, cx)
 220                .flex(1., true)
 221                .into_any_named("project search view")
 222        }
 223    }
 224
 225    fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
 226        let handle = cx.weak_handle();
 227        cx.update_global(|state: &mut ActiveSearches, cx| {
 228            state
 229                .0
 230                .insert(self.model.read(cx).project.downgrade(), handle)
 231        });
 232
 233        if cx.is_self_focused() {
 234            if self.query_editor_was_focused {
 235                cx.focus(&self.query_editor);
 236            } else {
 237                cx.focus(&self.results_editor);
 238            }
 239        }
 240    }
 241}
 242
 243impl Item for ProjectSearchView {
 244    fn tab_tooltip_text(&self, cx: &AppContext) -> Option<Cow<str>> {
 245        let query_text = self.query_editor.read(cx).text(cx);
 246
 247        query_text
 248            .is_empty()
 249            .not()
 250            .then(|| query_text.into())
 251            .or_else(|| Some("Project Search".into()))
 252    }
 253
 254    fn act_as_type<'a>(
 255        &'a self,
 256        type_id: TypeId,
 257        self_handle: &'a ViewHandle<Self>,
 258        _: &'a AppContext,
 259    ) -> Option<&'a AnyViewHandle> {
 260        if type_id == TypeId::of::<Self>() {
 261            Some(self_handle)
 262        } else if type_id == TypeId::of::<Editor>() {
 263            Some(&self.results_editor)
 264        } else {
 265            None
 266        }
 267    }
 268
 269    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
 270        self.results_editor
 271            .update(cx, |editor, cx| editor.deactivated(cx));
 272    }
 273
 274    fn tab_content<T: View>(
 275        &self,
 276        _detail: Option<usize>,
 277        tab_theme: &theme::Tab,
 278        cx: &AppContext,
 279    ) -> AnyElement<T> {
 280        Flex::row()
 281            .with_child(
 282                Svg::new("icons/magnifying_glass_12.svg")
 283                    .with_color(tab_theme.label.text.color)
 284                    .constrained()
 285                    .with_width(tab_theme.type_icon_width)
 286                    .aligned()
 287                    .contained()
 288                    .with_margin_right(tab_theme.spacing),
 289            )
 290            .with_children(self.model.read(cx).active_query.as_ref().map(|query| {
 291                let query_text = util::truncate_and_trailoff(query.as_str(), MAX_TAB_TITLE_LEN);
 292
 293                Label::new(query_text, tab_theme.label.clone()).aligned()
 294            }))
 295            .into_any()
 296    }
 297
 298    fn for_each_project_item(&self, cx: &AppContext, f: &mut dyn FnMut(usize, &dyn project::Item)) {
 299        self.results_editor.for_each_project_item(cx, f)
 300    }
 301
 302    fn is_singleton(&self, _: &AppContext) -> bool {
 303        false
 304    }
 305
 306    fn can_save(&self, _: &AppContext) -> bool {
 307        true
 308    }
 309
 310    fn is_dirty(&self, cx: &AppContext) -> bool {
 311        self.results_editor.read(cx).is_dirty(cx)
 312    }
 313
 314    fn has_conflict(&self, cx: &AppContext) -> bool {
 315        self.results_editor.read(cx).has_conflict(cx)
 316    }
 317
 318    fn save(
 319        &mut self,
 320        project: ModelHandle<Project>,
 321        cx: &mut ViewContext<Self>,
 322    ) -> Task<anyhow::Result<()>> {
 323        self.results_editor
 324            .update(cx, |editor, cx| editor.save(project, cx))
 325    }
 326
 327    fn save_as(
 328        &mut self,
 329        _: ModelHandle<Project>,
 330        _: PathBuf,
 331        _: &mut ViewContext<Self>,
 332    ) -> Task<anyhow::Result<()>> {
 333        unreachable!("save_as should not have been called")
 334    }
 335
 336    fn reload(
 337        &mut self,
 338        project: ModelHandle<Project>,
 339        cx: &mut ViewContext<Self>,
 340    ) -> Task<anyhow::Result<()>> {
 341        self.results_editor
 342            .update(cx, |editor, cx| editor.reload(project, cx))
 343    }
 344
 345    fn clone_on_split(&self, _workspace_id: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self>
 346    where
 347        Self: Sized,
 348    {
 349        let model = self.model.update(cx, |model, cx| model.clone(cx));
 350        Some(Self::new(model, cx))
 351    }
 352
 353    fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
 354        self.results_editor
 355            .update(cx, |editor, cx| editor.added_to_workspace(workspace, cx));
 356    }
 357
 358    fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
 359        self.results_editor.update(cx, |editor, _| {
 360            editor.set_nav_history(Some(nav_history));
 361        });
 362    }
 363
 364    fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
 365        self.results_editor
 366            .update(cx, |editor, cx| editor.navigate(data, cx))
 367    }
 368
 369    fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
 370        match event {
 371            ViewEvent::UpdateTab => {
 372                smallvec::smallvec![ItemEvent::UpdateBreadcrumbs, ItemEvent::UpdateTab]
 373            }
 374            ViewEvent::EditorEvent(editor_event) => Editor::to_item_events(editor_event),
 375            _ => SmallVec::new(),
 376        }
 377    }
 378
 379    fn breadcrumb_location(&self) -> ToolbarItemLocation {
 380        if self.has_matches() {
 381            ToolbarItemLocation::Secondary
 382        } else {
 383            ToolbarItemLocation::Hidden
 384        }
 385    }
 386
 387    fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
 388        self.results_editor.breadcrumbs(theme, cx)
 389    }
 390
 391    fn serialized_item_kind() -> Option<&'static str> {
 392        None
 393    }
 394
 395    fn deserialize(
 396        _project: ModelHandle<Project>,
 397        _workspace: WeakViewHandle<Workspace>,
 398        _workspace_id: workspace::WorkspaceId,
 399        _item_id: workspace::ItemId,
 400        _cx: &mut ViewContext<Pane>,
 401    ) -> Task<anyhow::Result<ViewHandle<Self>>> {
 402        unimplemented!()
 403    }
 404}
 405
 406impl ProjectSearchView {
 407    fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
 408        let project;
 409        let excerpts;
 410        let mut query_text = String::new();
 411        let mut regex = false;
 412        let mut case_sensitive = false;
 413        let mut whole_word = false;
 414
 415        {
 416            let model = model.read(cx);
 417            project = model.project.clone();
 418            excerpts = model.excerpts.clone();
 419            if let Some(active_query) = model.active_query.as_ref() {
 420                query_text = active_query.as_str().to_string();
 421                regex = active_query.is_regex();
 422                case_sensitive = active_query.case_sensitive();
 423                whole_word = active_query.whole_word();
 424            }
 425        }
 426        cx.observe(&model, |this, _, cx| this.model_changed(cx))
 427            .detach();
 428
 429        let query_editor = cx.add_view(|cx| {
 430            let mut editor = Editor::single_line(
 431                Some(Arc::new(|theme| theme.search.editor.input.clone())),
 432                cx,
 433            );
 434            editor.set_text(query_text, cx);
 435            editor
 436        });
 437        // Subscribe to query_editor in order to reraise editor events for workspace item activation purposes
 438        cx.subscribe(&query_editor, |_, _, event, cx| {
 439            cx.emit(ViewEvent::EditorEvent(event.clone()))
 440        })
 441        .detach();
 442
 443        let results_editor = cx.add_view(|cx| {
 444            let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
 445            editor.set_searchable(false);
 446            editor
 447        });
 448        cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
 449            .detach();
 450
 451        cx.subscribe(&results_editor, |this, _, event, cx| {
 452            if matches!(event, editor::Event::SelectionsChanged { .. }) {
 453                this.update_match_index(cx);
 454            }
 455            // Reraise editor events for workspace item activation purposes
 456            cx.emit(ViewEvent::EditorEvent(event.clone()));
 457        })
 458        .detach();
 459
 460        let included_files_editor = cx.add_view(|cx| {
 461            let mut editor = Editor::single_line(
 462                Some(Arc::new(|theme| {
 463                    theme.search.include_exclude_editor.input.clone()
 464                })),
 465                cx,
 466            );
 467            editor.set_placeholder_text("Include: crates/**/*.toml", cx);
 468
 469            editor
 470        });
 471        // Subscribe to include_files_editor in order to reraise editor events for workspace item activation purposes
 472        cx.subscribe(&included_files_editor, |_, _, event, cx| {
 473            cx.emit(ViewEvent::EditorEvent(event.clone()))
 474        })
 475        .detach();
 476
 477        let excluded_files_editor = cx.add_view(|cx| {
 478            let mut editor = Editor::single_line(
 479                Some(Arc::new(|theme| {
 480                    theme.search.include_exclude_editor.input.clone()
 481                })),
 482                cx,
 483            );
 484            editor.set_placeholder_text("Exclude: vendor/*, *.lock", cx);
 485
 486            editor
 487        });
 488        // Subscribe to excluded_files_editor in order to reraise editor events for workspace item activation purposes
 489        cx.subscribe(&excluded_files_editor, |_, _, event, cx| {
 490            cx.emit(ViewEvent::EditorEvent(event.clone()))
 491        })
 492        .detach();
 493
 494        let mut this = ProjectSearchView {
 495            search_id: model.read(cx).search_id,
 496            model,
 497            query_editor,
 498            results_editor,
 499            case_sensitive,
 500            whole_word,
 501            regex,
 502            panels_with_errors: HashSet::new(),
 503            active_match_index: None,
 504            query_editor_was_focused: false,
 505            included_files_editor,
 506            excluded_files_editor,
 507        };
 508        this.model_changed(cx);
 509        this
 510    }
 511
 512    // Re-activate the most recently activated search or the most recent if it has been closed.
 513    // If no search exists in the workspace, create a new one.
 514    fn deploy(
 515        workspace: &mut Workspace,
 516        _: &workspace::NewSearch,
 517        cx: &mut ViewContext<Workspace>,
 518    ) {
 519        // Clean up entries for dropped projects
 520        cx.update_global(|state: &mut ActiveSearches, cx| {
 521            state.0.retain(|project, _| project.is_upgradable(cx))
 522        });
 523
 524        let active_search = cx
 525            .global::<ActiveSearches>()
 526            .0
 527            .get(&workspace.project().downgrade());
 528
 529        let existing = active_search
 530            .and_then(|active_search| {
 531                workspace
 532                    .items_of_type::<ProjectSearchView>(cx)
 533                    .find(|search| search == active_search)
 534            })
 535            .or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
 536
 537        let query = workspace.active_item(cx).and_then(|item| {
 538            let editor = item.act_as::<Editor>(cx)?;
 539            let query = editor.query_suggestion(cx);
 540            if query.is_empty() {
 541                None
 542            } else {
 543                Some(query)
 544            }
 545        });
 546
 547        let search = if let Some(existing) = existing {
 548            workspace.activate_item(&existing, cx);
 549            existing
 550        } else {
 551            let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
 552            let view = cx.add_view(|cx| ProjectSearchView::new(model, cx));
 553            workspace.add_item(Box::new(view.clone()), cx);
 554            view
 555        };
 556
 557        search.update(cx, |search, cx| {
 558            if let Some(query) = query {
 559                search.set_query(&query, cx);
 560            }
 561            search.focus_query_editor(cx)
 562        });
 563    }
 564
 565    fn search(&mut self, cx: &mut ViewContext<Self>) {
 566        if let Some(query) = self.build_search_query(cx) {
 567            self.model.update(cx, |model, cx| model.search(query, cx));
 568        }
 569    }
 570
 571    fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
 572        let text = self.query_editor.read(cx).text(cx);
 573        let included_files =
 574            match Self::load_glob_set(&self.included_files_editor.read(cx).text(cx)) {
 575                Ok(included_files) => {
 576                    self.panels_with_errors.remove(&InputPanel::Include);
 577                    included_files
 578                }
 579                Err(_e) => {
 580                    self.panels_with_errors.insert(InputPanel::Include);
 581                    cx.notify();
 582                    return None;
 583                }
 584            };
 585        let excluded_files =
 586            match Self::load_glob_set(&self.excluded_files_editor.read(cx).text(cx)) {
 587                Ok(excluded_files) => {
 588                    self.panels_with_errors.remove(&InputPanel::Exclude);
 589                    excluded_files
 590                }
 591                Err(_e) => {
 592                    self.panels_with_errors.insert(InputPanel::Exclude);
 593                    cx.notify();
 594                    return None;
 595                }
 596            };
 597        if self.regex {
 598            match SearchQuery::regex(
 599                text,
 600                self.whole_word,
 601                self.case_sensitive,
 602                included_files,
 603                excluded_files,
 604            ) {
 605                Ok(query) => {
 606                    self.panels_with_errors.remove(&InputPanel::Query);
 607                    Some(query)
 608                }
 609                Err(_e) => {
 610                    self.panels_with_errors.insert(InputPanel::Query);
 611                    cx.notify();
 612                    None
 613                }
 614            }
 615        } else {
 616            Some(SearchQuery::text(
 617                text,
 618                self.whole_word,
 619                self.case_sensitive,
 620                included_files,
 621                excluded_files,
 622            ))
 623        }
 624    }
 625
 626    fn load_glob_set(text: &str) -> Result<Vec<GlobMatcher>> {
 627        text.split(',')
 628            .map(str::trim)
 629            .filter(|glob_str| !glob_str.is_empty())
 630            .map(|glob_str| anyhow::Ok(Glob::new(glob_str)?.compile_matcher()))
 631            .collect()
 632    }
 633
 634    fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
 635        if let Some(index) = self.active_match_index {
 636            let match_ranges = self.model.read(cx).match_ranges.clone();
 637            let new_index = self.results_editor.update(cx, |editor, cx| {
 638                editor.match_index_for_direction(&match_ranges, index, direction, cx)
 639            });
 640
 641            let range_to_select = match_ranges[new_index].clone();
 642            self.results_editor.update(cx, |editor, cx| {
 643                editor.unfold_ranges([range_to_select.clone()], false, true, cx);
 644                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 645                    s.select_ranges([range_to_select])
 646                });
 647            });
 648        }
 649    }
 650
 651    fn focus_query_editor(&mut self, cx: &mut ViewContext<Self>) {
 652        self.query_editor.update(cx, |query_editor, cx| {
 653            query_editor.select_all(&SelectAll, cx);
 654        });
 655        self.query_editor_was_focused = true;
 656        cx.focus(&self.query_editor);
 657    }
 658
 659    fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
 660        self.query_editor
 661            .update(cx, |query_editor, cx| query_editor.set_text(query, cx));
 662    }
 663
 664    fn focus_results_editor(&mut self, cx: &mut ViewContext<Self>) {
 665        self.query_editor.update(cx, |query_editor, cx| {
 666            let cursor = query_editor.selections.newest_anchor().head();
 667            query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor]));
 668        });
 669        self.query_editor_was_focused = false;
 670        cx.focus(&self.results_editor);
 671    }
 672
 673    fn model_changed(&mut self, cx: &mut ViewContext<Self>) {
 674        let match_ranges = self.model.read(cx).match_ranges.clone();
 675        if match_ranges.is_empty() {
 676            self.active_match_index = None;
 677        } else {
 678            let prev_search_id = mem::replace(&mut self.search_id, self.model.read(cx).search_id);
 679            let is_new_search = self.search_id != prev_search_id;
 680            self.results_editor.update(cx, |editor, cx| {
 681                if is_new_search {
 682                    editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 683                        s.select_ranges(match_ranges.first().cloned())
 684                    });
 685                }
 686                editor.highlight_background::<Self>(
 687                    match_ranges,
 688                    |theme| theme.search.match_background,
 689                    cx,
 690                );
 691            });
 692            if is_new_search && self.query_editor.is_focused(cx) {
 693                self.focus_results_editor(cx);
 694            }
 695        }
 696
 697        cx.emit(ViewEvent::UpdateTab);
 698        cx.notify();
 699    }
 700
 701    fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
 702        let results_editor = self.results_editor.read(cx);
 703        let new_index = active_match_index(
 704            &self.model.read(cx).match_ranges,
 705            &results_editor.selections.newest_anchor().head(),
 706            &results_editor.buffer().read(cx).snapshot(cx),
 707        );
 708        if self.active_match_index != new_index {
 709            self.active_match_index = new_index;
 710            cx.notify();
 711        }
 712    }
 713
 714    pub fn has_matches(&self) -> bool {
 715        self.active_match_index.is_some()
 716    }
 717
 718    fn move_focus_to_results(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext<Pane>) {
 719        if let Some(search_view) = pane
 720            .active_item()
 721            .and_then(|item| item.downcast::<ProjectSearchView>())
 722        {
 723            search_view.update(cx, |search_view, cx| {
 724                if !search_view.results_editor.is_focused(cx)
 725                    && !search_view.model.read(cx).match_ranges.is_empty()
 726                {
 727                    return search_view.focus_results_editor(cx);
 728                }
 729            });
 730        }
 731
 732        cx.propagate_action();
 733    }
 734}
 735
 736impl Default for ProjectSearchBar {
 737    fn default() -> Self {
 738        Self::new()
 739    }
 740}
 741
 742impl ProjectSearchBar {
 743    pub fn new() -> Self {
 744        Self {
 745            active_project_search: Default::default(),
 746            subscription: Default::default(),
 747        }
 748    }
 749
 750    fn search(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
 751        if let Some(search_view) = self.active_project_search.as_ref() {
 752            search_view.update(cx, |search_view, cx| search_view.search(cx));
 753        }
 754    }
 755
 756    fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
 757        if let Some(search_view) = workspace
 758            .active_item(cx)
 759            .and_then(|item| item.downcast::<ProjectSearchView>())
 760        {
 761            let new_query = search_view.update(cx, |search_view, cx| {
 762                let new_query = search_view.build_search_query(cx);
 763                if new_query.is_some() {
 764                    if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
 765                        search_view.query_editor.update(cx, |editor, cx| {
 766                            editor.set_text(old_query.as_str(), cx);
 767                        });
 768                        search_view.regex = old_query.is_regex();
 769                        search_view.whole_word = old_query.whole_word();
 770                        search_view.case_sensitive = old_query.case_sensitive();
 771                    }
 772                }
 773                new_query
 774            });
 775            if let Some(new_query) = new_query {
 776                let model = cx.add_model(|cx| {
 777                    let mut model = ProjectSearch::new(workspace.project().clone(), cx);
 778                    model.search(new_query, cx);
 779                    model
 780                });
 781                workspace.add_item(
 782                    Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
 783                    cx,
 784                );
 785            }
 786        }
 787    }
 788
 789    fn select_next_match(pane: &mut Pane, _: &SelectNextMatch, cx: &mut ViewContext<Pane>) {
 790        if let Some(search_view) = pane
 791            .active_item()
 792            .and_then(|item| item.downcast::<ProjectSearchView>())
 793        {
 794            search_view.update(cx, |view, cx| view.select_match(Direction::Next, cx));
 795        } else {
 796            cx.propagate_action();
 797        }
 798    }
 799
 800    fn select_prev_match(pane: &mut Pane, _: &SelectPrevMatch, cx: &mut ViewContext<Pane>) {
 801        if let Some(search_view) = pane
 802            .active_item()
 803            .and_then(|item| item.downcast::<ProjectSearchView>())
 804        {
 805            search_view.update(cx, |view, cx| view.select_match(Direction::Prev, cx));
 806        } else {
 807            cx.propagate_action();
 808        }
 809    }
 810
 811    fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
 812        self.cycle_field(Direction::Next, cx);
 813    }
 814
 815    fn tab_previous(&mut self, _: &editor::TabPrev, cx: &mut ViewContext<Self>) {
 816        self.cycle_field(Direction::Prev, cx);
 817    }
 818
 819    fn cycle_field(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
 820        let active_project_search = match &self.active_project_search {
 821            Some(active_project_search) => active_project_search,
 822
 823            None => {
 824                cx.propagate_action();
 825                return;
 826            }
 827        };
 828
 829        active_project_search.update(cx, |project_view, cx| {
 830            let views = &[
 831                &project_view.query_editor,
 832                &project_view.included_files_editor,
 833                &project_view.excluded_files_editor,
 834            ];
 835
 836            let current_index = match views
 837                .iter()
 838                .enumerate()
 839                .find(|(_, view)| view.is_focused(cx))
 840            {
 841                Some((index, _)) => index,
 842
 843                None => {
 844                    cx.propagate_action();
 845                    return;
 846                }
 847            };
 848
 849            let new_index = match direction {
 850                Direction::Next => (current_index + 1) % views.len(),
 851                Direction::Prev if current_index == 0 => views.len() - 1,
 852                Direction::Prev => (current_index - 1) % views.len(),
 853            };
 854            cx.focus(views[new_index]);
 855        });
 856    }
 857
 858    fn toggle_search_option(&mut self, option: SearchOption, cx: &mut ViewContext<Self>) -> bool {
 859        if let Some(search_view) = self.active_project_search.as_ref() {
 860            search_view.update(cx, |search_view, cx| {
 861                let value = match option {
 862                    SearchOption::WholeWord => &mut search_view.whole_word,
 863                    SearchOption::CaseSensitive => &mut search_view.case_sensitive,
 864                    SearchOption::Regex => &mut search_view.regex,
 865                };
 866                *value = !*value;
 867                search_view.search(cx);
 868            });
 869            cx.notify();
 870            true
 871        } else {
 872            false
 873        }
 874    }
 875
 876    fn render_nav_button(
 877        &self,
 878        icon: &'static str,
 879        direction: Direction,
 880        cx: &mut ViewContext<Self>,
 881    ) -> AnyElement<Self> {
 882        let action: Box<dyn Action>;
 883        let tooltip;
 884        match direction {
 885            Direction::Prev => {
 886                action = Box::new(SelectPrevMatch);
 887                tooltip = "Select Previous Match";
 888            }
 889            Direction::Next => {
 890                action = Box::new(SelectNextMatch);
 891                tooltip = "Select Next Match";
 892            }
 893        };
 894        let tooltip_style = theme::current(cx).tooltip.clone();
 895
 896        enum NavButton {}
 897        MouseEventHandler::<NavButton, _>::new(direction as usize, cx, |state, cx| {
 898            let theme = theme::current(cx);
 899            let style = theme.search.option_button.inactive_state().style_for(state);
 900            Label::new(icon, style.text.clone())
 901                .contained()
 902                .with_style(style.container)
 903        })
 904        .on_click(MouseButton::Left, move |_, this, cx| {
 905            if let Some(search) = this.active_project_search.as_ref() {
 906                search.update(cx, |search, cx| search.select_match(direction, cx));
 907            }
 908        })
 909        .with_cursor_style(CursorStyle::PointingHand)
 910        .with_tooltip::<NavButton>(
 911            direction as usize,
 912            tooltip.to_string(),
 913            Some(action),
 914            tooltip_style,
 915            cx,
 916        )
 917        .into_any()
 918    }
 919
 920    fn render_option_button(
 921        &self,
 922        icon: &'static str,
 923        option: SearchOption,
 924        cx: &mut ViewContext<Self>,
 925    ) -> AnyElement<Self> {
 926        let tooltip_style = theme::current(cx).tooltip.clone();
 927        let is_active = self.is_option_enabled(option, cx);
 928        MouseEventHandler::<Self, _>::new(option as usize, cx, |state, cx| {
 929            let theme = theme::current(cx);
 930            let style = theme
 931                .search
 932                .option_button
 933                .in_state(is_active)
 934                .style_for(state);
 935            Label::new(icon, style.text.clone())
 936                .contained()
 937                .with_style(style.container)
 938        })
 939        .on_click(MouseButton::Left, move |_, this, cx| {
 940            this.toggle_search_option(option, cx);
 941        })
 942        .with_cursor_style(CursorStyle::PointingHand)
 943        .with_tooltip::<Self>(
 944            option as usize,
 945            format!("Toggle {}", option.label()),
 946            Some(option.to_toggle_action()),
 947            tooltip_style,
 948            cx,
 949        )
 950        .into_any()
 951    }
 952
 953    fn is_option_enabled(&self, option: SearchOption, cx: &AppContext) -> bool {
 954        if let Some(search) = self.active_project_search.as_ref() {
 955            let search = search.read(cx);
 956            match option {
 957                SearchOption::WholeWord => search.whole_word,
 958                SearchOption::CaseSensitive => search.case_sensitive,
 959                SearchOption::Regex => search.regex,
 960            }
 961        } else {
 962            false
 963        }
 964    }
 965}
 966
 967impl Entity for ProjectSearchBar {
 968    type Event = ();
 969}
 970
 971impl View for ProjectSearchBar {
 972    fn ui_name() -> &'static str {
 973        "ProjectSearchBar"
 974    }
 975
 976    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
 977        if let Some(search) = self.active_project_search.as_ref() {
 978            let search = search.read(cx);
 979            let theme = theme::current(cx).clone();
 980            let query_container_style = if search.panels_with_errors.contains(&InputPanel::Query) {
 981                theme.search.invalid_editor
 982            } else {
 983                theme.search.editor.input.container
 984            };
 985            let include_container_style =
 986                if search.panels_with_errors.contains(&InputPanel::Include) {
 987                    theme.search.invalid_include_exclude_editor
 988                } else {
 989                    theme.search.include_exclude_editor.input.container
 990                };
 991            let exclude_container_style =
 992                if search.panels_with_errors.contains(&InputPanel::Exclude) {
 993                    theme.search.invalid_include_exclude_editor
 994                } else {
 995                    theme.search.include_exclude_editor.input.container
 996                };
 997
 998            let included_files_view = ChildView::new(&search.included_files_editor, cx)
 999                .aligned()
1000                .left()
1001                .flex(1.0, true);
1002            let excluded_files_view = ChildView::new(&search.excluded_files_editor, cx)
1003                .aligned()
1004                .right()
1005                .flex(1.0, true);
1006
1007            let row_spacing = theme.workspace.toolbar.container.padding.bottom;
1008
1009            Flex::column()
1010                .with_child(
1011                    Flex::row()
1012                        .with_child(
1013                            Flex::row()
1014                                .with_child(
1015                                    ChildView::new(&search.query_editor, cx)
1016                                        .aligned()
1017                                        .left()
1018                                        .flex(1., true),
1019                                )
1020                                .with_children(search.active_match_index.map(|match_ix| {
1021                                    Label::new(
1022                                        format!(
1023                                            "{}/{}",
1024                                            match_ix + 1,
1025                                            search.model.read(cx).match_ranges.len()
1026                                        ),
1027                                        theme.search.match_index.text.clone(),
1028                                    )
1029                                    .contained()
1030                                    .with_style(theme.search.match_index.container)
1031                                    .aligned()
1032                                }))
1033                                .contained()
1034                                .with_style(query_container_style)
1035                                .aligned()
1036                                .constrained()
1037                                .with_min_width(theme.search.editor.min_width)
1038                                .with_max_width(theme.search.editor.max_width)
1039                                .flex(1., false),
1040                        )
1041                        .with_child(
1042                            Flex::row()
1043                                .with_child(self.render_nav_button("<", Direction::Prev, cx))
1044                                .with_child(self.render_nav_button(">", Direction::Next, cx))
1045                                .aligned(),
1046                        )
1047                        .with_child(
1048                            Flex::row()
1049                                .with_child(self.render_option_button(
1050                                    "Case",
1051                                    SearchOption::CaseSensitive,
1052                                    cx,
1053                                ))
1054                                .with_child(self.render_option_button(
1055                                    "Word",
1056                                    SearchOption::WholeWord,
1057                                    cx,
1058                                ))
1059                                .with_child(self.render_option_button(
1060                                    "Regex",
1061                                    SearchOption::Regex,
1062                                    cx,
1063                                ))
1064                                .contained()
1065                                .with_style(theme.search.option_button_group)
1066                                .aligned(),
1067                        )
1068                        .contained()
1069                        .with_margin_bottom(row_spacing),
1070                )
1071                .with_child(
1072                    Flex::row()
1073                        .with_child(
1074                            Flex::row()
1075                                .with_child(included_files_view)
1076                                .contained()
1077                                .with_style(include_container_style)
1078                                .aligned()
1079                                .constrained()
1080                                .with_min_width(theme.search.include_exclude_editor.min_width)
1081                                .with_max_width(theme.search.include_exclude_editor.max_width)
1082                                .flex(1., false),
1083                        )
1084                        .with_child(
1085                            Flex::row()
1086                                .with_child(excluded_files_view)
1087                                .contained()
1088                                .with_style(exclude_container_style)
1089                                .aligned()
1090                                .constrained()
1091                                .with_min_width(theme.search.include_exclude_editor.min_width)
1092                                .with_max_width(theme.search.include_exclude_editor.max_width)
1093                                .flex(1., false),
1094                        ),
1095                )
1096                .contained()
1097                .with_style(theme.search.container)
1098                .aligned()
1099                .left()
1100                .into_any_named("project search")
1101        } else {
1102            Empty::new().into_any()
1103        }
1104    }
1105}
1106
1107impl ToolbarItemView for ProjectSearchBar {
1108    fn set_active_pane_item(
1109        &mut self,
1110        active_pane_item: Option<&dyn ItemHandle>,
1111        cx: &mut ViewContext<Self>,
1112    ) -> ToolbarItemLocation {
1113        cx.notify();
1114        self.subscription = None;
1115        self.active_project_search = None;
1116        if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
1117            self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
1118            self.active_project_search = Some(search);
1119            ToolbarItemLocation::PrimaryLeft {
1120                flex: Some((1., false)),
1121            }
1122        } else {
1123            ToolbarItemLocation::Hidden
1124        }
1125    }
1126
1127    fn row_count(&self) -> usize {
1128        2
1129    }
1130}
1131
1132#[cfg(test)]
1133pub mod tests {
1134    use super::*;
1135    use editor::DisplayPoint;
1136    use gpui::{color::Color, executor::Deterministic, TestAppContext};
1137    use project::FakeFs;
1138    use serde_json::json;
1139    use settings::SettingsStore;
1140    use std::sync::Arc;
1141    use theme::ThemeSettings;
1142
1143    #[gpui::test]
1144    async fn test_project_search(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
1145        init_test(cx);
1146
1147        let fs = FakeFs::new(cx.background());
1148        fs.insert_tree(
1149            "/dir",
1150            json!({
1151                "one.rs": "const ONE: usize = 1;",
1152                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
1153                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
1154                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
1155            }),
1156        )
1157        .await;
1158        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
1159        let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
1160        let (_, search_view) = cx.add_window(|cx| ProjectSearchView::new(search.clone(), cx));
1161
1162        search_view.update(cx, |search_view, cx| {
1163            search_view
1164                .query_editor
1165                .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
1166            search_view.search(cx);
1167        });
1168        deterministic.run_until_parked();
1169        search_view.update(cx, |search_view, cx| {
1170            assert_eq!(
1171                search_view
1172                    .results_editor
1173                    .update(cx, |editor, cx| editor.display_text(cx)),
1174                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
1175            );
1176            assert_eq!(
1177                search_view
1178                    .results_editor
1179                    .update(cx, |editor, cx| editor.all_background_highlights(cx)),
1180                &[
1181                    (
1182                        DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
1183                        Color::red()
1184                    ),
1185                    (
1186                        DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
1187                        Color::red()
1188                    ),
1189                    (
1190                        DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
1191                        Color::red()
1192                    )
1193                ]
1194            );
1195            assert_eq!(search_view.active_match_index, Some(0));
1196            assert_eq!(
1197                search_view
1198                    .results_editor
1199                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1200                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1201            );
1202
1203            search_view.select_match(Direction::Next, cx);
1204        });
1205
1206        search_view.update(cx, |search_view, cx| {
1207            assert_eq!(search_view.active_match_index, Some(1));
1208            assert_eq!(
1209                search_view
1210                    .results_editor
1211                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1212                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1213            );
1214            search_view.select_match(Direction::Next, cx);
1215        });
1216
1217        search_view.update(cx, |search_view, cx| {
1218            assert_eq!(search_view.active_match_index, Some(2));
1219            assert_eq!(
1220                search_view
1221                    .results_editor
1222                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1223                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1224            );
1225            search_view.select_match(Direction::Next, cx);
1226        });
1227
1228        search_view.update(cx, |search_view, cx| {
1229            assert_eq!(search_view.active_match_index, Some(0));
1230            assert_eq!(
1231                search_view
1232                    .results_editor
1233                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1234                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1235            );
1236            search_view.select_match(Direction::Prev, cx);
1237        });
1238
1239        search_view.update(cx, |search_view, cx| {
1240            assert_eq!(search_view.active_match_index, Some(2));
1241            assert_eq!(
1242                search_view
1243                    .results_editor
1244                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1245                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1246            );
1247            search_view.select_match(Direction::Prev, cx);
1248        });
1249
1250        search_view.update(cx, |search_view, cx| {
1251            assert_eq!(search_view.active_match_index, Some(1));
1252            assert_eq!(
1253                search_view
1254                    .results_editor
1255                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1256                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1257            );
1258        });
1259    }
1260
1261    #[gpui::test]
1262    async fn test_project_search_focus(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
1263        init_test(cx);
1264
1265        let fs = FakeFs::new(cx.background());
1266        fs.insert_tree(
1267            "/dir",
1268            json!({
1269                "one.rs": "const ONE: usize = 1;",
1270                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
1271                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
1272                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
1273            }),
1274        )
1275        .await;
1276        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
1277        let (window_id, workspace) = cx.add_window(|cx| Workspace::test_new(project, cx));
1278
1279        let active_item = cx.read(|cx| {
1280            workspace
1281                .read(cx)
1282                .active_pane()
1283                .read(cx)
1284                .active_item()
1285                .and_then(|item| item.downcast::<ProjectSearchView>())
1286        });
1287        assert!(
1288            active_item.is_none(),
1289            "Expected no search panel to be active, but got: {active_item:?}"
1290        );
1291
1292        workspace.update(cx, |workspace, cx| {
1293            ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx)
1294        });
1295
1296        let Some(search_view) = cx.read(|cx| {
1297            workspace
1298                .read(cx)
1299                .active_pane()
1300                .read(cx)
1301                .active_item()
1302                .and_then(|item| item.downcast::<ProjectSearchView>())
1303        }) else {
1304            panic!("Search view expected to appear after new search event trigger")
1305        };
1306        let search_view_id = search_view.id();
1307
1308        cx.spawn(
1309            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1310        )
1311        .detach();
1312        deterministic.run_until_parked();
1313        search_view.update(cx, |search_view, cx| {
1314            assert!(
1315                search_view.query_editor.is_focused(cx),
1316                "Empty search view should be focused after the toggle focus event: no results panel to focus on",
1317            );
1318        });
1319
1320        search_view.update(cx, |search_view, cx| {
1321            let query_editor = &search_view.query_editor;
1322            assert!(
1323                query_editor.is_focused(cx),
1324                "Search view should be focused after the new search view is activated",
1325            );
1326            let query_text = query_editor.read(cx).text(cx);
1327            assert!(
1328                query_text.is_empty(),
1329                "New search query should be empty but got '{query_text}'",
1330            );
1331            let results_text = search_view
1332                .results_editor
1333                .update(cx, |editor, cx| editor.display_text(cx));
1334            assert!(
1335                results_text.is_empty(),
1336                "Empty search view should have no results but got '{results_text}'"
1337            );
1338        });
1339
1340        search_view.update(cx, |search_view, cx| {
1341            search_view.query_editor.update(cx, |query_editor, cx| {
1342                query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", cx)
1343            });
1344            search_view.search(cx);
1345        });
1346        deterministic.run_until_parked();
1347        search_view.update(cx, |search_view, cx| {
1348            let results_text = search_view
1349                .results_editor
1350                .update(cx, |editor, cx| editor.display_text(cx));
1351            assert!(
1352                results_text.is_empty(),
1353                "Search view for mismatching query should have no results but got '{results_text}'"
1354            );
1355            assert!(
1356                search_view.query_editor.is_focused(cx),
1357                "Search view should be focused after mismatching query had been used in search",
1358            );
1359        });
1360        cx.spawn(
1361            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1362        )
1363        .detach();
1364        deterministic.run_until_parked();
1365        search_view.update(cx, |search_view, cx| {
1366            assert!(
1367                search_view.query_editor.is_focused(cx),
1368                "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on",
1369            );
1370        });
1371
1372        search_view.update(cx, |search_view, cx| {
1373            search_view
1374                .query_editor
1375                .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
1376            search_view.search(cx);
1377        });
1378        deterministic.run_until_parked();
1379        search_view.update(cx, |search_view, cx| {
1380            assert_eq!(
1381                search_view
1382                    .results_editor
1383                    .update(cx, |editor, cx| editor.display_text(cx)),
1384                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
1385                "Search view results should match the query"
1386            );
1387            assert!(
1388                search_view.results_editor.is_focused(cx),
1389                "Search view with mismatching query should be focused after search results are available",
1390            );
1391        });
1392        cx.spawn(
1393            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1394        )
1395        .detach();
1396        deterministic.run_until_parked();
1397        search_view.update(cx, |search_view, cx| {
1398            assert!(
1399                search_view.results_editor.is_focused(cx),
1400                "Search view with matching query should still have its results editor focused after the toggle focus event",
1401            );
1402        });
1403
1404        workspace.update(cx, |workspace, cx| {
1405            ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx)
1406        });
1407        search_view.update(cx, |search_view, cx| {
1408            assert_eq!(search_view.query_editor.read(cx).text(cx), "two", "Query should be updated to first search result after search view 2nd open in a row");
1409            assert_eq!(
1410                search_view
1411                    .results_editor
1412                    .update(cx, |editor, cx| editor.display_text(cx)),
1413                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
1414                "Results should be unchanged after search view 2nd open in a row"
1415            );
1416            assert!(
1417                search_view.query_editor.is_focused(cx),
1418                "Focus should be moved into query editor again after search view 2nd open in a row"
1419            );
1420        });
1421
1422        cx.spawn(
1423            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1424        )
1425        .detach();
1426        deterministic.run_until_parked();
1427        search_view.update(cx, |search_view, cx| {
1428            assert!(
1429                search_view.results_editor.is_focused(cx),
1430                "Search view with matching query should switch focus to the results editor after the toggle focus event",
1431            );
1432        });
1433    }
1434
1435    pub fn init_test(cx: &mut TestAppContext) {
1436        cx.foreground().forbid_parking();
1437        let fonts = cx.font_cache();
1438        let mut theme = gpui::fonts::with_font_cache(fonts.clone(), theme::Theme::default);
1439        theme.search.match_background = Color::red();
1440
1441        cx.update(|cx| {
1442            cx.set_global(SettingsStore::test(cx));
1443            cx.set_global(ActiveSearches::default());
1444
1445            theme::init((), cx);
1446            cx.update_global::<SettingsStore, _, _>(|store, _| {
1447                let mut settings = store.get::<ThemeSettings>(None).clone();
1448                settings.theme = Arc::new(theme);
1449                store.override_global(settings)
1450            });
1451
1452            language::init(cx);
1453            client::init_settings(cx);
1454            editor::init(cx);
1455            workspace::init_settings(cx);
1456            Project::init_settings(cx);
1457            super::init(cx);
1458        });
1459    }
1460}