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.style_for(state, false);
 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.search.option_button.style_for(state, is_active);
 931            Label::new(icon, style.text.clone())
 932                .contained()
 933                .with_style(style.container)
 934        })
 935        .on_click(MouseButton::Left, move |_, this, cx| {
 936            this.toggle_search_option(option, cx);
 937        })
 938        .with_cursor_style(CursorStyle::PointingHand)
 939        .with_tooltip::<Self>(
 940            option as usize,
 941            format!("Toggle {}", option.label()),
 942            Some(option.to_toggle_action()),
 943            tooltip_style,
 944            cx,
 945        )
 946        .into_any()
 947    }
 948
 949    fn is_option_enabled(&self, option: SearchOption, cx: &AppContext) -> bool {
 950        if let Some(search) = self.active_project_search.as_ref() {
 951            let search = search.read(cx);
 952            match option {
 953                SearchOption::WholeWord => search.whole_word,
 954                SearchOption::CaseSensitive => search.case_sensitive,
 955                SearchOption::Regex => search.regex,
 956            }
 957        } else {
 958            false
 959        }
 960    }
 961}
 962
 963impl Entity for ProjectSearchBar {
 964    type Event = ();
 965}
 966
 967impl View for ProjectSearchBar {
 968    fn ui_name() -> &'static str {
 969        "ProjectSearchBar"
 970    }
 971
 972    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
 973        if let Some(search) = self.active_project_search.as_ref() {
 974            let search = search.read(cx);
 975            let theme = theme::current(cx).clone();
 976            let query_container_style = if search.panels_with_errors.contains(&InputPanel::Query) {
 977                theme.search.invalid_editor
 978            } else {
 979                theme.search.editor.input.container
 980            };
 981            let include_container_style =
 982                if search.panels_with_errors.contains(&InputPanel::Include) {
 983                    theme.search.invalid_include_exclude_editor
 984                } else {
 985                    theme.search.include_exclude_editor.input.container
 986                };
 987            let exclude_container_style =
 988                if search.panels_with_errors.contains(&InputPanel::Exclude) {
 989                    theme.search.invalid_include_exclude_editor
 990                } else {
 991                    theme.search.include_exclude_editor.input.container
 992                };
 993
 994            let included_files_view = ChildView::new(&search.included_files_editor, cx)
 995                .aligned()
 996                .left()
 997                .flex(1.0, true);
 998            let excluded_files_view = ChildView::new(&search.excluded_files_editor, cx)
 999                .aligned()
1000                .right()
1001                .flex(1.0, true);
1002
1003            let row_spacing = theme.workspace.toolbar.container.padding.bottom;
1004
1005            Flex::column()
1006                .with_child(
1007                    Flex::row()
1008                        .with_child(
1009                            Flex::row()
1010                                .with_child(
1011                                    ChildView::new(&search.query_editor, cx)
1012                                        .aligned()
1013                                        .left()
1014                                        .flex(1., true),
1015                                )
1016                                .with_children(search.active_match_index.map(|match_ix| {
1017                                    Label::new(
1018                                        format!(
1019                                            "{}/{}",
1020                                            match_ix + 1,
1021                                            search.model.read(cx).match_ranges.len()
1022                                        ),
1023                                        theme.search.match_index.text.clone(),
1024                                    )
1025                                    .contained()
1026                                    .with_style(theme.search.match_index.container)
1027                                    .aligned()
1028                                }))
1029                                .contained()
1030                                .with_style(query_container_style)
1031                                .aligned()
1032                                .constrained()
1033                                .with_min_width(theme.search.editor.min_width)
1034                                .with_max_width(theme.search.editor.max_width)
1035                                .flex(1., false),
1036                        )
1037                        .with_child(
1038                            Flex::row()
1039                                .with_child(self.render_nav_button("<", Direction::Prev, cx))
1040                                .with_child(self.render_nav_button(">", Direction::Next, cx))
1041                                .aligned(),
1042                        )
1043                        .with_child(
1044                            Flex::row()
1045                                .with_child(self.render_option_button(
1046                                    "Case",
1047                                    SearchOption::CaseSensitive,
1048                                    cx,
1049                                ))
1050                                .with_child(self.render_option_button(
1051                                    "Word",
1052                                    SearchOption::WholeWord,
1053                                    cx,
1054                                ))
1055                                .with_child(self.render_option_button(
1056                                    "Regex",
1057                                    SearchOption::Regex,
1058                                    cx,
1059                                ))
1060                                .contained()
1061                                .with_style(theme.search.option_button_group)
1062                                .aligned(),
1063                        )
1064                        .contained()
1065                        .with_margin_bottom(row_spacing),
1066                )
1067                .with_child(
1068                    Flex::row()
1069                        .with_child(
1070                            Flex::row()
1071                                .with_child(included_files_view)
1072                                .contained()
1073                                .with_style(include_container_style)
1074                                .aligned()
1075                                .constrained()
1076                                .with_min_width(theme.search.include_exclude_editor.min_width)
1077                                .with_max_width(theme.search.include_exclude_editor.max_width)
1078                                .flex(1., false),
1079                        )
1080                        .with_child(
1081                            Flex::row()
1082                                .with_child(excluded_files_view)
1083                                .contained()
1084                                .with_style(exclude_container_style)
1085                                .aligned()
1086                                .constrained()
1087                                .with_min_width(theme.search.include_exclude_editor.min_width)
1088                                .with_max_width(theme.search.include_exclude_editor.max_width)
1089                                .flex(1., false),
1090                        ),
1091                )
1092                .contained()
1093                .with_style(theme.search.container)
1094                .aligned()
1095                .left()
1096                .into_any_named("project search")
1097        } else {
1098            Empty::new().into_any()
1099        }
1100    }
1101}
1102
1103impl ToolbarItemView for ProjectSearchBar {
1104    fn set_active_pane_item(
1105        &mut self,
1106        active_pane_item: Option<&dyn ItemHandle>,
1107        cx: &mut ViewContext<Self>,
1108    ) -> ToolbarItemLocation {
1109        cx.notify();
1110        self.subscription = None;
1111        self.active_project_search = None;
1112        if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
1113            self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
1114            self.active_project_search = Some(search);
1115            ToolbarItemLocation::PrimaryLeft {
1116                flex: Some((1., false)),
1117            }
1118        } else {
1119            ToolbarItemLocation::Hidden
1120        }
1121    }
1122
1123    fn row_count(&self) -> usize {
1124        2
1125    }
1126}
1127
1128#[cfg(test)]
1129pub mod tests {
1130    use super::*;
1131    use editor::DisplayPoint;
1132    use gpui::{color::Color, executor::Deterministic, TestAppContext};
1133    use project::FakeFs;
1134    use serde_json::json;
1135    use settings::SettingsStore;
1136    use std::sync::Arc;
1137    use theme::ThemeSettings;
1138
1139    #[gpui::test]
1140    async fn test_project_search(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
1141        init_test(cx);
1142
1143        let fs = FakeFs::new(cx.background());
1144        fs.insert_tree(
1145            "/dir",
1146            json!({
1147                "one.rs": "const ONE: usize = 1;",
1148                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
1149                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
1150                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
1151            }),
1152        )
1153        .await;
1154        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
1155        let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
1156        let (_, search_view) = cx.add_window(|cx| ProjectSearchView::new(search.clone(), cx));
1157
1158        search_view.update(cx, |search_view, cx| {
1159            search_view
1160                .query_editor
1161                .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
1162            search_view.search(cx);
1163        });
1164        deterministic.run_until_parked();
1165        search_view.update(cx, |search_view, cx| {
1166            assert_eq!(
1167                search_view
1168                    .results_editor
1169                    .update(cx, |editor, cx| editor.display_text(cx)),
1170                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
1171            );
1172            assert_eq!(
1173                search_view
1174                    .results_editor
1175                    .update(cx, |editor, cx| editor.all_background_highlights(cx)),
1176                &[
1177                    (
1178                        DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
1179                        Color::red()
1180                    ),
1181                    (
1182                        DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
1183                        Color::red()
1184                    ),
1185                    (
1186                        DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
1187                        Color::red()
1188                    )
1189                ]
1190            );
1191            assert_eq!(search_view.active_match_index, Some(0));
1192            assert_eq!(
1193                search_view
1194                    .results_editor
1195                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1196                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1197            );
1198
1199            search_view.select_match(Direction::Next, cx);
1200        });
1201
1202        search_view.update(cx, |search_view, cx| {
1203            assert_eq!(search_view.active_match_index, Some(1));
1204            assert_eq!(
1205                search_view
1206                    .results_editor
1207                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1208                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1209            );
1210            search_view.select_match(Direction::Next, cx);
1211        });
1212
1213        search_view.update(cx, |search_view, cx| {
1214            assert_eq!(search_view.active_match_index, Some(2));
1215            assert_eq!(
1216                search_view
1217                    .results_editor
1218                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1219                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1220            );
1221            search_view.select_match(Direction::Next, cx);
1222        });
1223
1224        search_view.update(cx, |search_view, cx| {
1225            assert_eq!(search_view.active_match_index, Some(0));
1226            assert_eq!(
1227                search_view
1228                    .results_editor
1229                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1230                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1231            );
1232            search_view.select_match(Direction::Prev, cx);
1233        });
1234
1235        search_view.update(cx, |search_view, cx| {
1236            assert_eq!(search_view.active_match_index, Some(2));
1237            assert_eq!(
1238                search_view
1239                    .results_editor
1240                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1241                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1242            );
1243            search_view.select_match(Direction::Prev, cx);
1244        });
1245
1246        search_view.update(cx, |search_view, cx| {
1247            assert_eq!(search_view.active_match_index, Some(1));
1248            assert_eq!(
1249                search_view
1250                    .results_editor
1251                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1252                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1253            );
1254        });
1255    }
1256
1257    #[gpui::test]
1258    async fn test_project_search_focus(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
1259        init_test(cx);
1260
1261        let fs = FakeFs::new(cx.background());
1262        fs.insert_tree(
1263            "/dir",
1264            json!({
1265                "one.rs": "const ONE: usize = 1;",
1266                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
1267                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
1268                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
1269            }),
1270        )
1271        .await;
1272        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
1273        let (window_id, workspace) = cx.add_window(|cx| Workspace::test_new(project, cx));
1274
1275        let active_item = cx.read(|cx| {
1276            workspace
1277                .read(cx)
1278                .active_pane()
1279                .read(cx)
1280                .active_item()
1281                .and_then(|item| item.downcast::<ProjectSearchView>())
1282        });
1283        assert!(
1284            active_item.is_none(),
1285            "Expected no search panel to be active, but got: {active_item:?}"
1286        );
1287
1288        workspace.update(cx, |workspace, cx| {
1289            ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx)
1290        });
1291
1292        let Some(search_view) = cx.read(|cx| {
1293            workspace
1294                .read(cx)
1295                .active_pane()
1296                .read(cx)
1297                .active_item()
1298                .and_then(|item| item.downcast::<ProjectSearchView>())
1299        }) else {
1300            panic!("Search view expected to appear after new search event trigger")
1301        };
1302        let search_view_id = search_view.id();
1303
1304        cx.spawn(
1305            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1306        )
1307        .detach();
1308        deterministic.run_until_parked();
1309        search_view.update(cx, |search_view, cx| {
1310            assert!(
1311                search_view.query_editor.is_focused(cx),
1312                "Empty search view should be focused after the toggle focus event: no results panel to focus on",
1313            );
1314        });
1315
1316        search_view.update(cx, |search_view, cx| {
1317            let query_editor = &search_view.query_editor;
1318            assert!(
1319                query_editor.is_focused(cx),
1320                "Search view should be focused after the new search view is activated",
1321            );
1322            let query_text = query_editor.read(cx).text(cx);
1323            assert!(
1324                query_text.is_empty(),
1325                "New search query should be empty but got '{query_text}'",
1326            );
1327            let results_text = search_view
1328                .results_editor
1329                .update(cx, |editor, cx| editor.display_text(cx));
1330            assert!(
1331                results_text.is_empty(),
1332                "Empty search view should have no results but got '{results_text}'"
1333            );
1334        });
1335
1336        search_view.update(cx, |search_view, cx| {
1337            search_view.query_editor.update(cx, |query_editor, cx| {
1338                query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", cx)
1339            });
1340            search_view.search(cx);
1341        });
1342        deterministic.run_until_parked();
1343        search_view.update(cx, |search_view, cx| {
1344            let results_text = search_view
1345                .results_editor
1346                .update(cx, |editor, cx| editor.display_text(cx));
1347            assert!(
1348                results_text.is_empty(),
1349                "Search view for mismatching query should have no results but got '{results_text}'"
1350            );
1351            assert!(
1352                search_view.query_editor.is_focused(cx),
1353                "Search view should be focused after mismatching query had been used in search",
1354            );
1355        });
1356        cx.spawn(
1357            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1358        )
1359        .detach();
1360        deterministic.run_until_parked();
1361        search_view.update(cx, |search_view, cx| {
1362            assert!(
1363                search_view.query_editor.is_focused(cx),
1364                "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on",
1365            );
1366        });
1367
1368        search_view.update(cx, |search_view, cx| {
1369            search_view
1370                .query_editor
1371                .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
1372            search_view.search(cx);
1373        });
1374        deterministic.run_until_parked();
1375        search_view.update(cx, |search_view, cx| {
1376            assert_eq!(
1377                search_view
1378                    .results_editor
1379                    .update(cx, |editor, cx| editor.display_text(cx)),
1380                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
1381                "Search view results should match the query"
1382            );
1383            assert!(
1384                search_view.results_editor.is_focused(cx),
1385                "Search view with mismatching query should be focused after search results are available",
1386            );
1387        });
1388        cx.spawn(
1389            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1390        )
1391        .detach();
1392        deterministic.run_until_parked();
1393        search_view.update(cx, |search_view, cx| {
1394            assert!(
1395                search_view.results_editor.is_focused(cx),
1396                "Search view with matching query should still have its results editor focused after the toggle focus event",
1397            );
1398        });
1399
1400        workspace.update(cx, |workspace, cx| {
1401            ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx)
1402        });
1403        search_view.update(cx, |search_view, cx| {
1404            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");
1405            assert_eq!(
1406                search_view
1407                    .results_editor
1408                    .update(cx, |editor, cx| editor.display_text(cx)),
1409                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
1410                "Results should be unchanged after search view 2nd open in a row"
1411            );
1412            assert!(
1413                search_view.query_editor.is_focused(cx),
1414                "Focus should be moved into query editor again after search view 2nd open in a row"
1415            );
1416        });
1417
1418        cx.spawn(
1419            |mut cx| async move { cx.dispatch_action(window_id, search_view_id, &ToggleFocus) },
1420        )
1421        .detach();
1422        deterministic.run_until_parked();
1423        search_view.update(cx, |search_view, cx| {
1424            assert!(
1425                search_view.results_editor.is_focused(cx),
1426                "Search view with matching query should switch focus to the results editor after the toggle focus event",
1427            );
1428        });
1429    }
1430
1431    pub fn init_test(cx: &mut TestAppContext) {
1432        cx.foreground().forbid_parking();
1433        let fonts = cx.font_cache();
1434        let mut theme = gpui::fonts::with_font_cache(fonts.clone(), theme::Theme::default);
1435        theme.search.match_background = Color::red();
1436
1437        cx.update(|cx| {
1438            cx.set_global(SettingsStore::test(cx));
1439            cx.set_global(ActiveSearches::default());
1440
1441            theme::init((), cx);
1442            cx.update_global::<SettingsStore, _, _>(|store, _| {
1443                let mut settings = store.get::<ThemeSettings>(None).clone();
1444                settings.theme = Arc::new(theme);
1445                store.override_global(settings)
1446            });
1447
1448            language::init(cx);
1449            client::init_settings(cx);
1450            editor::init(cx);
1451            workspace::init_settings(cx);
1452            Project::init_settings(cx);
1453            super::init(cx);
1454        });
1455    }
1456}