project_search.rs

   1use crate::{
   2    buffer_search::Deploy, BufferSearchBar, FocusSearch, NextHistoryQuery, PreviousHistoryQuery,
   3    ReplaceAll, ReplaceNext, SearchOptions, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive,
   4    ToggleIncludeIgnored, ToggleRegex, ToggleReplace, ToggleWholeWord,
   5};
   6use collections::{HashMap, HashSet};
   7use editor::{
   8    actions::SelectAll,
   9    items::active_match_index,
  10    scroll::{Autoscroll, Axis},
  11    Anchor, Editor, EditorElement, EditorEvent, EditorSettings, EditorStyle, MultiBuffer,
  12    MAX_TAB_TITLE_LEN,
  13};
  14use futures::StreamExt;
  15use gpui::{
  16    actions, div, Action, AnyElement, AnyView, AppContext, Context as _, EntityId, EventEmitter,
  17    FocusHandle, FocusableView, Global, Hsla, InteractiveElement, IntoElement, KeyContext, Model,
  18    ModelContext, ParentElement, Point, Render, SharedString, Styled, Subscription, Task,
  19    TextStyle, UpdateGlobal, View, ViewContext, VisualContext, WeakModel, WeakView, WindowContext,
  20};
  21use language::Buffer;
  22use menu::Confirm;
  23use project::{
  24    search::{SearchInputKind, SearchQuery},
  25    search_history::SearchHistoryCursor,
  26    Project, ProjectPath,
  27};
  28use settings::Settings;
  29use std::{
  30    any::{Any, TypeId},
  31    mem,
  32    ops::{Not, Range},
  33    path::Path,
  34};
  35use theme::ThemeSettings;
  36use ui::{
  37    h_flex, prelude::*, v_flex, Icon, IconButton, IconButtonShape, IconName, KeyBinding, Label,
  38    LabelCommon, LabelSize, Selectable, Tooltip,
  39};
  40use util::paths::PathMatcher;
  41use workspace::{
  42    item::{BreadcrumbText, Item, ItemEvent, ItemHandle},
  43    searchable::{Direction, SearchableItem, SearchableItemHandle},
  44    DeploySearch, ItemNavHistory, NewSearch, ToolbarItemEvent, ToolbarItemLocation,
  45    ToolbarItemView, Workspace, WorkspaceId,
  46};
  47
  48actions!(
  49    project_search,
  50    [SearchInNew, ToggleFocus, NextField, ToggleFilters]
  51);
  52
  53#[derive(Default)]
  54struct ActiveSettings(HashMap<WeakModel<Project>, ProjectSearchSettings>);
  55
  56impl Global for ActiveSettings {}
  57
  58pub fn init(cx: &mut AppContext) {
  59    cx.set_global(ActiveSettings::default());
  60    cx.observe_new_views(|workspace: &mut Workspace, _cx| {
  61        register_workspace_action(workspace, move |search_bar, _: &Deploy, cx| {
  62            search_bar.focus_search(cx);
  63        });
  64        register_workspace_action(workspace, move |search_bar, _: &FocusSearch, cx| {
  65            search_bar.focus_search(cx);
  66        });
  67        register_workspace_action(workspace, move |search_bar, _: &ToggleFilters, cx| {
  68            search_bar.toggle_filters(cx);
  69        });
  70        register_workspace_action(workspace, move |search_bar, _: &ToggleCaseSensitive, cx| {
  71            search_bar.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
  72        });
  73        register_workspace_action(workspace, move |search_bar, _: &ToggleWholeWord, cx| {
  74            search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx);
  75        });
  76        register_workspace_action(workspace, move |search_bar, _: &ToggleRegex, cx| {
  77            search_bar.toggle_search_option(SearchOptions::REGEX, cx);
  78        });
  79        register_workspace_action(workspace, move |search_bar, action: &ToggleReplace, cx| {
  80            search_bar.toggle_replace(action, cx)
  81        });
  82        register_workspace_action(
  83            workspace,
  84            move |search_bar, action: &SelectPrevMatch, cx| {
  85                search_bar.select_prev_match(action, cx)
  86            },
  87        );
  88        register_workspace_action(
  89            workspace,
  90            move |search_bar, action: &SelectNextMatch, cx| {
  91                search_bar.select_next_match(action, cx)
  92            },
  93        );
  94
  95        // Only handle search_in_new if there is a search present
  96        register_workspace_action_for_present_search(workspace, |workspace, action, cx| {
  97            ProjectSearchView::search_in_new(workspace, action, cx)
  98        });
  99
 100        // Both on present and dismissed search, we need to unconditionally handle those actions to focus from the editor.
 101        workspace.register_action(move |workspace, action: &DeploySearch, cx| {
 102            if workspace.has_active_modal(cx) {
 103                cx.propagate();
 104                return;
 105            }
 106            ProjectSearchView::deploy_search(workspace, action, cx);
 107            cx.notify();
 108        });
 109        workspace.register_action(move |workspace, action: &NewSearch, cx| {
 110            if workspace.has_active_modal(cx) {
 111                cx.propagate();
 112                return;
 113            }
 114            ProjectSearchView::new_search(workspace, action, cx);
 115            cx.notify();
 116        });
 117    })
 118    .detach();
 119}
 120
 121fn is_contains_uppercase(str: &str) -> bool {
 122    str.chars().any(|c| c.is_uppercase())
 123}
 124
 125pub struct ProjectSearch {
 126    project: Model<Project>,
 127    excerpts: Model<MultiBuffer>,
 128    pending_search: Option<Task<Option<()>>>,
 129    match_ranges: Vec<Range<Anchor>>,
 130    active_query: Option<SearchQuery>,
 131    last_search_query_text: Option<String>,
 132    search_id: usize,
 133    no_results: Option<bool>,
 134    limit_reached: bool,
 135    search_history_cursor: SearchHistoryCursor,
 136    search_included_history_cursor: SearchHistoryCursor,
 137    search_excluded_history_cursor: SearchHistoryCursor,
 138}
 139
 140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 141enum InputPanel {
 142    Query,
 143    Exclude,
 144    Include,
 145}
 146
 147pub struct ProjectSearchView {
 148    workspace: WeakView<Workspace>,
 149    focus_handle: FocusHandle,
 150    model: Model<ProjectSearch>,
 151    query_editor: View<Editor>,
 152    replacement_editor: View<Editor>,
 153    results_editor: View<Editor>,
 154    search_options: SearchOptions,
 155    panels_with_errors: HashSet<InputPanel>,
 156    active_match_index: Option<usize>,
 157    search_id: usize,
 158    included_files_editor: View<Editor>,
 159    excluded_files_editor: View<Editor>,
 160    filters_enabled: bool,
 161    replace_enabled: bool,
 162    included_opened_only: bool,
 163    _subscriptions: Vec<Subscription>,
 164}
 165
 166#[derive(Debug, Clone)]
 167pub struct ProjectSearchSettings {
 168    search_options: SearchOptions,
 169    filters_enabled: bool,
 170}
 171
 172pub struct ProjectSearchBar {
 173    active_project_search: Option<View<ProjectSearchView>>,
 174    subscription: Option<Subscription>,
 175}
 176
 177impl ProjectSearch {
 178    pub fn new(project: Model<Project>, cx: &mut ModelContext<Self>) -> Self {
 179        let capability = project.read(cx).capability();
 180
 181        Self {
 182            project,
 183            excerpts: cx.new_model(|_| MultiBuffer::new(capability)),
 184            pending_search: Default::default(),
 185            match_ranges: Default::default(),
 186            active_query: None,
 187            last_search_query_text: None,
 188            search_id: 0,
 189            no_results: None,
 190            limit_reached: false,
 191            search_history_cursor: Default::default(),
 192            search_included_history_cursor: Default::default(),
 193            search_excluded_history_cursor: Default::default(),
 194        }
 195    }
 196
 197    fn clone(&self, cx: &mut ModelContext<Self>) -> Model<Self> {
 198        cx.new_model(|cx| Self {
 199            project: self.project.clone(),
 200            excerpts: self
 201                .excerpts
 202                .update(cx, |excerpts, cx| cx.new_model(|cx| excerpts.clone(cx))),
 203            pending_search: Default::default(),
 204            match_ranges: self.match_ranges.clone(),
 205            active_query: self.active_query.clone(),
 206            last_search_query_text: self.last_search_query_text.clone(),
 207            search_id: self.search_id,
 208            no_results: self.no_results,
 209            limit_reached: self.limit_reached,
 210            search_history_cursor: self.search_history_cursor.clone(),
 211            search_included_history_cursor: self.search_included_history_cursor.clone(),
 212            search_excluded_history_cursor: self.search_excluded_history_cursor.clone(),
 213        })
 214    }
 215    fn cursor(&self, kind: SearchInputKind) -> &SearchHistoryCursor {
 216        match kind {
 217            SearchInputKind::Query => &self.search_history_cursor,
 218            SearchInputKind::Include => &self.search_included_history_cursor,
 219            SearchInputKind::Exclude => &self.search_excluded_history_cursor,
 220        }
 221    }
 222    fn cursor_mut(&mut self, kind: SearchInputKind) -> &mut SearchHistoryCursor {
 223        match kind {
 224            SearchInputKind::Query => &mut self.search_history_cursor,
 225            SearchInputKind::Include => &mut self.search_included_history_cursor,
 226            SearchInputKind::Exclude => &mut self.search_excluded_history_cursor,
 227        }
 228    }
 229
 230    fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
 231        let search = self.project.update(cx, |project, cx| {
 232            project
 233                .search_history_mut(SearchInputKind::Query)
 234                .add(&mut self.search_history_cursor, query.as_str().to_string());
 235            let included = query.as_inner().files_to_include().sources().join(",");
 236            if !included.is_empty() {
 237                project
 238                    .search_history_mut(SearchInputKind::Include)
 239                    .add(&mut self.search_included_history_cursor, included);
 240            }
 241            let excluded = query.as_inner().files_to_exclude().sources().join(",");
 242            if !excluded.is_empty() {
 243                project
 244                    .search_history_mut(SearchInputKind::Exclude)
 245                    .add(&mut self.search_excluded_history_cursor, excluded);
 246            }
 247            project.search(query.clone(), cx)
 248        });
 249        self.last_search_query_text = Some(query.as_str().to_string());
 250        self.search_id += 1;
 251        self.active_query = Some(query);
 252        self.match_ranges.clear();
 253        self.pending_search = Some(cx.spawn(|this, mut cx| async move {
 254            let mut matches = search.ready_chunks(1024);
 255            let this = this.upgrade()?;
 256            this.update(&mut cx, |this, cx| {
 257                this.match_ranges.clear();
 258                this.excerpts.update(cx, |this, cx| this.clear(cx));
 259                this.no_results = Some(true);
 260                this.limit_reached = false;
 261            })
 262            .ok()?;
 263
 264            let mut limit_reached = false;
 265            while let Some(results) = matches.next().await {
 266                let mut buffers_with_ranges = Vec::with_capacity(results.len());
 267                for result in results {
 268                    match result {
 269                        project::search::SearchResult::Buffer { buffer, ranges } => {
 270                            buffers_with_ranges.push((buffer, ranges));
 271                        }
 272                        project::search::SearchResult::LimitReached => {
 273                            limit_reached = true;
 274                        }
 275                    }
 276                }
 277
 278                let match_ranges = this
 279                    .update(&mut cx, |this, cx| {
 280                        this.excerpts.update(cx, |excerpts, cx| {
 281                            excerpts.push_multiple_excerpts_with_context_lines(
 282                                buffers_with_ranges,
 283                                editor::DEFAULT_MULTIBUFFER_CONTEXT,
 284                                cx,
 285                            )
 286                        })
 287                    })
 288                    .ok()?
 289                    .await;
 290
 291                this.update(&mut cx, |this, cx| {
 292                    this.match_ranges.extend(match_ranges);
 293                    cx.notify();
 294                })
 295                .ok()?;
 296            }
 297
 298            this.update(&mut cx, |this, cx| {
 299                if !this.match_ranges.is_empty() {
 300                    this.no_results = Some(false);
 301                }
 302                this.limit_reached = limit_reached;
 303                this.pending_search.take();
 304                cx.notify();
 305            })
 306            .ok()?;
 307
 308            None
 309        }));
 310        cx.notify();
 311    }
 312}
 313
 314#[derive(Clone, Debug, PartialEq, Eq)]
 315pub enum ViewEvent {
 316    UpdateTab,
 317    Activate,
 318    EditorEvent(editor::EditorEvent),
 319    Dismiss,
 320}
 321
 322impl EventEmitter<ViewEvent> for ProjectSearchView {}
 323
 324impl Render for ProjectSearchView {
 325    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
 326        if self.has_matches() {
 327            div()
 328                .flex_1()
 329                .size_full()
 330                .track_focus(&self.focus_handle(cx))
 331                .child(self.results_editor.clone())
 332        } else {
 333            let model = self.model.read(cx);
 334            let has_no_results = model.no_results.unwrap_or(false);
 335            let is_search_underway = model.pending_search.is_some();
 336            let major_text = if is_search_underway {
 337                "Searching..."
 338            } else if has_no_results {
 339                "No results"
 340            } else {
 341                "Search all files"
 342            };
 343
 344            let major_text = div()
 345                .justify_center()
 346                .max_w_96()
 347                .child(Label::new(major_text).size(LabelSize::Large));
 348
 349            let minor_text: Option<AnyElement> = if let Some(no_results) = model.no_results {
 350                if model.pending_search.is_none() && no_results {
 351                    Some(
 352                        Label::new("No results found in this project for the provided query")
 353                            .size(LabelSize::Small)
 354                            .into_any_element(),
 355                    )
 356                } else {
 357                    None
 358                }
 359            } else {
 360                Some(self.landing_text_minor(cx).into_any_element())
 361            };
 362            let minor_text = minor_text.map(|text| div().items_center().max_w_96().child(text));
 363            v_flex()
 364                .flex_1()
 365                .size_full()
 366                .justify_center()
 367                .bg(cx.theme().colors().editor_background)
 368                .track_focus(&self.focus_handle(cx))
 369                .child(
 370                    h_flex()
 371                        .size_full()
 372                        .justify_center()
 373                        .child(h_flex().flex_1())
 374                        .child(v_flex().gap_1().child(major_text).children(minor_text))
 375                        .child(h_flex().flex_1()),
 376                )
 377        }
 378    }
 379}
 380
 381impl FocusableView for ProjectSearchView {
 382    fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
 383        self.focus_handle.clone()
 384    }
 385}
 386
 387impl Item for ProjectSearchView {
 388    type Event = ViewEvent;
 389    fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
 390        let query_text = self.query_editor.read(cx).text(cx);
 391
 392        query_text
 393            .is_empty()
 394            .not()
 395            .then(|| query_text.into())
 396            .or_else(|| Some("Project Search".into()))
 397    }
 398
 399    fn act_as_type<'a>(
 400        &'a self,
 401        type_id: TypeId,
 402        self_handle: &'a View<Self>,
 403        _: &'a AppContext,
 404    ) -> Option<AnyView> {
 405        if type_id == TypeId::of::<Self>() {
 406            Some(self_handle.clone().into())
 407        } else if type_id == TypeId::of::<Editor>() {
 408            Some(self.results_editor.clone().into())
 409        } else {
 410            None
 411        }
 412    }
 413
 414    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
 415        self.results_editor
 416            .update(cx, |editor, cx| editor.deactivated(cx));
 417    }
 418
 419    fn tab_icon(&self, _cx: &WindowContext) -> Option<Icon> {
 420        Some(Icon::new(IconName::MagnifyingGlass))
 421    }
 422
 423    fn tab_content_text(&self, cx: &WindowContext) -> Option<SharedString> {
 424        let last_query: Option<SharedString> = self
 425            .model
 426            .read(cx)
 427            .last_search_query_text
 428            .as_ref()
 429            .map(|query| {
 430                let query = query.replace('\n', "");
 431                let query_text = util::truncate_and_trailoff(&query, MAX_TAB_TITLE_LEN);
 432                query_text.into()
 433            });
 434        Some(
 435            last_query
 436                .filter(|query| !query.is_empty())
 437                .unwrap_or_else(|| "Project Search".into()),
 438        )
 439    }
 440
 441    fn telemetry_event_text(&self) -> Option<&'static str> {
 442        Some("project search")
 443    }
 444
 445    fn for_each_project_item(
 446        &self,
 447        cx: &AppContext,
 448        f: &mut dyn FnMut(EntityId, &dyn project::Item),
 449    ) {
 450        self.results_editor.for_each_project_item(cx, f)
 451    }
 452
 453    fn is_singleton(&self, _: &AppContext) -> bool {
 454        false
 455    }
 456
 457    fn can_save(&self, _: &AppContext) -> bool {
 458        true
 459    }
 460
 461    fn is_dirty(&self, cx: &AppContext) -> bool {
 462        self.results_editor.read(cx).is_dirty(cx)
 463    }
 464
 465    fn has_conflict(&self, cx: &AppContext) -> bool {
 466        self.results_editor.read(cx).has_conflict(cx)
 467    }
 468
 469    fn save(
 470        &mut self,
 471        format: bool,
 472        project: Model<Project>,
 473        cx: &mut ViewContext<Self>,
 474    ) -> Task<anyhow::Result<()>> {
 475        self.results_editor
 476            .update(cx, |editor, cx| editor.save(format, project, cx))
 477    }
 478
 479    fn save_as(
 480        &mut self,
 481        _: Model<Project>,
 482        _: ProjectPath,
 483        _: &mut ViewContext<Self>,
 484    ) -> Task<anyhow::Result<()>> {
 485        unreachable!("save_as should not have been called")
 486    }
 487
 488    fn reload(
 489        &mut self,
 490        project: Model<Project>,
 491        cx: &mut ViewContext<Self>,
 492    ) -> Task<anyhow::Result<()>> {
 493        self.results_editor
 494            .update(cx, |editor, cx| editor.reload(project, cx))
 495    }
 496
 497    fn clone_on_split(
 498        &self,
 499        _workspace_id: Option<WorkspaceId>,
 500        cx: &mut ViewContext<Self>,
 501    ) -> Option<View<Self>>
 502    where
 503        Self: Sized,
 504    {
 505        let model = self.model.update(cx, |model, cx| model.clone(cx));
 506        Some(cx.new_view(|cx| Self::new(self.workspace.clone(), model, cx, None)))
 507    }
 508
 509    fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
 510        self.results_editor
 511            .update(cx, |editor, cx| editor.added_to_workspace(workspace, cx));
 512    }
 513
 514    fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
 515        self.results_editor.update(cx, |editor, _| {
 516            editor.set_nav_history(Some(nav_history));
 517        });
 518    }
 519
 520    fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
 521        self.results_editor
 522            .update(cx, |editor, cx| editor.navigate(data, cx))
 523    }
 524
 525    fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
 526        match event {
 527            ViewEvent::UpdateTab => {
 528                f(ItemEvent::UpdateBreadcrumbs);
 529                f(ItemEvent::UpdateTab);
 530            }
 531            ViewEvent::EditorEvent(editor_event) => {
 532                Editor::to_item_events(editor_event, f);
 533            }
 534            ViewEvent::Dismiss => f(ItemEvent::CloseItem),
 535            _ => {}
 536        }
 537    }
 538
 539    fn breadcrumb_location(&self) -> ToolbarItemLocation {
 540        if self.has_matches() {
 541            ToolbarItemLocation::Secondary
 542        } else {
 543            ToolbarItemLocation::Hidden
 544        }
 545    }
 546
 547    fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
 548        self.results_editor.breadcrumbs(theme, cx)
 549    }
 550}
 551
 552impl ProjectSearchView {
 553    pub fn get_matches(&self, cx: &AppContext) -> Vec<Range<Anchor>> {
 554        self.model.read(cx).match_ranges.clone()
 555    }
 556
 557    fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) {
 558        self.filters_enabled = !self.filters_enabled;
 559        ActiveSettings::update_global(cx, |settings, cx| {
 560            settings.0.insert(
 561                self.model.read(cx).project.downgrade(),
 562                self.current_settings(),
 563            );
 564        });
 565    }
 566
 567    fn current_settings(&self) -> ProjectSearchSettings {
 568        ProjectSearchSettings {
 569            search_options: self.search_options,
 570            filters_enabled: self.filters_enabled,
 571        }
 572    }
 573
 574    fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext<Self>) {
 575        self.search_options.toggle(option);
 576        ActiveSettings::update_global(cx, |settings, cx| {
 577            settings.0.insert(
 578                self.model.read(cx).project.downgrade(),
 579                self.current_settings(),
 580            );
 581        });
 582    }
 583
 584    fn toggle_opened_only(&mut self, _cx: &mut ViewContext<Self>) {
 585        self.included_opened_only = !self.included_opened_only;
 586    }
 587
 588    fn replace_next(&mut self, _: &ReplaceNext, cx: &mut ViewContext<Self>) {
 589        if self.model.read(cx).match_ranges.is_empty() {
 590            return;
 591        }
 592        let Some(active_index) = self.active_match_index else {
 593            return;
 594        };
 595
 596        let query = self.model.read(cx).active_query.clone();
 597        if let Some(query) = query {
 598            let query = query.with_replacement(self.replacement(cx));
 599
 600            // TODO: Do we need the clone here?
 601            let mat = self.model.read(cx).match_ranges[active_index].clone();
 602            self.results_editor.update(cx, |editor, cx| {
 603                editor.replace(&mat, &query, cx);
 604            });
 605            self.select_match(Direction::Next, cx)
 606        }
 607    }
 608    pub fn replacement(&self, cx: &AppContext) -> String {
 609        self.replacement_editor.read(cx).text(cx)
 610    }
 611    fn replace_all(&mut self, _: &ReplaceAll, cx: &mut ViewContext<Self>) {
 612        if self.active_match_index.is_none() {
 613            return;
 614        }
 615
 616        let Some(query) = self.model.read(cx).active_query.as_ref() else {
 617            return;
 618        };
 619        let query = query.clone().with_replacement(self.replacement(cx));
 620
 621        let match_ranges = self
 622            .model
 623            .update(cx, |model, _| mem::take(&mut model.match_ranges));
 624        if match_ranges.is_empty() {
 625            return;
 626        }
 627
 628        self.results_editor.update(cx, |editor, cx| {
 629            editor.replace_all(&mut match_ranges.iter(), &query, cx);
 630        });
 631
 632        self.model.update(cx, |model, _cx| {
 633            model.match_ranges = match_ranges;
 634        });
 635    }
 636
 637    pub fn new(
 638        workspace: WeakView<Workspace>,
 639        model: Model<ProjectSearch>,
 640        cx: &mut ViewContext<Self>,
 641        settings: Option<ProjectSearchSettings>,
 642    ) -> Self {
 643        let project;
 644        let excerpts;
 645        let mut replacement_text = None;
 646        let mut query_text = String::new();
 647        let mut subscriptions = Vec::new();
 648
 649        // Read in settings if available
 650        let (mut options, filters_enabled) = if let Some(settings) = settings {
 651            (settings.search_options, settings.filters_enabled)
 652        } else {
 653            let search_options =
 654                SearchOptions::from_settings(&EditorSettings::get_global(cx).search);
 655            (search_options, false)
 656        };
 657
 658        {
 659            let model = model.read(cx);
 660            project = model.project.clone();
 661            excerpts = model.excerpts.clone();
 662            if let Some(active_query) = model.active_query.as_ref() {
 663                query_text = active_query.as_str().to_string();
 664                replacement_text = active_query.replacement().map(ToOwned::to_owned);
 665                options = SearchOptions::from_query(active_query);
 666            }
 667        }
 668        subscriptions.push(cx.observe(&model, |this, _, cx| this.model_changed(cx)));
 669
 670        let query_editor = cx.new_view(|cx| {
 671            let mut editor = Editor::single_line(cx);
 672            editor.set_placeholder_text("Search all files...", cx);
 673            editor.set_text(query_text, cx);
 674            editor
 675        });
 676        // Subscribe to query_editor in order to reraise editor events for workspace item activation purposes
 677        subscriptions.push(
 678            cx.subscribe(&query_editor, |this, _, event: &EditorEvent, cx| {
 679                if let EditorEvent::Edited { .. } = event {
 680                    if EditorSettings::get_global(cx).use_smartcase_search {
 681                        let query = this.search_query_text(cx);
 682                        if !query.is_empty()
 683                            && this.search_options.contains(SearchOptions::CASE_SENSITIVE)
 684                                != is_contains_uppercase(&query)
 685                        {
 686                            this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
 687                        }
 688                    }
 689                }
 690                cx.emit(ViewEvent::EditorEvent(event.clone()))
 691            }),
 692        );
 693        let replacement_editor = cx.new_view(|cx| {
 694            let mut editor = Editor::single_line(cx);
 695            editor.set_placeholder_text("Replace in project...", cx);
 696            if let Some(text) = replacement_text {
 697                editor.set_text(text, cx);
 698            }
 699            editor
 700        });
 701        let results_editor = cx.new_view(|cx| {
 702            let mut editor = Editor::for_multibuffer(excerpts, Some(project.clone()), true, cx);
 703            editor.set_searchable(false);
 704            editor
 705        });
 706        subscriptions.push(cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab)));
 707
 708        subscriptions.push(
 709            cx.subscribe(&results_editor, |this, _, event: &EditorEvent, cx| {
 710                if matches!(event, editor::EditorEvent::SelectionsChanged { .. }) {
 711                    this.update_match_index(cx);
 712                }
 713                // Reraise editor events for workspace item activation purposes
 714                cx.emit(ViewEvent::EditorEvent(event.clone()));
 715            }),
 716        );
 717
 718        let included_files_editor = cx.new_view(|cx| {
 719            let mut editor = Editor::single_line(cx);
 720            editor.set_placeholder_text("Include: crates/**/*.toml", cx);
 721
 722            editor
 723        });
 724        // Subscribe to include_files_editor in order to reraise editor events for workspace item activation purposes
 725        subscriptions.push(
 726            cx.subscribe(&included_files_editor, |_, _, event: &EditorEvent, cx| {
 727                cx.emit(ViewEvent::EditorEvent(event.clone()))
 728            }),
 729        );
 730
 731        let excluded_files_editor = cx.new_view(|cx| {
 732            let mut editor = Editor::single_line(cx);
 733            editor.set_placeholder_text("Exclude: vendor/*, *.lock", cx);
 734
 735            editor
 736        });
 737        // Subscribe to excluded_files_editor in order to reraise editor events for workspace item activation purposes
 738        subscriptions.push(
 739            cx.subscribe(&excluded_files_editor, |_, _, event: &EditorEvent, cx| {
 740                cx.emit(ViewEvent::EditorEvent(event.clone()))
 741            }),
 742        );
 743
 744        let focus_handle = cx.focus_handle();
 745        subscriptions.push(cx.on_focus_in(&focus_handle, |this, cx| {
 746            if this.focus_handle.is_focused(cx) {
 747                if this.has_matches() {
 748                    this.results_editor.focus_handle(cx).focus(cx);
 749                } else {
 750                    this.query_editor.focus_handle(cx).focus(cx);
 751                }
 752            }
 753        }));
 754
 755        // Check if Worktrees have all been previously indexed
 756        let mut this = ProjectSearchView {
 757            workspace,
 758            focus_handle,
 759            replacement_editor,
 760            search_id: model.read(cx).search_id,
 761            model,
 762            query_editor,
 763            results_editor,
 764            search_options: options,
 765            panels_with_errors: HashSet::default(),
 766            active_match_index: None,
 767            included_files_editor,
 768            excluded_files_editor,
 769            filters_enabled,
 770            replace_enabled: false,
 771            included_opened_only: false,
 772            _subscriptions: subscriptions,
 773        };
 774        this.model_changed(cx);
 775        this
 776    }
 777
 778    pub fn new_search_in_directory(
 779        workspace: &mut Workspace,
 780        dir_path: &Path,
 781        cx: &mut ViewContext<Workspace>,
 782    ) {
 783        let Some(filter_str) = dir_path.to_str() else {
 784            return;
 785        };
 786
 787        let weak_workspace = cx.view().downgrade();
 788
 789        let model = cx.new_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
 790        let search = cx.new_view(|cx| ProjectSearchView::new(weak_workspace, model, cx, None));
 791        workspace.add_item_to_active_pane(Box::new(search.clone()), None, true, cx);
 792        search.update(cx, |search, cx| {
 793            search
 794                .included_files_editor
 795                .update(cx, |editor, cx| editor.set_text(filter_str, cx));
 796            search.filters_enabled = true;
 797            search.focus_query_editor(cx)
 798        });
 799    }
 800
 801    /// Re-activate the most recently activated search in this pane or the most recent if it has been closed.
 802    /// If no search exists in the workspace, create a new one.
 803    pub fn deploy_search(
 804        workspace: &mut Workspace,
 805        action: &workspace::DeploySearch,
 806        cx: &mut ViewContext<Workspace>,
 807    ) {
 808        let existing = workspace
 809            .active_pane()
 810            .read(cx)
 811            .items()
 812            .find_map(|item| item.downcast::<ProjectSearchView>());
 813
 814        Self::existing_or_new_search(workspace, existing, action, cx);
 815    }
 816
 817    fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
 818        if let Some(search_view) = workspace
 819            .active_item(cx)
 820            .and_then(|item| item.downcast::<ProjectSearchView>())
 821        {
 822            let new_query = search_view.update(cx, |search_view, cx| {
 823                let new_query = search_view.build_search_query(cx);
 824                if new_query.is_some() {
 825                    if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
 826                        search_view.query_editor.update(cx, |editor, cx| {
 827                            editor.set_text(old_query.as_str(), cx);
 828                        });
 829                        search_view.search_options = SearchOptions::from_query(&old_query);
 830                    }
 831                }
 832                new_query
 833            });
 834            if let Some(new_query) = new_query {
 835                let model = cx.new_model(|cx| {
 836                    let mut model = ProjectSearch::new(workspace.project().clone(), cx);
 837                    model.search(new_query, cx);
 838                    model
 839                });
 840                let weak_workspace = cx.view().downgrade();
 841                workspace.add_item_to_active_pane(
 842                    Box::new(
 843                        cx.new_view(|cx| ProjectSearchView::new(weak_workspace, model, cx, None)),
 844                    ),
 845                    None,
 846                    true,
 847                    cx,
 848                );
 849            }
 850        }
 851    }
 852
 853    // Add another search tab to the workspace.
 854    fn new_search(
 855        workspace: &mut Workspace,
 856        _: &workspace::NewSearch,
 857        cx: &mut ViewContext<Workspace>,
 858    ) {
 859        Self::existing_or_new_search(workspace, None, &DeploySearch::find(), cx)
 860    }
 861
 862    fn existing_or_new_search(
 863        workspace: &mut Workspace,
 864        existing: Option<View<ProjectSearchView>>,
 865        action: &workspace::DeploySearch,
 866        cx: &mut ViewContext<Workspace>,
 867    ) {
 868        let query = workspace.active_item(cx).and_then(|item| {
 869            if let Some(buffer_search_query) = buffer_search_query(workspace, item.as_ref(), cx) {
 870                return Some(buffer_search_query);
 871            }
 872
 873            let editor = item.act_as::<Editor>(cx)?;
 874            let query = editor.query_suggestion(cx);
 875            if query.is_empty() {
 876                None
 877            } else {
 878                Some(query)
 879            }
 880        });
 881
 882        let search = if let Some(existing) = existing {
 883            workspace.activate_item(&existing, true, true, cx);
 884            existing
 885        } else {
 886            let settings = cx
 887                .global::<ActiveSettings>()
 888                .0
 889                .get(&workspace.project().downgrade());
 890
 891            let settings = settings.cloned();
 892
 893            let weak_workspace = cx.view().downgrade();
 894
 895            let model = cx.new_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
 896            let view =
 897                cx.new_view(|cx| ProjectSearchView::new(weak_workspace, model, cx, settings));
 898
 899            workspace.add_item_to_active_pane(Box::new(view.clone()), None, true, cx);
 900            view
 901        };
 902
 903        search.update(cx, |search, cx| {
 904            search.replace_enabled = action.replace_enabled;
 905            if let Some(query) = query {
 906                search.set_query(&query, cx);
 907            }
 908            search.focus_query_editor(cx)
 909        });
 910    }
 911
 912    fn search(&mut self, cx: &mut ViewContext<Self>) {
 913        if let Some(query) = self.build_search_query(cx) {
 914            self.model.update(cx, |model, cx| model.search(query, cx));
 915        }
 916    }
 917
 918    pub fn search_query_text(&self, cx: &WindowContext) -> String {
 919        self.query_editor.read(cx).text(cx)
 920    }
 921
 922    fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
 923        // Do not bail early in this function, as we want to fill out `self.panels_with_errors`.
 924        let text = self.query_editor.read(cx).text(cx);
 925        let open_buffers = if self.included_opened_only {
 926            Some(self.open_buffers(cx))
 927        } else {
 928            None
 929        };
 930        let included_files =
 931            match Self::parse_path_matches(&self.included_files_editor.read(cx).text(cx)) {
 932                Ok(included_files) => {
 933                    let should_unmark_error = self.panels_with_errors.remove(&InputPanel::Include);
 934                    if should_unmark_error {
 935                        cx.notify();
 936                    }
 937                    included_files
 938                }
 939                Err(_e) => {
 940                    let should_mark_error = self.panels_with_errors.insert(InputPanel::Include);
 941                    if should_mark_error {
 942                        cx.notify();
 943                    }
 944                    PathMatcher::default()
 945                }
 946            };
 947        let excluded_files =
 948            match Self::parse_path_matches(&self.excluded_files_editor.read(cx).text(cx)) {
 949                Ok(excluded_files) => {
 950                    let should_unmark_error = self.panels_with_errors.remove(&InputPanel::Exclude);
 951                    if should_unmark_error {
 952                        cx.notify();
 953                    }
 954
 955                    excluded_files
 956                }
 957                Err(_e) => {
 958                    let should_mark_error = self.panels_with_errors.insert(InputPanel::Exclude);
 959                    if should_mark_error {
 960                        cx.notify();
 961                    }
 962                    PathMatcher::default()
 963                }
 964            };
 965
 966        let query = if self.search_options.contains(SearchOptions::REGEX) {
 967            match SearchQuery::regex(
 968                text,
 969                self.search_options.contains(SearchOptions::WHOLE_WORD),
 970                self.search_options.contains(SearchOptions::CASE_SENSITIVE),
 971                self.search_options.contains(SearchOptions::INCLUDE_IGNORED),
 972                included_files,
 973                excluded_files,
 974                open_buffers,
 975            ) {
 976                Ok(query) => {
 977                    let should_unmark_error = self.panels_with_errors.remove(&InputPanel::Query);
 978                    if should_unmark_error {
 979                        cx.notify();
 980                    }
 981
 982                    Some(query)
 983                }
 984                Err(_e) => {
 985                    let should_mark_error = self.panels_with_errors.insert(InputPanel::Query);
 986                    if should_mark_error {
 987                        cx.notify();
 988                    }
 989
 990                    None
 991                }
 992            }
 993        } else {
 994            match SearchQuery::text(
 995                text,
 996                self.search_options.contains(SearchOptions::WHOLE_WORD),
 997                self.search_options.contains(SearchOptions::CASE_SENSITIVE),
 998                self.search_options.contains(SearchOptions::INCLUDE_IGNORED),
 999                included_files,
1000                excluded_files,
1001                open_buffers,
1002            ) {
1003                Ok(query) => {
1004                    let should_unmark_error = self.panels_with_errors.remove(&InputPanel::Query);
1005                    if should_unmark_error {
1006                        cx.notify();
1007                    }
1008
1009                    Some(query)
1010                }
1011                Err(_e) => {
1012                    let should_mark_error = self.panels_with_errors.insert(InputPanel::Query);
1013                    if should_mark_error {
1014                        cx.notify();
1015                    }
1016
1017                    None
1018                }
1019            }
1020        };
1021        if !self.panels_with_errors.is_empty() {
1022            return None;
1023        }
1024        if query.as_ref().is_some_and(|query| query.is_empty()) {
1025            return None;
1026        }
1027        query
1028    }
1029
1030    fn open_buffers(&self, cx: &mut ViewContext<Self>) -> Vec<Model<Buffer>> {
1031        let mut buffers = Vec::new();
1032        self.workspace
1033            .update(cx, |workspace, cx| {
1034                for editor in workspace.items_of_type::<Editor>(cx) {
1035                    if let Some(buffer) = editor.read(cx).buffer().read(cx).as_singleton() {
1036                        buffers.push(buffer);
1037                    }
1038                }
1039            })
1040            .ok();
1041        buffers
1042    }
1043
1044    fn parse_path_matches(text: &str) -> anyhow::Result<PathMatcher> {
1045        let queries = text
1046            .split(',')
1047            .map(str::trim)
1048            .filter(|maybe_glob_str| !maybe_glob_str.is_empty())
1049            .map(str::to_owned)
1050            .collect::<Vec<_>>();
1051        Ok(PathMatcher::new(&queries)?)
1052    }
1053
1054    fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
1055        if let Some(index) = self.active_match_index {
1056            let match_ranges = self.model.read(cx).match_ranges.clone();
1057
1058            if !EditorSettings::get_global(cx).search_wrap
1059                && ((direction == Direction::Next && index + 1 >= match_ranges.len())
1060                    || (direction == Direction::Prev && index == 0))
1061            {
1062                crate::show_no_more_matches(cx);
1063                return;
1064            }
1065
1066            let new_index = self.results_editor.update(cx, |editor, cx| {
1067                editor.match_index_for_direction(&match_ranges, index, direction, 1, cx)
1068            });
1069
1070            let range_to_select = match_ranges[new_index].clone();
1071            self.results_editor.update(cx, |editor, cx| {
1072                let range_to_select = editor.range_for_match(&range_to_select);
1073                editor.unfold_ranges(&[range_to_select.clone()], false, true, cx);
1074                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
1075                    s.select_ranges([range_to_select])
1076                });
1077            });
1078        }
1079    }
1080
1081    fn focus_query_editor(&mut self, cx: &mut ViewContext<Self>) {
1082        self.query_editor.update(cx, |query_editor, cx| {
1083            query_editor.select_all(&SelectAll, cx);
1084        });
1085        let editor_handle = self.query_editor.focus_handle(cx);
1086        cx.focus(&editor_handle);
1087    }
1088
1089    fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
1090        self.set_search_editor(SearchInputKind::Query, query, cx);
1091        if EditorSettings::get_global(cx).use_smartcase_search
1092            && !query.is_empty()
1093            && self.search_options.contains(SearchOptions::CASE_SENSITIVE)
1094                != is_contains_uppercase(query)
1095        {
1096            self.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx)
1097        }
1098    }
1099
1100    fn set_search_editor(&mut self, kind: SearchInputKind, text: &str, cx: &mut ViewContext<Self>) {
1101        let editor = match kind {
1102            SearchInputKind::Query => &self.query_editor,
1103            SearchInputKind::Include => &self.included_files_editor,
1104
1105            SearchInputKind::Exclude => &self.excluded_files_editor,
1106        };
1107        editor.update(cx, |included_editor, cx| included_editor.set_text(text, cx));
1108    }
1109
1110    fn focus_results_editor(&mut self, cx: &mut ViewContext<Self>) {
1111        self.query_editor.update(cx, |query_editor, cx| {
1112            let cursor = query_editor.selections.newest_anchor().head();
1113            query_editor.change_selections(None, cx, |s| s.select_ranges([cursor..cursor]));
1114        });
1115        let results_handle = self.results_editor.focus_handle(cx);
1116        cx.focus(&results_handle);
1117    }
1118
1119    fn model_changed(&mut self, cx: &mut ViewContext<Self>) {
1120        let match_ranges = self.model.read(cx).match_ranges.clone();
1121        if match_ranges.is_empty() {
1122            self.active_match_index = None;
1123        } else {
1124            self.active_match_index = Some(0);
1125            self.update_match_index(cx);
1126            let prev_search_id = mem::replace(&mut self.search_id, self.model.read(cx).search_id);
1127            let is_new_search = self.search_id != prev_search_id;
1128            self.results_editor.update(cx, |editor, cx| {
1129                if is_new_search {
1130                    let range_to_select = match_ranges
1131                        .first()
1132                        .map(|range| editor.range_for_match(range));
1133                    editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
1134                        s.select_ranges(range_to_select)
1135                    });
1136                    editor.scroll(Point::default(), Some(Axis::Vertical), cx);
1137                }
1138                editor.highlight_background::<Self>(
1139                    &match_ranges,
1140                    |theme| theme.search_match_background,
1141                    cx,
1142                );
1143            });
1144            if is_new_search && self.query_editor.focus_handle(cx).is_focused(cx) {
1145                self.focus_results_editor(cx);
1146            }
1147        }
1148
1149        cx.emit(ViewEvent::UpdateTab);
1150        cx.notify();
1151    }
1152
1153    fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
1154        let results_editor = self.results_editor.read(cx);
1155        let new_index = active_match_index(
1156            &self.model.read(cx).match_ranges,
1157            &results_editor.selections.newest_anchor().head(),
1158            &results_editor.buffer().read(cx).snapshot(cx),
1159        );
1160        if self.active_match_index != new_index {
1161            self.active_match_index = new_index;
1162            cx.notify();
1163        }
1164    }
1165
1166    pub fn has_matches(&self) -> bool {
1167        self.active_match_index.is_some()
1168    }
1169
1170    fn landing_text_minor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1171        let focus_handle = self.focus_handle.clone();
1172        v_flex()
1173            .gap_1()
1174            .child(
1175                Label::new("Hit enter to search. For more options:")
1176                    .color(Color::Muted)
1177                    .mb_2(),
1178            )
1179            .child(
1180                Button::new("filter-paths", "Include/exclude specific paths")
1181                    .icon(IconName::Filter)
1182                    .icon_position(IconPosition::Start)
1183                    .icon_size(IconSize::Small)
1184                    .key_binding(KeyBinding::for_action_in(&ToggleFilters, &focus_handle, cx))
1185                    .on_click(|_event, cx| cx.dispatch_action(ToggleFilters.boxed_clone())),
1186            )
1187            .child(
1188                Button::new("find-replace", "Find and replace")
1189                    .icon(IconName::Replace)
1190                    .icon_position(IconPosition::Start)
1191                    .icon_size(IconSize::Small)
1192                    .key_binding(KeyBinding::for_action_in(&ToggleReplace, &focus_handle, cx))
1193                    .on_click(|_event, cx| cx.dispatch_action(ToggleReplace.boxed_clone())),
1194            )
1195            .child(
1196                Button::new("regex", "Match with regex")
1197                    .icon(IconName::Regex)
1198                    .icon_position(IconPosition::Start)
1199                    .icon_size(IconSize::Small)
1200                    .key_binding(KeyBinding::for_action_in(&ToggleRegex, &focus_handle, cx))
1201                    .on_click(|_event, cx| cx.dispatch_action(ToggleRegex.boxed_clone())),
1202            )
1203            .child(
1204                Button::new("match-case", "Match case")
1205                    .icon(IconName::CaseSensitive)
1206                    .icon_position(IconPosition::Start)
1207                    .icon_size(IconSize::Small)
1208                    .key_binding(KeyBinding::for_action_in(
1209                        &ToggleCaseSensitive,
1210                        &focus_handle,
1211                        cx,
1212                    ))
1213                    .on_click(|_event, cx| cx.dispatch_action(ToggleCaseSensitive.boxed_clone())),
1214            )
1215            .child(
1216                Button::new("match-whole-words", "Match whole words")
1217                    .icon(IconName::WholeWord)
1218                    .icon_position(IconPosition::Start)
1219                    .icon_size(IconSize::Small)
1220                    .key_binding(KeyBinding::for_action_in(
1221                        &ToggleWholeWord,
1222                        &focus_handle,
1223                        cx,
1224                    ))
1225                    .on_click(|_event, cx| cx.dispatch_action(ToggleWholeWord.boxed_clone())),
1226            )
1227    }
1228
1229    fn border_color_for(&self, panel: InputPanel, cx: &WindowContext) -> Hsla {
1230        if self.panels_with_errors.contains(&panel) {
1231            Color::Error.color(cx)
1232        } else {
1233            cx.theme().colors().border
1234        }
1235    }
1236
1237    fn move_focus_to_results(&mut self, cx: &mut ViewContext<Self>) {
1238        if !self.results_editor.focus_handle(cx).is_focused(cx)
1239            && !self.model.read(cx).match_ranges.is_empty()
1240        {
1241            cx.stop_propagation();
1242            self.focus_results_editor(cx)
1243        }
1244    }
1245
1246    #[cfg(any(test, feature = "test-support"))]
1247    pub fn results_editor(&self) -> &View<Editor> {
1248        &self.results_editor
1249    }
1250}
1251
1252fn buffer_search_query(
1253    workspace: &mut Workspace,
1254    item: &dyn ItemHandle,
1255    cx: &mut ViewContext<'_, Workspace>,
1256) -> Option<String> {
1257    let buffer_search_bar = workspace
1258        .pane_for(item)
1259        .and_then(|pane| {
1260            pane.read(cx)
1261                .toolbar()
1262                .read(cx)
1263                .item_of_type::<BufferSearchBar>()
1264        })?
1265        .read(cx);
1266    if buffer_search_bar.query_editor_focused() {
1267        let buffer_search_query = buffer_search_bar.query(cx);
1268        if !buffer_search_query.is_empty() {
1269            return Some(buffer_search_query);
1270        }
1271    }
1272    None
1273}
1274
1275impl Default for ProjectSearchBar {
1276    fn default() -> Self {
1277        Self::new()
1278    }
1279}
1280
1281impl ProjectSearchBar {
1282    pub fn new() -> Self {
1283        Self {
1284            active_project_search: None,
1285            subscription: None,
1286        }
1287    }
1288
1289    fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
1290        if let Some(search_view) = self.active_project_search.as_ref() {
1291            search_view.update(cx, |search_view, cx| {
1292                if !search_view
1293                    .replacement_editor
1294                    .focus_handle(cx)
1295                    .is_focused(cx)
1296                {
1297                    cx.stop_propagation();
1298                    search_view.search(cx);
1299                }
1300            });
1301        }
1302    }
1303
1304    fn tab(&mut self, _: &editor::actions::Tab, cx: &mut ViewContext<Self>) {
1305        self.cycle_field(Direction::Next, cx);
1306    }
1307
1308    fn tab_previous(&mut self, _: &editor::actions::TabPrev, cx: &mut ViewContext<Self>) {
1309        self.cycle_field(Direction::Prev, cx);
1310    }
1311
1312    fn focus_search(&mut self, cx: &mut ViewContext<Self>) {
1313        if let Some(search_view) = self.active_project_search.as_ref() {
1314            search_view.update(cx, |search_view, cx| {
1315                search_view.query_editor.focus_handle(cx).focus(cx);
1316            });
1317        }
1318    }
1319
1320    fn cycle_field(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
1321        let active_project_search = match &self.active_project_search {
1322            Some(active_project_search) => active_project_search,
1323
1324            None => {
1325                return;
1326            }
1327        };
1328
1329        active_project_search.update(cx, |project_view, cx| {
1330            let mut views = vec![&project_view.query_editor];
1331            if project_view.replace_enabled {
1332                views.push(&project_view.replacement_editor);
1333            }
1334            if project_view.filters_enabled {
1335                views.extend([
1336                    &project_view.included_files_editor,
1337                    &project_view.excluded_files_editor,
1338                ]);
1339            }
1340            let current_index = match views
1341                .iter()
1342                .enumerate()
1343                .find(|(_, view)| view.focus_handle(cx).is_focused(cx))
1344            {
1345                Some((index, _)) => index,
1346                None => return,
1347            };
1348
1349            let new_index = match direction {
1350                Direction::Next => (current_index + 1) % views.len(),
1351                Direction::Prev if current_index == 0 => views.len() - 1,
1352                Direction::Prev => (current_index - 1) % views.len(),
1353            };
1354            let next_focus_handle = views[new_index].focus_handle(cx);
1355            cx.focus(&next_focus_handle);
1356            cx.stop_propagation();
1357        });
1358    }
1359
1360    fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext<Self>) -> bool {
1361        if let Some(search_view) = self.active_project_search.as_ref() {
1362            search_view.update(cx, |search_view, cx| {
1363                search_view.toggle_search_option(option, cx);
1364                if search_view.model.read(cx).active_query.is_some() {
1365                    search_view.search(cx);
1366                }
1367            });
1368
1369            cx.notify();
1370            true
1371        } else {
1372            false
1373        }
1374    }
1375
1376    fn toggle_replace(&mut self, _: &ToggleReplace, cx: &mut ViewContext<Self>) {
1377        if let Some(search) = &self.active_project_search {
1378            search.update(cx, |this, cx| {
1379                this.replace_enabled = !this.replace_enabled;
1380                let editor_to_focus = if this.replace_enabled {
1381                    this.replacement_editor.focus_handle(cx)
1382                } else {
1383                    this.query_editor.focus_handle(cx)
1384                };
1385                cx.focus(&editor_to_focus);
1386                cx.notify();
1387            });
1388        }
1389    }
1390
1391    fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) -> bool {
1392        if let Some(search_view) = self.active_project_search.as_ref() {
1393            search_view.update(cx, |search_view, cx| {
1394                search_view.toggle_filters(cx);
1395                search_view
1396                    .included_files_editor
1397                    .update(cx, |_, cx| cx.notify());
1398                search_view
1399                    .excluded_files_editor
1400                    .update(cx, |_, cx| cx.notify());
1401                cx.refresh();
1402                cx.notify();
1403            });
1404            cx.notify();
1405            true
1406        } else {
1407            false
1408        }
1409    }
1410
1411    fn toggle_opened_only(&mut self, cx: &mut ViewContext<Self>) -> bool {
1412        if let Some(search_view) = self.active_project_search.as_ref() {
1413            search_view.update(cx, |search_view, cx| {
1414                search_view.toggle_opened_only(cx);
1415                if search_view.model.read(cx).active_query.is_some() {
1416                    search_view.search(cx);
1417                }
1418            });
1419
1420            cx.notify();
1421            true
1422        } else {
1423            false
1424        }
1425    }
1426
1427    fn is_opened_only_enabled(&self, cx: &AppContext) -> bool {
1428        if let Some(search_view) = self.active_project_search.as_ref() {
1429            search_view.read(cx).included_opened_only
1430        } else {
1431            false
1432        }
1433    }
1434
1435    fn move_focus_to_results(&self, cx: &mut ViewContext<Self>) {
1436        if let Some(search_view) = self.active_project_search.as_ref() {
1437            search_view.update(cx, |search_view, cx| {
1438                search_view.move_focus_to_results(cx);
1439            });
1440            cx.notify();
1441        }
1442    }
1443
1444    fn is_option_enabled(&self, option: SearchOptions, cx: &AppContext) -> bool {
1445        if let Some(search) = self.active_project_search.as_ref() {
1446            search.read(cx).search_options.contains(option)
1447        } else {
1448            false
1449        }
1450    }
1451
1452    fn next_history_query(&mut self, _: &NextHistoryQuery, cx: &mut ViewContext<Self>) {
1453        if let Some(search_view) = self.active_project_search.as_ref() {
1454            search_view.update(cx, |search_view, cx| {
1455                for (editor, kind) in [
1456                    (search_view.query_editor.clone(), SearchInputKind::Query),
1457                    (
1458                        search_view.included_files_editor.clone(),
1459                        SearchInputKind::Include,
1460                    ),
1461                    (
1462                        search_view.excluded_files_editor.clone(),
1463                        SearchInputKind::Exclude,
1464                    ),
1465                ] {
1466                    if editor.focus_handle(cx).is_focused(cx) {
1467                        let new_query = search_view.model.update(cx, |model, cx| {
1468                            let project = model.project.clone();
1469
1470                            if let Some(new_query) = project.update(cx, |project, _| {
1471                                project
1472                                    .search_history_mut(kind)
1473                                    .next(model.cursor_mut(kind))
1474                                    .map(str::to_string)
1475                            }) {
1476                                new_query
1477                            } else {
1478                                model.cursor_mut(kind).reset();
1479                                String::new()
1480                            }
1481                        });
1482                        search_view.set_search_editor(kind, &new_query, cx);
1483                    }
1484                }
1485            });
1486        }
1487    }
1488
1489    fn previous_history_query(&mut self, _: &PreviousHistoryQuery, cx: &mut ViewContext<Self>) {
1490        if let Some(search_view) = self.active_project_search.as_ref() {
1491            search_view.update(cx, |search_view, cx| {
1492                for (editor, kind) in [
1493                    (search_view.query_editor.clone(), SearchInputKind::Query),
1494                    (
1495                        search_view.included_files_editor.clone(),
1496                        SearchInputKind::Include,
1497                    ),
1498                    (
1499                        search_view.excluded_files_editor.clone(),
1500                        SearchInputKind::Exclude,
1501                    ),
1502                ] {
1503                    if editor.focus_handle(cx).is_focused(cx) {
1504                        if editor.read(cx).text(cx).is_empty() {
1505                            if let Some(new_query) = search_view
1506                                .model
1507                                .read(cx)
1508                                .project
1509                                .read(cx)
1510                                .search_history(kind)
1511                                .current(search_view.model.read(cx).cursor(kind))
1512                                .map(str::to_string)
1513                            {
1514                                search_view.set_search_editor(kind, &new_query, cx);
1515                                return;
1516                            }
1517                        }
1518
1519                        if let Some(new_query) = search_view.model.update(cx, |model, cx| {
1520                            let project = model.project.clone();
1521                            project.update(cx, |project, _| {
1522                                project
1523                                    .search_history_mut(kind)
1524                                    .previous(model.cursor_mut(kind))
1525                                    .map(str::to_string)
1526                            })
1527                        }) {
1528                            search_view.set_search_editor(kind, &new_query, cx);
1529                        }
1530                    }
1531                }
1532            });
1533        }
1534    }
1535
1536    fn select_next_match(&mut self, _: &SelectNextMatch, cx: &mut ViewContext<Self>) {
1537        if let Some(search) = self.active_project_search.as_ref() {
1538            search.update(cx, |this, cx| {
1539                this.select_match(Direction::Next, cx);
1540            })
1541        }
1542    }
1543
1544    fn select_prev_match(&mut self, _: &SelectPrevMatch, cx: &mut ViewContext<Self>) {
1545        if let Some(search) = self.active_project_search.as_ref() {
1546            search.update(cx, |this, cx| {
1547                this.select_match(Direction::Prev, cx);
1548            })
1549        }
1550    }
1551
1552    fn render_text_input(&self, editor: &View<Editor>, cx: &ViewContext<Self>) -> impl IntoElement {
1553        let settings = ThemeSettings::get_global(cx);
1554        let text_style = TextStyle {
1555            color: if editor.read(cx).read_only(cx) {
1556                cx.theme().colors().text_disabled
1557            } else {
1558                cx.theme().colors().text
1559            },
1560            font_family: settings.buffer_font.family.clone(),
1561            font_features: settings.buffer_font.features.clone(),
1562            font_fallbacks: settings.buffer_font.fallbacks.clone(),
1563            font_size: rems(0.875).into(),
1564            font_weight: settings.buffer_font.weight,
1565            line_height: relative(1.3),
1566            ..Default::default()
1567        };
1568
1569        EditorElement::new(
1570            editor,
1571            EditorStyle {
1572                background: cx.theme().colors().editor_background,
1573                local_player: cx.theme().players().local(),
1574                text: text_style,
1575                ..Default::default()
1576            },
1577        )
1578    }
1579}
1580
1581impl Render for ProjectSearchBar {
1582    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1583        let Some(search) = self.active_project_search.clone() else {
1584            return div();
1585        };
1586        let search = search.read(cx);
1587        let focus_handle = search.focus_handle(cx);
1588
1589        let input_base_styles = || {
1590            h_flex()
1591                .w_full()
1592                .h_8()
1593                .px_2()
1594                .py_1()
1595                .border_1()
1596                .border_color(search.border_color_for(InputPanel::Query, cx))
1597                .rounded_lg()
1598        };
1599
1600        let query_column = input_base_styles()
1601            .on_action(cx.listener(|this, action, cx| this.confirm(action, cx)))
1602            .on_action(cx.listener(|this, action, cx| this.previous_history_query(action, cx)))
1603            .on_action(cx.listener(|this, action, cx| this.next_history_query(action, cx)))
1604            .child(self.render_text_input(&search.query_editor, cx))
1605            .child(
1606                h_flex()
1607                    .gap_0p5()
1608                    .child(SearchOptions::CASE_SENSITIVE.as_button(
1609                        self.is_option_enabled(SearchOptions::CASE_SENSITIVE, cx),
1610                        focus_handle.clone(),
1611                        cx.listener(|this, _, cx| {
1612                            this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
1613                        }),
1614                    ))
1615                    .child(SearchOptions::WHOLE_WORD.as_button(
1616                        self.is_option_enabled(SearchOptions::WHOLE_WORD, cx),
1617                        focus_handle.clone(),
1618                        cx.listener(|this, _, cx| {
1619                            this.toggle_search_option(SearchOptions::WHOLE_WORD, cx);
1620                        }),
1621                    ))
1622                    .child(SearchOptions::REGEX.as_button(
1623                        self.is_option_enabled(SearchOptions::REGEX, cx),
1624                        focus_handle.clone(),
1625                        cx.listener(|this, _, cx| {
1626                            this.toggle_search_option(SearchOptions::REGEX, cx);
1627                        }),
1628                    )),
1629            );
1630
1631        let mode_column = h_flex()
1632            .gap_1()
1633            .child(
1634                IconButton::new("project-search-filter-button", IconName::Filter)
1635                    .shape(IconButtonShape::Square)
1636                    .tooltip(|cx| Tooltip::for_action("Toggle Filters", &ToggleFilters, cx))
1637                    .on_click(cx.listener(|this, _, cx| {
1638                        this.toggle_filters(cx);
1639                    }))
1640                    .selected(
1641                        self.active_project_search
1642                            .as_ref()
1643                            .map(|search| search.read(cx).filters_enabled)
1644                            .unwrap_or_default(),
1645                    )
1646                    .tooltip({
1647                        let focus_handle = focus_handle.clone();
1648                        move |cx| {
1649                            Tooltip::for_action_in(
1650                                "Toggle Filters",
1651                                &ToggleFilters,
1652                                &focus_handle,
1653                                cx,
1654                            )
1655                        }
1656                    }),
1657            )
1658            .child(
1659                IconButton::new("project-search-toggle-replace", IconName::Replace)
1660                    .shape(IconButtonShape::Square)
1661                    .on_click(cx.listener(|this, _, cx| {
1662                        this.toggle_replace(&ToggleReplace, cx);
1663                    }))
1664                    .selected(
1665                        self.active_project_search
1666                            .as_ref()
1667                            .map(|search| search.read(cx).replace_enabled)
1668                            .unwrap_or_default(),
1669                    )
1670                    .tooltip({
1671                        let focus_handle = focus_handle.clone();
1672                        move |cx| {
1673                            Tooltip::for_action_in(
1674                                "Toggle Replace",
1675                                &ToggleReplace,
1676                                &focus_handle,
1677                                cx,
1678                            )
1679                        }
1680                    }),
1681            );
1682
1683        let limit_reached = search.model.read(cx).limit_reached;
1684
1685        let match_text = search
1686            .active_match_index
1687            .and_then(|index| {
1688                let index = index + 1;
1689                let match_quantity = search.model.read(cx).match_ranges.len();
1690                if match_quantity > 0 {
1691                    debug_assert!(match_quantity >= index);
1692                    if limit_reached {
1693                        Some(format!("{index}/{match_quantity}+").to_string())
1694                    } else {
1695                        Some(format!("{index}/{match_quantity}").to_string())
1696                    }
1697                } else {
1698                    None
1699                }
1700            })
1701            .unwrap_or_else(|| "0/0".to_string());
1702
1703        let matches_column = h_flex()
1704            .child(
1705                IconButton::new("project-search-prev-match", IconName::ChevronLeft)
1706                    .shape(IconButtonShape::Square)
1707                    .disabled(search.active_match_index.is_none())
1708                    .on_click(cx.listener(|this, _, cx| {
1709                        if let Some(search) = this.active_project_search.as_ref() {
1710                            search.update(cx, |this, cx| {
1711                                this.select_match(Direction::Prev, cx);
1712                            })
1713                        }
1714                    }))
1715                    .tooltip({
1716                        let focus_handle = focus_handle.clone();
1717                        move |cx| {
1718                            Tooltip::for_action_in(
1719                                "Go To Previous Match",
1720                                &SelectPrevMatch,
1721                                &focus_handle,
1722                                cx,
1723                            )
1724                        }
1725                    }),
1726            )
1727            .child(
1728                IconButton::new("project-search-next-match", IconName::ChevronRight)
1729                    .shape(IconButtonShape::Square)
1730                    .disabled(search.active_match_index.is_none())
1731                    .on_click(cx.listener(|this, _, cx| {
1732                        if let Some(search) = this.active_project_search.as_ref() {
1733                            search.update(cx, |this, cx| {
1734                                this.select_match(Direction::Next, cx);
1735                            })
1736                        }
1737                    }))
1738                    .tooltip({
1739                        let focus_handle = focus_handle.clone();
1740                        move |cx| {
1741                            Tooltip::for_action_in(
1742                                "Go To Next Match",
1743                                &SelectNextMatch,
1744                                &focus_handle,
1745                                cx,
1746                            )
1747                        }
1748                    }),
1749            )
1750            .child(
1751                div()
1752                    .id("matches")
1753                    .ml_1()
1754                    .child(
1755                        Label::new(match_text).color(if search.active_match_index.is_some() {
1756                            Color::Default
1757                        } else {
1758                            Color::Disabled
1759                        }),
1760                    )
1761                    .when(limit_reached, |el| {
1762                        el.tooltip(|cx| {
1763                            Tooltip::text("Search limits reached.\nTry narrowing your search.", cx)
1764                        })
1765                    }),
1766            );
1767
1768        let search_line = h_flex()
1769            .w_full()
1770            .gap_1p5()
1771            .child(query_column)
1772            .child(h_flex().min_w_40().child(mode_column).child(matches_column));
1773
1774        let replace_line = search.replace_enabled.then(|| {
1775            let replace_column =
1776                input_base_styles().child(self.render_text_input(&search.replacement_editor, cx));
1777
1778            let focus_handle = search.replacement_editor.read(cx).focus_handle(cx);
1779
1780            let replace_actions =
1781                h_flex()
1782                    .min_w_40()
1783                    .gap_1()
1784                    .when(search.replace_enabled, |this| {
1785                        this.child(
1786                            IconButton::new("project-search-replace-next", IconName::ReplaceNext)
1787                                .shape(IconButtonShape::Square)
1788                                .on_click(cx.listener(|this, _, cx| {
1789                                    if let Some(search) = this.active_project_search.as_ref() {
1790                                        search.update(cx, |this, cx| {
1791                                            this.replace_next(&ReplaceNext, cx);
1792                                        })
1793                                    }
1794                                }))
1795                                .tooltip({
1796                                    let focus_handle = focus_handle.clone();
1797                                    move |cx| {
1798                                        Tooltip::for_action_in(
1799                                            "Replace Next Match",
1800                                            &ReplaceNext,
1801                                            &focus_handle,
1802                                            cx,
1803                                        )
1804                                    }
1805                                }),
1806                        )
1807                        .child(
1808                            IconButton::new("project-search-replace-all", IconName::ReplaceAll)
1809                                .shape(IconButtonShape::Square)
1810                                .on_click(cx.listener(|this, _, cx| {
1811                                    if let Some(search) = this.active_project_search.as_ref() {
1812                                        search.update(cx, |this, cx| {
1813                                            this.replace_all(&ReplaceAll, cx);
1814                                        })
1815                                    }
1816                                }))
1817                                .tooltip({
1818                                    let focus_handle = focus_handle.clone();
1819                                    move |cx| {
1820                                        Tooltip::for_action_in(
1821                                            "Replace All Matches",
1822                                            &ReplaceAll,
1823                                            &focus_handle,
1824                                            cx,
1825                                        )
1826                                    }
1827                                }),
1828                        )
1829                    });
1830
1831            h_flex()
1832                .w_full()
1833                .gap_1p5()
1834                .child(replace_column)
1835                .child(replace_actions)
1836        });
1837
1838        let filter_line = search.filters_enabled.then(|| {
1839            h_flex()
1840                .w_full()
1841                .gap_1p5()
1842                .child(
1843                    input_base_styles()
1844                        .on_action(
1845                            cx.listener(|this, action, cx| this.previous_history_query(action, cx)),
1846                        )
1847                        .on_action(
1848                            cx.listener(|this, action, cx| this.next_history_query(action, cx)),
1849                        )
1850                        .child(self.render_text_input(&search.included_files_editor, cx)),
1851                )
1852                .child(
1853                    input_base_styles()
1854                        .on_action(
1855                            cx.listener(|this, action, cx| this.previous_history_query(action, cx)),
1856                        )
1857                        .on_action(
1858                            cx.listener(|this, action, cx| this.next_history_query(action, cx)),
1859                        )
1860                        .child(self.render_text_input(&search.excluded_files_editor, cx)),
1861                )
1862                .child(
1863                    h_flex()
1864                        .min_w_40()
1865                        .gap_1()
1866                        .child(
1867                            IconButton::new("project-search-opened-only", IconName::FileSearch)
1868                                .shape(IconButtonShape::Square)
1869                                .icon_size(IconSize::XSmall)
1870                                .selected(self.is_opened_only_enabled(cx))
1871                                .tooltip(|cx| Tooltip::text("Only Search Open Files", cx))
1872                                .on_click(cx.listener(|this, _, cx| {
1873                                    this.toggle_opened_only(cx);
1874                                })),
1875                        )
1876                        .child(
1877                            SearchOptions::INCLUDE_IGNORED.as_button(
1878                                search
1879                                    .search_options
1880                                    .contains(SearchOptions::INCLUDE_IGNORED),
1881                                focus_handle.clone(),
1882                                cx.listener(|this, _, cx| {
1883                                    this.toggle_search_option(SearchOptions::INCLUDE_IGNORED, cx);
1884                                }),
1885                            ),
1886                        ),
1887                )
1888        });
1889
1890        let mut key_context = KeyContext::default();
1891
1892        key_context.add("ProjectSearchBar");
1893
1894        if search.replacement_editor.focus_handle(cx).is_focused(cx) {
1895            key_context.add("in_replace");
1896        }
1897
1898        v_flex()
1899            .key_context(key_context)
1900            .on_action(cx.listener(|this, _: &ToggleFocus, cx| this.move_focus_to_results(cx)))
1901            .on_action(cx.listener(|this, _: &ToggleFilters, cx| {
1902                this.toggle_filters(cx);
1903            }))
1904            .capture_action(cx.listener(|this, action, cx| {
1905                this.tab(action, cx);
1906                cx.stop_propagation();
1907            }))
1908            .capture_action(cx.listener(|this, action, cx| {
1909                this.tab_previous(action, cx);
1910                cx.stop_propagation();
1911            }))
1912            .on_action(cx.listener(|this, action, cx| this.confirm(action, cx)))
1913            .on_action(cx.listener(|this, action, cx| {
1914                this.toggle_replace(action, cx);
1915            }))
1916            .on_action(cx.listener(|this, _: &ToggleWholeWord, cx| {
1917                this.toggle_search_option(SearchOptions::WHOLE_WORD, cx);
1918            }))
1919            .on_action(cx.listener(|this, _: &ToggleCaseSensitive, cx| {
1920                this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
1921            }))
1922            .on_action(cx.listener(|this, action, cx| {
1923                if let Some(search) = this.active_project_search.as_ref() {
1924                    search.update(cx, |this, cx| {
1925                        this.replace_next(action, cx);
1926                    })
1927                }
1928            }))
1929            .on_action(cx.listener(|this, action, cx| {
1930                if let Some(search) = this.active_project_search.as_ref() {
1931                    search.update(cx, |this, cx| {
1932                        this.replace_all(action, cx);
1933                    })
1934                }
1935            }))
1936            .when(search.filters_enabled, |this| {
1937                this.on_action(cx.listener(|this, _: &ToggleIncludeIgnored, cx| {
1938                    this.toggle_search_option(SearchOptions::INCLUDE_IGNORED, cx);
1939                }))
1940            })
1941            .on_action(cx.listener(Self::select_next_match))
1942            .on_action(cx.listener(Self::select_prev_match))
1943            .gap_2()
1944            .w_full()
1945            .child(search_line)
1946            .children(replace_line)
1947            .children(filter_line)
1948    }
1949}
1950
1951impl EventEmitter<ToolbarItemEvent> for ProjectSearchBar {}
1952
1953impl ToolbarItemView for ProjectSearchBar {
1954    fn set_active_pane_item(
1955        &mut self,
1956        active_pane_item: Option<&dyn ItemHandle>,
1957        cx: &mut ViewContext<Self>,
1958    ) -> ToolbarItemLocation {
1959        cx.notify();
1960        self.subscription = None;
1961        self.active_project_search = None;
1962        if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
1963            self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
1964            self.active_project_search = Some(search);
1965            ToolbarItemLocation::PrimaryLeft {}
1966        } else {
1967            ToolbarItemLocation::Hidden
1968        }
1969    }
1970}
1971
1972fn register_workspace_action<A: Action>(
1973    workspace: &mut Workspace,
1974    callback: fn(&mut ProjectSearchBar, &A, &mut ViewContext<ProjectSearchBar>),
1975) {
1976    workspace.register_action(move |workspace, action: &A, cx| {
1977        if workspace.has_active_modal(cx) {
1978            cx.propagate();
1979            return;
1980        }
1981
1982        workspace.active_pane().update(cx, |pane, cx| {
1983            pane.toolbar().update(cx, move |workspace, cx| {
1984                if let Some(search_bar) = workspace.item_of_type::<ProjectSearchBar>() {
1985                    search_bar.update(cx, move |search_bar, cx| {
1986                        if search_bar.active_project_search.is_some() {
1987                            callback(search_bar, action, cx);
1988                            cx.notify();
1989                        } else {
1990                            cx.propagate();
1991                        }
1992                    });
1993                }
1994            });
1995        })
1996    });
1997}
1998
1999fn register_workspace_action_for_present_search<A: Action>(
2000    workspace: &mut Workspace,
2001    callback: fn(&mut Workspace, &A, &mut ViewContext<Workspace>),
2002) {
2003    workspace.register_action(move |workspace, action: &A, cx| {
2004        if workspace.has_active_modal(cx) {
2005            cx.propagate();
2006            return;
2007        }
2008
2009        let should_notify = workspace
2010            .active_pane()
2011            .read(cx)
2012            .toolbar()
2013            .read(cx)
2014            .item_of_type::<ProjectSearchBar>()
2015            .map(|search_bar| search_bar.read(cx).active_project_search.is_some())
2016            .unwrap_or(false);
2017        if should_notify {
2018            callback(workspace, action, cx);
2019            cx.notify();
2020        } else {
2021            cx.propagate();
2022        }
2023    });
2024}
2025
2026#[cfg(any(test, feature = "test-support"))]
2027pub fn perform_project_search(
2028    search_view: &View<ProjectSearchView>,
2029    text: impl Into<std::sync::Arc<str>>,
2030    cx: &mut gpui::VisualTestContext,
2031) {
2032    cx.run_until_parked();
2033    search_view.update(cx, |search_view, cx| {
2034        search_view
2035            .query_editor
2036            .update(cx, |query_editor, cx| query_editor.set_text(text, cx));
2037        search_view.search(cx);
2038    });
2039    cx.run_until_parked();
2040}
2041
2042#[cfg(test)]
2043pub mod tests {
2044    use std::{ops::Deref as _, sync::Arc};
2045
2046    use super::*;
2047    use editor::{display_map::DisplayRow, DisplayPoint};
2048    use gpui::{Action, TestAppContext, VisualTestContext, WindowHandle};
2049    use project::FakeFs;
2050    use serde_json::json;
2051    use settings::SettingsStore;
2052    use workspace::DeploySearch;
2053
2054    #[gpui::test]
2055    async fn test_project_search(cx: &mut TestAppContext) {
2056        init_test(cx);
2057
2058        let fs = FakeFs::new(cx.background_executor.clone());
2059        fs.insert_tree(
2060            "/dir",
2061            json!({
2062                "one.rs": "const ONE: usize = 1;",
2063                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2064                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2065                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2066            }),
2067        )
2068        .await;
2069        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
2070        let window = cx.add_window(|cx| Workspace::test_new(project.clone(), cx));
2071        let workspace = window.root(cx).unwrap();
2072        let search = cx.new_model(|cx| ProjectSearch::new(project.clone(), cx));
2073        let search_view = cx.add_window(|cx| {
2074            ProjectSearchView::new(workspace.downgrade(), search.clone(), cx, None)
2075        });
2076
2077        perform_search(search_view, "TWO", cx);
2078        search_view.update(cx, |search_view, cx| {
2079            assert_eq!(
2080                search_view
2081                    .results_editor
2082                    .update(cx, |editor, cx| editor.display_text(cx)),
2083                "\n\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\n\n\nconst TWO: usize = one::ONE + one::ONE;\n"
2084            );
2085            let match_background_color = cx.theme().colors().search_match_background;
2086            assert_eq!(
2087                search_view
2088                    .results_editor
2089                    .update(cx, |editor, cx| editor.all_text_background_highlights(cx)),
2090                &[
2091                    (
2092                        DisplayPoint::new(DisplayRow(3), 32)..DisplayPoint::new(DisplayRow(3), 35),
2093                        match_background_color
2094                    ),
2095                    (
2096                        DisplayPoint::new(DisplayRow(3), 37)..DisplayPoint::new(DisplayRow(3), 40),
2097                        match_background_color
2098                    ),
2099                    (
2100                        DisplayPoint::new(DisplayRow(8), 6)..DisplayPoint::new(DisplayRow(8), 9),
2101                        match_background_color
2102                    )
2103                ]
2104            );
2105            assert_eq!(search_view.active_match_index, Some(0));
2106            assert_eq!(
2107                search_view
2108                    .results_editor
2109                    .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
2110                [DisplayPoint::new(DisplayRow(3), 32)..DisplayPoint::new(DisplayRow(3), 35)]
2111            );
2112
2113            search_view.select_match(Direction::Next, cx);
2114        }).unwrap();
2115
2116        search_view
2117            .update(cx, |search_view, cx| {
2118                assert_eq!(search_view.active_match_index, Some(1));
2119                assert_eq!(
2120                    search_view
2121                        .results_editor
2122                        .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
2123                    [DisplayPoint::new(DisplayRow(3), 37)..DisplayPoint::new(DisplayRow(3), 40)]
2124                );
2125                search_view.select_match(Direction::Next, cx);
2126            })
2127            .unwrap();
2128
2129        search_view
2130            .update(cx, |search_view, cx| {
2131                assert_eq!(search_view.active_match_index, Some(2));
2132                assert_eq!(
2133                    search_view
2134                        .results_editor
2135                        .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
2136                    [DisplayPoint::new(DisplayRow(8), 6)..DisplayPoint::new(DisplayRow(8), 9)]
2137                );
2138                search_view.select_match(Direction::Next, cx);
2139            })
2140            .unwrap();
2141
2142        search_view
2143            .update(cx, |search_view, cx| {
2144                assert_eq!(search_view.active_match_index, Some(0));
2145                assert_eq!(
2146                    search_view
2147                        .results_editor
2148                        .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
2149                    [DisplayPoint::new(DisplayRow(3), 32)..DisplayPoint::new(DisplayRow(3), 35)]
2150                );
2151                search_view.select_match(Direction::Prev, cx);
2152            })
2153            .unwrap();
2154
2155        search_view
2156            .update(cx, |search_view, cx| {
2157                assert_eq!(search_view.active_match_index, Some(2));
2158                assert_eq!(
2159                    search_view
2160                        .results_editor
2161                        .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
2162                    [DisplayPoint::new(DisplayRow(8), 6)..DisplayPoint::new(DisplayRow(8), 9)]
2163                );
2164                search_view.select_match(Direction::Prev, cx);
2165            })
2166            .unwrap();
2167
2168        search_view
2169            .update(cx, |search_view, cx| {
2170                assert_eq!(search_view.active_match_index, Some(1));
2171                assert_eq!(
2172                    search_view
2173                        .results_editor
2174                        .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
2175                    [DisplayPoint::new(DisplayRow(3), 37)..DisplayPoint::new(DisplayRow(3), 40)]
2176                );
2177            })
2178            .unwrap();
2179    }
2180
2181    #[gpui::test]
2182    async fn test_deploy_project_search_focus(cx: &mut TestAppContext) {
2183        init_test(cx);
2184
2185        let fs = FakeFs::new(cx.background_executor.clone());
2186        fs.insert_tree(
2187            "/dir",
2188            json!({
2189                "one.rs": "const ONE: usize = 1;",
2190                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2191                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2192                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2193            }),
2194        )
2195        .await;
2196        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
2197        let window = cx.add_window(|cx| Workspace::test_new(project, cx));
2198        let workspace = window;
2199        let search_bar = window.build_view(cx, |_| ProjectSearchBar::new());
2200
2201        let active_item = cx.read(|cx| {
2202            workspace
2203                .read(cx)
2204                .unwrap()
2205                .active_pane()
2206                .read(cx)
2207                .active_item()
2208                .and_then(|item| item.downcast::<ProjectSearchView>())
2209        });
2210        assert!(
2211            active_item.is_none(),
2212            "Expected no search panel to be active"
2213        );
2214
2215        window
2216            .update(cx, move |workspace, cx| {
2217                assert_eq!(workspace.panes().len(), 1);
2218                workspace.panes()[0].update(cx, move |pane, cx| {
2219                    pane.toolbar()
2220                        .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
2221                });
2222
2223                ProjectSearchView::deploy_search(workspace, &workspace::DeploySearch::find(), cx)
2224            })
2225            .unwrap();
2226
2227        let Some(search_view) = cx.read(|cx| {
2228            workspace
2229                .read(cx)
2230                .unwrap()
2231                .active_pane()
2232                .read(cx)
2233                .active_item()
2234                .and_then(|item| item.downcast::<ProjectSearchView>())
2235        }) else {
2236            panic!("Search view expected to appear after new search event trigger")
2237        };
2238
2239        cx.spawn(|mut cx| async move {
2240            window
2241                .update(&mut cx, |_, cx| {
2242                    cx.dispatch_action(ToggleFocus.boxed_clone())
2243                })
2244                .unwrap();
2245        })
2246        .detach();
2247        cx.background_executor.run_until_parked();
2248        window
2249            .update(cx, |_, cx| {
2250                search_view.update(cx, |search_view, cx| {
2251                assert!(
2252                    search_view.query_editor.focus_handle(cx).is_focused(cx),
2253                    "Empty search view should be focused after the toggle focus event: no results panel to focus on",
2254                );
2255           });
2256        }).unwrap();
2257
2258        window
2259            .update(cx, |_, cx| {
2260                search_view.update(cx, |search_view, cx| {
2261                    let query_editor = &search_view.query_editor;
2262                    assert!(
2263                        query_editor.focus_handle(cx).is_focused(cx),
2264                        "Search view should be focused after the new search view is activated",
2265                    );
2266                    let query_text = query_editor.read(cx).text(cx);
2267                    assert!(
2268                        query_text.is_empty(),
2269                        "New search query should be empty but got '{query_text}'",
2270                    );
2271                    let results_text = search_view
2272                        .results_editor
2273                        .update(cx, |editor, cx| editor.display_text(cx));
2274                    assert!(
2275                        results_text.is_empty(),
2276                        "Empty search view should have no results but got '{results_text}'"
2277                    );
2278                });
2279            })
2280            .unwrap();
2281
2282        window
2283            .update(cx, |_, cx| {
2284                search_view.update(cx, |search_view, cx| {
2285                    search_view.query_editor.update(cx, |query_editor, cx| {
2286                        query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", cx)
2287                    });
2288                    search_view.search(cx);
2289                });
2290            })
2291            .unwrap();
2292        cx.background_executor.run_until_parked();
2293        window
2294            .update(cx, |_, cx| {
2295            search_view.update(cx, |search_view, cx| {
2296                let results_text = search_view
2297                    .results_editor
2298                    .update(cx, |editor, cx| editor.display_text(cx));
2299                assert!(
2300                    results_text.is_empty(),
2301                    "Search view for mismatching query should have no results but got '{results_text}'"
2302                );
2303                assert!(
2304                    search_view.query_editor.focus_handle(cx).is_focused(cx),
2305                    "Search view should be focused after mismatching query had been used in search",
2306                );
2307            });
2308        }).unwrap();
2309
2310        cx.spawn(|mut cx| async move {
2311            window.update(&mut cx, |_, cx| {
2312                cx.dispatch_action(ToggleFocus.boxed_clone())
2313            })
2314        })
2315        .detach();
2316        cx.background_executor.run_until_parked();
2317        window.update(cx, |_, cx| {
2318            search_view.update(cx, |search_view, cx| {
2319                assert!(
2320                    search_view.query_editor.focus_handle(cx).is_focused(cx),
2321                    "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on",
2322                );
2323            });
2324        }).unwrap();
2325
2326        window
2327            .update(cx, |_, cx| {
2328                search_view.update(cx, |search_view, cx| {
2329                    search_view
2330                        .query_editor
2331                        .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
2332                    search_view.search(cx);
2333                });
2334            })
2335            .unwrap();
2336        cx.background_executor.run_until_parked();
2337        window.update(cx, |_, cx| {
2338            search_view.update(cx, |search_view, cx| {
2339                assert_eq!(
2340                    search_view
2341                        .results_editor
2342                        .update(cx, |editor, cx| editor.display_text(cx)),
2343                    "\n\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\n\n\nconst TWO: usize = one::ONE + one::ONE;\n",
2344                    "Search view results should match the query"
2345                );
2346                assert!(
2347                    search_view.results_editor.focus_handle(cx).is_focused(cx),
2348                    "Search view with mismatching query should be focused after search results are available",
2349                );
2350            });
2351        }).unwrap();
2352        cx.spawn(|mut cx| async move {
2353            window
2354                .update(&mut cx, |_, cx| {
2355                    cx.dispatch_action(ToggleFocus.boxed_clone())
2356                })
2357                .unwrap();
2358        })
2359        .detach();
2360        cx.background_executor.run_until_parked();
2361        window.update(cx, |_, cx| {
2362            search_view.update(cx, |search_view, cx| {
2363                assert!(
2364                    search_view.results_editor.focus_handle(cx).is_focused(cx),
2365                    "Search view with matching query should still have its results editor focused after the toggle focus event",
2366                );
2367            });
2368        }).unwrap();
2369
2370        workspace
2371            .update(cx, |workspace, cx| {
2372                ProjectSearchView::deploy_search(workspace, &workspace::DeploySearch::find(), cx)
2373            })
2374            .unwrap();
2375        window.update(cx, |_, cx| {
2376            search_view.update(cx, |search_view, cx| {
2377                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");
2378                assert_eq!(
2379                    search_view
2380                        .results_editor
2381                        .update(cx, |editor, cx| editor.display_text(cx)),
2382                    "\n\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\n\n\nconst TWO: usize = one::ONE + one::ONE;\n",
2383                    "Results should be unchanged after search view 2nd open in a row"
2384                );
2385                assert!(
2386                    search_view.query_editor.focus_handle(cx).is_focused(cx),
2387                    "Focus should be moved into query editor again after search view 2nd open in a row"
2388                );
2389            });
2390        }).unwrap();
2391
2392        cx.spawn(|mut cx| async move {
2393            window
2394                .update(&mut cx, |_, cx| {
2395                    cx.dispatch_action(ToggleFocus.boxed_clone())
2396                })
2397                .unwrap();
2398        })
2399        .detach();
2400        cx.background_executor.run_until_parked();
2401        window.update(cx, |_, cx| {
2402            search_view.update(cx, |search_view, cx| {
2403                assert!(
2404                    search_view.results_editor.focus_handle(cx).is_focused(cx),
2405                    "Search view with matching query should switch focus to the results editor after the toggle focus event",
2406                );
2407            });
2408        }).unwrap();
2409    }
2410
2411    #[gpui::test]
2412    async fn test_new_project_search_focus(cx: &mut TestAppContext) {
2413        init_test(cx);
2414
2415        let fs = FakeFs::new(cx.background_executor.clone());
2416        fs.insert_tree(
2417            "/dir",
2418            json!({
2419                "one.rs": "const ONE: usize = 1;",
2420                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2421                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2422                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2423            }),
2424        )
2425        .await;
2426        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
2427        let window = cx.add_window(|cx| Workspace::test_new(project, cx));
2428        let workspace = window;
2429        let search_bar = window.build_view(cx, |_| ProjectSearchBar::new());
2430
2431        let active_item = cx.read(|cx| {
2432            workspace
2433                .read(cx)
2434                .unwrap()
2435                .active_pane()
2436                .read(cx)
2437                .active_item()
2438                .and_then(|item| item.downcast::<ProjectSearchView>())
2439        });
2440        assert!(
2441            active_item.is_none(),
2442            "Expected no search panel to be active"
2443        );
2444
2445        window
2446            .update(cx, move |workspace, cx| {
2447                assert_eq!(workspace.panes().len(), 1);
2448                workspace.panes()[0].update(cx, move |pane, cx| {
2449                    pane.toolbar()
2450                        .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
2451                });
2452
2453                ProjectSearchView::new_search(workspace, &workspace::NewSearch, cx)
2454            })
2455            .unwrap();
2456
2457        let Some(search_view) = cx.read(|cx| {
2458            workspace
2459                .read(cx)
2460                .unwrap()
2461                .active_pane()
2462                .read(cx)
2463                .active_item()
2464                .and_then(|item| item.downcast::<ProjectSearchView>())
2465        }) else {
2466            panic!("Search view expected to appear after new search event trigger")
2467        };
2468
2469        cx.spawn(|mut cx| async move {
2470            window
2471                .update(&mut cx, |_, cx| {
2472                    cx.dispatch_action(ToggleFocus.boxed_clone())
2473                })
2474                .unwrap();
2475        })
2476        .detach();
2477        cx.background_executor.run_until_parked();
2478
2479        window.update(cx, |_, cx| {
2480            search_view.update(cx, |search_view, cx| {
2481                    assert!(
2482                        search_view.query_editor.focus_handle(cx).is_focused(cx),
2483                        "Empty search view should be focused after the toggle focus event: no results panel to focus on",
2484                    );
2485                });
2486        }).unwrap();
2487
2488        window
2489            .update(cx, |_, cx| {
2490                search_view.update(cx, |search_view, cx| {
2491                    let query_editor = &search_view.query_editor;
2492                    assert!(
2493                        query_editor.focus_handle(cx).is_focused(cx),
2494                        "Search view should be focused after the new search view is activated",
2495                    );
2496                    let query_text = query_editor.read(cx).text(cx);
2497                    assert!(
2498                        query_text.is_empty(),
2499                        "New search query should be empty but got '{query_text}'",
2500                    );
2501                    let results_text = search_view
2502                        .results_editor
2503                        .update(cx, |editor, cx| editor.display_text(cx));
2504                    assert!(
2505                        results_text.is_empty(),
2506                        "Empty search view should have no results but got '{results_text}'"
2507                    );
2508                });
2509            })
2510            .unwrap();
2511
2512        window
2513            .update(cx, |_, cx| {
2514                search_view.update(cx, |search_view, cx| {
2515                    search_view.query_editor.update(cx, |query_editor, cx| {
2516                        query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", cx)
2517                    });
2518                    search_view.search(cx);
2519                });
2520            })
2521            .unwrap();
2522
2523        cx.background_executor.run_until_parked();
2524        window
2525            .update(cx, |_, cx| {
2526                search_view.update(cx, |search_view, cx| {
2527                    let results_text = search_view
2528                        .results_editor
2529                        .update(cx, |editor, cx| editor.display_text(cx));
2530                    assert!(
2531                results_text.is_empty(),
2532                "Search view for mismatching query should have no results but got '{results_text}'"
2533            );
2534                    assert!(
2535                search_view.query_editor.focus_handle(cx).is_focused(cx),
2536                "Search view should be focused after mismatching query had been used in search",
2537            );
2538                });
2539            })
2540            .unwrap();
2541        cx.spawn(|mut cx| async move {
2542            window.update(&mut cx, |_, cx| {
2543                cx.dispatch_action(ToggleFocus.boxed_clone())
2544            })
2545        })
2546        .detach();
2547        cx.background_executor.run_until_parked();
2548        window.update(cx, |_, cx| {
2549            search_view.update(cx, |search_view, cx| {
2550                    assert!(
2551                        search_view.query_editor.focus_handle(cx).is_focused(cx),
2552                        "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on",
2553                    );
2554                });
2555        }).unwrap();
2556
2557        window
2558            .update(cx, |_, cx| {
2559                search_view.update(cx, |search_view, cx| {
2560                    search_view
2561                        .query_editor
2562                        .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
2563                    search_view.search(cx);
2564                })
2565            })
2566            .unwrap();
2567        cx.background_executor.run_until_parked();
2568        window.update(cx, |_, cx|
2569        search_view.update(cx, |search_view, cx| {
2570                assert_eq!(
2571                    search_view
2572                        .results_editor
2573                        .update(cx, |editor, cx| editor.display_text(cx)),
2574                    "\n\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\n\n\nconst TWO: usize = one::ONE + one::ONE;\n",
2575                    "Search view results should match the query"
2576                );
2577                assert!(
2578                    search_view.results_editor.focus_handle(cx).is_focused(cx),
2579                    "Search view with mismatching query should be focused after search results are available",
2580                );
2581            })).unwrap();
2582        cx.spawn(|mut cx| async move {
2583            window
2584                .update(&mut cx, |_, cx| {
2585                    cx.dispatch_action(ToggleFocus.boxed_clone())
2586                })
2587                .unwrap();
2588        })
2589        .detach();
2590        cx.background_executor.run_until_parked();
2591        window.update(cx, |_, cx| {
2592            search_view.update(cx, |search_view, cx| {
2593                    assert!(
2594                        search_view.results_editor.focus_handle(cx).is_focused(cx),
2595                        "Search view with matching query should still have its results editor focused after the toggle focus event",
2596                    );
2597                });
2598        }).unwrap();
2599
2600        workspace
2601            .update(cx, |workspace, cx| {
2602                ProjectSearchView::new_search(workspace, &workspace::NewSearch, cx)
2603            })
2604            .unwrap();
2605        cx.background_executor.run_until_parked();
2606        let Some(search_view_2) = cx.read(|cx| {
2607            workspace
2608                .read(cx)
2609                .unwrap()
2610                .active_pane()
2611                .read(cx)
2612                .active_item()
2613                .and_then(|item| item.downcast::<ProjectSearchView>())
2614        }) else {
2615            panic!("Search view expected to appear after new search event trigger")
2616        };
2617        assert!(
2618            search_view_2 != search_view,
2619            "New search view should be open after `workspace::NewSearch` event"
2620        );
2621
2622        window.update(cx, |_, cx| {
2623            search_view.update(cx, |search_view, cx| {
2624                    assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO", "First search view should not have an updated query");
2625                    assert_eq!(
2626                        search_view
2627                            .results_editor
2628                            .update(cx, |editor, cx| editor.display_text(cx)),
2629                        "\n\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\n\n\nconst TWO: usize = one::ONE + one::ONE;\n",
2630                        "Results of the first search view should not update too"
2631                    );
2632                    assert!(
2633                        !search_view.query_editor.focus_handle(cx).is_focused(cx),
2634                        "Focus should be moved away from the first search view"
2635                    );
2636                });
2637        }).unwrap();
2638
2639        window.update(cx, |_, cx| {
2640            search_view_2.update(cx, |search_view_2, cx| {
2641                    assert_eq!(
2642                        search_view_2.query_editor.read(cx).text(cx),
2643                        "two",
2644                        "New search view should get the query from the text cursor was at during the event spawn (first search view's first result)"
2645                    );
2646                    assert_eq!(
2647                        search_view_2
2648                            .results_editor
2649                            .update(cx, |editor, cx| editor.display_text(cx)),
2650                        "",
2651                        "No search results should be in the 2nd view yet, as we did not spawn a search for it"
2652                    );
2653                    assert!(
2654                        search_view_2.query_editor.focus_handle(cx).is_focused(cx),
2655                        "Focus should be moved into query editor of the new window"
2656                    );
2657                });
2658        }).unwrap();
2659
2660        window
2661            .update(cx, |_, cx| {
2662                search_view_2.update(cx, |search_view_2, cx| {
2663                    search_view_2
2664                        .query_editor
2665                        .update(cx, |query_editor, cx| query_editor.set_text("FOUR", cx));
2666                    search_view_2.search(cx);
2667                });
2668            })
2669            .unwrap();
2670
2671        cx.background_executor.run_until_parked();
2672        window.update(cx, |_, cx| {
2673            search_view_2.update(cx, |search_view_2, cx| {
2674                    assert_eq!(
2675                        search_view_2
2676                            .results_editor
2677                            .update(cx, |editor, cx| editor.display_text(cx)),
2678                        "\n\n\nconst FOUR: usize = one::ONE + three::THREE;\n",
2679                        "New search view with the updated query should have new search results"
2680                    );
2681                    assert!(
2682                        search_view_2.results_editor.focus_handle(cx).is_focused(cx),
2683                        "Search view with mismatching query should be focused after search results are available",
2684                    );
2685                });
2686        }).unwrap();
2687
2688        cx.spawn(|mut cx| async move {
2689            window
2690                .update(&mut cx, |_, cx| {
2691                    cx.dispatch_action(ToggleFocus.boxed_clone())
2692                })
2693                .unwrap();
2694        })
2695        .detach();
2696        cx.background_executor.run_until_parked();
2697        window.update(cx, |_, cx| {
2698            search_view_2.update(cx, |search_view_2, cx| {
2699                    assert!(
2700                        search_view_2.results_editor.focus_handle(cx).is_focused(cx),
2701                        "Search view with matching query should switch focus to the results editor after the toggle focus event",
2702                    );
2703                });}).unwrap();
2704    }
2705
2706    #[gpui::test]
2707    async fn test_new_project_search_in_directory(cx: &mut TestAppContext) {
2708        init_test(cx);
2709
2710        let fs = FakeFs::new(cx.background_executor.clone());
2711        fs.insert_tree(
2712            "/dir",
2713            json!({
2714                "a": {
2715                    "one.rs": "const ONE: usize = 1;",
2716                    "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2717                },
2718                "b": {
2719                    "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2720                    "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2721                },
2722            }),
2723        )
2724        .await;
2725        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
2726        let worktree_id = project.read_with(cx, |project, cx| {
2727            project.worktrees(cx).next().unwrap().read(cx).id()
2728        });
2729        let window = cx.add_window(|cx| Workspace::test_new(project, cx));
2730        let workspace = window.root(cx).unwrap();
2731        let search_bar = window.build_view(cx, |_| ProjectSearchBar::new());
2732
2733        let active_item = cx.read(|cx| {
2734            workspace
2735                .read(cx)
2736                .active_pane()
2737                .read(cx)
2738                .active_item()
2739                .and_then(|item| item.downcast::<ProjectSearchView>())
2740        });
2741        assert!(
2742            active_item.is_none(),
2743            "Expected no search panel to be active"
2744        );
2745
2746        window
2747            .update(cx, move |workspace, cx| {
2748                assert_eq!(workspace.panes().len(), 1);
2749                workspace.panes()[0].update(cx, move |pane, cx| {
2750                    pane.toolbar()
2751                        .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
2752                });
2753            })
2754            .unwrap();
2755
2756        let a_dir_entry = cx.update(|cx| {
2757            workspace
2758                .read(cx)
2759                .project()
2760                .read(cx)
2761                .entry_for_path(&(worktree_id, "a").into(), cx)
2762                .expect("no entry for /a/ directory")
2763        });
2764        assert!(a_dir_entry.is_dir());
2765        window
2766            .update(cx, |workspace, cx| {
2767                ProjectSearchView::new_search_in_directory(workspace, &a_dir_entry.path, cx)
2768            })
2769            .unwrap();
2770
2771        let Some(search_view) = cx.read(|cx| {
2772            workspace
2773                .read(cx)
2774                .active_pane()
2775                .read(cx)
2776                .active_item()
2777                .and_then(|item| item.downcast::<ProjectSearchView>())
2778        }) else {
2779            panic!("Search view expected to appear after new search in directory event trigger")
2780        };
2781        cx.background_executor.run_until_parked();
2782        window
2783            .update(cx, |_, cx| {
2784                search_view.update(cx, |search_view, cx| {
2785                    assert!(
2786                        search_view.query_editor.focus_handle(cx).is_focused(cx),
2787                        "On new search in directory, focus should be moved into query editor"
2788                    );
2789                    search_view.excluded_files_editor.update(cx, |editor, cx| {
2790                        assert!(
2791                            editor.display_text(cx).is_empty(),
2792                            "New search in directory should not have any excluded files"
2793                        );
2794                    });
2795                    search_view.included_files_editor.update(cx, |editor, cx| {
2796                        assert_eq!(
2797                            editor.display_text(cx),
2798                            a_dir_entry.path.to_str().unwrap(),
2799                            "New search in directory should have included dir entry path"
2800                        );
2801                    });
2802                });
2803            })
2804            .unwrap();
2805        window
2806            .update(cx, |_, cx| {
2807                search_view.update(cx, |search_view, cx| {
2808                    search_view
2809                        .query_editor
2810                        .update(cx, |query_editor, cx| query_editor.set_text("const", cx));
2811                    search_view.search(cx);
2812                });
2813            })
2814            .unwrap();
2815        cx.background_executor.run_until_parked();
2816        window
2817            .update(cx, |_, cx| {
2818                search_view.update(cx, |search_view, cx| {
2819                    assert_eq!(
2820                search_view
2821                    .results_editor
2822                    .update(cx, |editor, cx| editor.display_text(cx)),
2823                "\n\n\nconst ONE: usize = 1;\n\n\n\n\nconst TWO: usize = one::ONE + one::ONE;\n",
2824                "New search in directory should have a filter that matches a certain directory"
2825            );
2826                })
2827            })
2828            .unwrap();
2829    }
2830
2831    #[gpui::test]
2832    async fn test_search_query_history(cx: &mut TestAppContext) {
2833        init_test(cx);
2834
2835        let fs = FakeFs::new(cx.background_executor.clone());
2836        fs.insert_tree(
2837            "/dir",
2838            json!({
2839                "one.rs": "const ONE: usize = 1;",
2840                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2841                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2842                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2843            }),
2844        )
2845        .await;
2846        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
2847        let window = cx.add_window(|cx| Workspace::test_new(project, cx));
2848        let workspace = window.root(cx).unwrap();
2849        let search_bar = window.build_view(cx, |_| ProjectSearchBar::new());
2850
2851        window
2852            .update(cx, {
2853                let search_bar = search_bar.clone();
2854                move |workspace, cx| {
2855                    assert_eq!(workspace.panes().len(), 1);
2856                    workspace.panes()[0].update(cx, move |pane, cx| {
2857                        pane.toolbar()
2858                            .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
2859                    });
2860
2861                    ProjectSearchView::new_search(workspace, &workspace::NewSearch, cx)
2862                }
2863            })
2864            .unwrap();
2865
2866        let search_view = cx.read(|cx| {
2867            workspace
2868                .read(cx)
2869                .active_pane()
2870                .read(cx)
2871                .active_item()
2872                .and_then(|item| item.downcast::<ProjectSearchView>())
2873                .expect("Search view expected to appear after new search event trigger")
2874        });
2875
2876        // Add 3 search items into the history + another unsubmitted one.
2877        window
2878            .update(cx, |_, cx| {
2879                search_view.update(cx, |search_view, cx| {
2880                    search_view.search_options = SearchOptions::CASE_SENSITIVE;
2881                    search_view
2882                        .query_editor
2883                        .update(cx, |query_editor, cx| query_editor.set_text("ONE", cx));
2884                    search_view.search(cx);
2885                });
2886            })
2887            .unwrap();
2888
2889        cx.background_executor.run_until_parked();
2890        window
2891            .update(cx, |_, cx| {
2892                search_view.update(cx, |search_view, cx| {
2893                    search_view
2894                        .query_editor
2895                        .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
2896                    search_view.search(cx);
2897                });
2898            })
2899            .unwrap();
2900        cx.background_executor.run_until_parked();
2901        window
2902            .update(cx, |_, cx| {
2903                search_view.update(cx, |search_view, cx| {
2904                    search_view
2905                        .query_editor
2906                        .update(cx, |query_editor, cx| query_editor.set_text("THREE", cx));
2907                    search_view.search(cx);
2908                })
2909            })
2910            .unwrap();
2911        cx.background_executor.run_until_parked();
2912        window
2913            .update(cx, |_, cx| {
2914                search_view.update(cx, |search_view, cx| {
2915                    search_view.query_editor.update(cx, |query_editor, cx| {
2916                        query_editor.set_text("JUST_TEXT_INPUT", cx)
2917                    });
2918                })
2919            })
2920            .unwrap();
2921        cx.background_executor.run_until_parked();
2922
2923        // Ensure that the latest input with search settings is active.
2924        window
2925            .update(cx, |_, cx| {
2926                search_view.update(cx, |search_view, cx| {
2927                    assert_eq!(
2928                        search_view.query_editor.read(cx).text(cx),
2929                        "JUST_TEXT_INPUT"
2930                    );
2931                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
2932                });
2933            })
2934            .unwrap();
2935
2936        // Next history query after the latest should set the query to the empty string.
2937        window
2938            .update(cx, |_, cx| {
2939                search_bar.update(cx, |search_bar, cx| {
2940                    search_bar.focus_search(cx);
2941                    search_bar.next_history_query(&NextHistoryQuery, cx);
2942                })
2943            })
2944            .unwrap();
2945        window
2946            .update(cx, |_, cx| {
2947                search_view.update(cx, |search_view, cx| {
2948                    assert_eq!(search_view.query_editor.read(cx).text(cx), "");
2949                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
2950                });
2951            })
2952            .unwrap();
2953        window
2954            .update(cx, |_, cx| {
2955                search_bar.update(cx, |search_bar, cx| {
2956                    search_bar.focus_search(cx);
2957                    search_bar.next_history_query(&NextHistoryQuery, cx);
2958                })
2959            })
2960            .unwrap();
2961        window
2962            .update(cx, |_, cx| {
2963                search_view.update(cx, |search_view, cx| {
2964                    assert_eq!(search_view.query_editor.read(cx).text(cx), "");
2965                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
2966                });
2967            })
2968            .unwrap();
2969
2970        // First previous query for empty current query should set the query to the latest submitted one.
2971        window
2972            .update(cx, |_, cx| {
2973                search_bar.update(cx, |search_bar, cx| {
2974                    search_bar.focus_search(cx);
2975                    search_bar.previous_history_query(&PreviousHistoryQuery, cx);
2976                });
2977            })
2978            .unwrap();
2979        window
2980            .update(cx, |_, cx| {
2981                search_view.update(cx, |search_view, cx| {
2982                    assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
2983                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
2984                });
2985            })
2986            .unwrap();
2987
2988        // Further previous items should go over the history in reverse order.
2989        window
2990            .update(cx, |_, cx| {
2991                search_bar.update(cx, |search_bar, cx| {
2992                    search_bar.focus_search(cx);
2993                    search_bar.previous_history_query(&PreviousHistoryQuery, cx);
2994                });
2995            })
2996            .unwrap();
2997        window
2998            .update(cx, |_, cx| {
2999                search_view.update(cx, |search_view, cx| {
3000                    assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
3001                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3002                });
3003            })
3004            .unwrap();
3005
3006        // Previous items should never go behind the first history item.
3007        window
3008            .update(cx, |_, cx| {
3009                search_bar.update(cx, |search_bar, cx| {
3010                    search_bar.focus_search(cx);
3011                    search_bar.previous_history_query(&PreviousHistoryQuery, cx);
3012                });
3013            })
3014            .unwrap();
3015        window
3016            .update(cx, |_, cx| {
3017                search_view.update(cx, |search_view, cx| {
3018                    assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE");
3019                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3020                });
3021            })
3022            .unwrap();
3023        window
3024            .update(cx, |_, cx| {
3025                search_bar.update(cx, |search_bar, cx| {
3026                    search_bar.focus_search(cx);
3027                    search_bar.previous_history_query(&PreviousHistoryQuery, cx);
3028                });
3029            })
3030            .unwrap();
3031        window
3032            .update(cx, |_, cx| {
3033                search_view.update(cx, |search_view, cx| {
3034                    assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE");
3035                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3036                });
3037            })
3038            .unwrap();
3039
3040        // Next items should go over the history in the original order.
3041        window
3042            .update(cx, |_, cx| {
3043                search_bar.update(cx, |search_bar, cx| {
3044                    search_bar.focus_search(cx);
3045                    search_bar.next_history_query(&NextHistoryQuery, cx);
3046                });
3047            })
3048            .unwrap();
3049        window
3050            .update(cx, |_, cx| {
3051                search_view.update(cx, |search_view, cx| {
3052                    assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
3053                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3054                });
3055            })
3056            .unwrap();
3057
3058        window
3059            .update(cx, |_, cx| {
3060                search_view.update(cx, |search_view, cx| {
3061                    search_view
3062                        .query_editor
3063                        .update(cx, |query_editor, cx| query_editor.set_text("TWO_NEW", cx));
3064                    search_view.search(cx);
3065                });
3066            })
3067            .unwrap();
3068        cx.background_executor.run_until_parked();
3069        window
3070            .update(cx, |_, cx| {
3071                search_view.update(cx, |search_view, cx| {
3072                    assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW");
3073                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3074                });
3075            })
3076            .unwrap();
3077
3078        // New search input should add another entry to history and move the selection to the end of the history.
3079        window
3080            .update(cx, |_, cx| {
3081                search_bar.update(cx, |search_bar, cx| {
3082                    search_bar.focus_search(cx);
3083                    search_bar.previous_history_query(&PreviousHistoryQuery, cx);
3084                });
3085            })
3086            .unwrap();
3087        window
3088            .update(cx, |_, cx| {
3089                search_view.update(cx, |search_view, cx| {
3090                    assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
3091                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3092                });
3093            })
3094            .unwrap();
3095        window
3096            .update(cx, |_, cx| {
3097                search_bar.update(cx, |search_bar, cx| {
3098                    search_bar.focus_search(cx);
3099                    search_bar.previous_history_query(&PreviousHistoryQuery, cx);
3100                });
3101            })
3102            .unwrap();
3103        window
3104            .update(cx, |_, cx| {
3105                search_view.update(cx, |search_view, cx| {
3106                    assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
3107                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3108                });
3109            })
3110            .unwrap();
3111        window
3112            .update(cx, |_, cx| {
3113                search_bar.update(cx, |search_bar, cx| {
3114                    search_bar.focus_search(cx);
3115                    search_bar.next_history_query(&NextHistoryQuery, cx);
3116                });
3117            })
3118            .unwrap();
3119        window
3120            .update(cx, |_, cx| {
3121                search_view.update(cx, |search_view, cx| {
3122                    assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
3123                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3124                });
3125            })
3126            .unwrap();
3127        window
3128            .update(cx, |_, cx| {
3129                search_bar.update(cx, |search_bar, cx| {
3130                    search_bar.focus_search(cx);
3131                    search_bar.next_history_query(&NextHistoryQuery, cx);
3132                });
3133            })
3134            .unwrap();
3135        window
3136            .update(cx, |_, cx| {
3137                search_view.update(cx, |search_view, cx| {
3138                    assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW");
3139                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3140                });
3141            })
3142            .unwrap();
3143        window
3144            .update(cx, |_, cx| {
3145                search_bar.update(cx, |search_bar, cx| {
3146                    search_bar.focus_search(cx);
3147                    search_bar.next_history_query(&NextHistoryQuery, cx);
3148                });
3149            })
3150            .unwrap();
3151        window
3152            .update(cx, |_, cx| {
3153                search_view.update(cx, |search_view, cx| {
3154                    assert_eq!(search_view.query_editor.read(cx).text(cx), "");
3155                    assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3156                });
3157            })
3158            .unwrap();
3159    }
3160
3161    #[gpui::test]
3162    async fn test_search_query_history_with_multiple_views(cx: &mut TestAppContext) {
3163        init_test(cx);
3164
3165        let fs = FakeFs::new(cx.background_executor.clone());
3166        fs.insert_tree(
3167            "/dir",
3168            json!({
3169                "one.rs": "const ONE: usize = 1;",
3170            }),
3171        )
3172        .await;
3173        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
3174        let worktree_id = project.update(cx, |this, cx| {
3175            this.worktrees(cx).next().unwrap().read(cx).id()
3176        });
3177
3178        let window = cx.add_window(|cx| Workspace::test_new(project, cx));
3179        let workspace = window.root(cx).unwrap();
3180
3181        let panes: Vec<_> = window
3182            .update(cx, |this, _| this.panes().to_owned())
3183            .unwrap();
3184
3185        let search_bar_1 = window.build_view(cx, |_| ProjectSearchBar::new());
3186        let search_bar_2 = window.build_view(cx, |_| ProjectSearchBar::new());
3187
3188        assert_eq!(panes.len(), 1);
3189        let first_pane = panes.first().cloned().unwrap();
3190        assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 0);
3191        window
3192            .update(cx, |workspace, cx| {
3193                workspace.open_path(
3194                    (worktree_id, "one.rs"),
3195                    Some(first_pane.downgrade()),
3196                    true,
3197                    cx,
3198                )
3199            })
3200            .unwrap()
3201            .await
3202            .unwrap();
3203        assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 1);
3204
3205        // Add a project search item to the first pane
3206        window
3207            .update(cx, {
3208                let search_bar = search_bar_1.clone();
3209                let pane = first_pane.clone();
3210                move |workspace, cx| {
3211                    pane.update(cx, move |pane, cx| {
3212                        pane.toolbar()
3213                            .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
3214                    });
3215
3216                    ProjectSearchView::new_search(workspace, &workspace::NewSearch, cx)
3217                }
3218            })
3219            .unwrap();
3220        let search_view_1 = cx.read(|cx| {
3221            workspace
3222                .read(cx)
3223                .active_item(cx)
3224                .and_then(|item| item.downcast::<ProjectSearchView>())
3225                .expect("Search view expected to appear after new search event trigger")
3226        });
3227
3228        let second_pane = window
3229            .update(cx, |workspace, cx| {
3230                workspace.split_and_clone(first_pane.clone(), workspace::SplitDirection::Right, cx)
3231            })
3232            .unwrap()
3233            .unwrap();
3234        assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 1);
3235
3236        assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 1);
3237        assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 2);
3238
3239        // Add a project search item to the second pane
3240        window
3241            .update(cx, {
3242                let search_bar = search_bar_2.clone();
3243                let pane = second_pane.clone();
3244                move |workspace, cx| {
3245                    assert_eq!(workspace.panes().len(), 2);
3246                    pane.update(cx, move |pane, cx| {
3247                        pane.toolbar()
3248                            .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
3249                    });
3250
3251                    ProjectSearchView::new_search(workspace, &workspace::NewSearch, cx)
3252                }
3253            })
3254            .unwrap();
3255
3256        let search_view_2 = cx.read(|cx| {
3257            workspace
3258                .read(cx)
3259                .active_item(cx)
3260                .and_then(|item| item.downcast::<ProjectSearchView>())
3261                .expect("Search view expected to appear after new search event trigger")
3262        });
3263
3264        cx.run_until_parked();
3265        assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 2);
3266        assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 2);
3267
3268        let update_search_view =
3269            |search_view: &View<ProjectSearchView>, query: &str, cx: &mut TestAppContext| {
3270                window
3271                    .update(cx, |_, cx| {
3272                        search_view.update(cx, |search_view, cx| {
3273                            search_view
3274                                .query_editor
3275                                .update(cx, |query_editor, cx| query_editor.set_text(query, cx));
3276                            search_view.search(cx);
3277                        });
3278                    })
3279                    .unwrap();
3280            };
3281
3282        let active_query =
3283            |search_view: &View<ProjectSearchView>, cx: &mut TestAppContext| -> String {
3284                window
3285                    .update(cx, |_, cx| {
3286                        search_view.update(cx, |search_view, cx| {
3287                            search_view.query_editor.read(cx).text(cx).to_string()
3288                        })
3289                    })
3290                    .unwrap()
3291            };
3292
3293        let select_prev_history_item =
3294            |search_bar: &View<ProjectSearchBar>, cx: &mut TestAppContext| {
3295                window
3296                    .update(cx, |_, cx| {
3297                        search_bar.update(cx, |search_bar, cx| {
3298                            search_bar.focus_search(cx);
3299                            search_bar.previous_history_query(&PreviousHistoryQuery, cx);
3300                        })
3301                    })
3302                    .unwrap();
3303            };
3304
3305        let select_next_history_item =
3306            |search_bar: &View<ProjectSearchBar>, cx: &mut TestAppContext| {
3307                window
3308                    .update(cx, |_, cx| {
3309                        search_bar.update(cx, |search_bar, cx| {
3310                            search_bar.focus_search(cx);
3311                            search_bar.next_history_query(&NextHistoryQuery, cx);
3312                        })
3313                    })
3314                    .unwrap();
3315            };
3316
3317        update_search_view(&search_view_1, "ONE", cx);
3318        cx.background_executor.run_until_parked();
3319
3320        update_search_view(&search_view_2, "TWO", cx);
3321        cx.background_executor.run_until_parked();
3322
3323        assert_eq!(active_query(&search_view_1, cx), "ONE");
3324        assert_eq!(active_query(&search_view_2, cx), "TWO");
3325
3326        // Selecting previous history item should select the query from search view 1.
3327        select_prev_history_item(&search_bar_2, cx);
3328        assert_eq!(active_query(&search_view_2, cx), "ONE");
3329
3330        // Selecting the previous history item should not change the query as it is already the first item.
3331        select_prev_history_item(&search_bar_2, cx);
3332        assert_eq!(active_query(&search_view_2, cx), "ONE");
3333
3334        // Changing the query in search view 2 should not affect the history of search view 1.
3335        assert_eq!(active_query(&search_view_1, cx), "ONE");
3336
3337        // Deploying a new search in search view 2
3338        update_search_view(&search_view_2, "THREE", cx);
3339        cx.background_executor.run_until_parked();
3340
3341        select_next_history_item(&search_bar_2, cx);
3342        assert_eq!(active_query(&search_view_2, cx), "");
3343
3344        select_prev_history_item(&search_bar_2, cx);
3345        assert_eq!(active_query(&search_view_2, cx), "THREE");
3346
3347        select_prev_history_item(&search_bar_2, cx);
3348        assert_eq!(active_query(&search_view_2, cx), "TWO");
3349
3350        select_prev_history_item(&search_bar_2, cx);
3351        assert_eq!(active_query(&search_view_2, cx), "ONE");
3352
3353        select_prev_history_item(&search_bar_2, cx);
3354        assert_eq!(active_query(&search_view_2, cx), "ONE");
3355
3356        // Search view 1 should now see the query from search view 2.
3357        assert_eq!(active_query(&search_view_1, cx), "ONE");
3358
3359        select_next_history_item(&search_bar_2, cx);
3360        assert_eq!(active_query(&search_view_2, cx), "TWO");
3361
3362        // Here is the new query from search view 2
3363        select_next_history_item(&search_bar_2, cx);
3364        assert_eq!(active_query(&search_view_2, cx), "THREE");
3365
3366        select_next_history_item(&search_bar_2, cx);
3367        assert_eq!(active_query(&search_view_2, cx), "");
3368
3369        select_next_history_item(&search_bar_1, cx);
3370        assert_eq!(active_query(&search_view_1, cx), "TWO");
3371
3372        select_next_history_item(&search_bar_1, cx);
3373        assert_eq!(active_query(&search_view_1, cx), "THREE");
3374
3375        select_next_history_item(&search_bar_1, cx);
3376        assert_eq!(active_query(&search_view_1, cx), "");
3377    }
3378
3379    #[gpui::test]
3380    async fn test_deploy_search_with_multiple_panes(cx: &mut TestAppContext) {
3381        init_test(cx);
3382
3383        // Setup 2 panes, both with a file open and one with a project search.
3384        let fs = FakeFs::new(cx.background_executor.clone());
3385        fs.insert_tree(
3386            "/dir",
3387            json!({
3388                "one.rs": "const ONE: usize = 1;",
3389            }),
3390        )
3391        .await;
3392        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
3393        let worktree_id = project.update(cx, |this, cx| {
3394            this.worktrees(cx).next().unwrap().read(cx).id()
3395        });
3396        let window = cx.add_window(|cx| Workspace::test_new(project, cx));
3397        let panes: Vec<_> = window
3398            .update(cx, |this, _| this.panes().to_owned())
3399            .unwrap();
3400        assert_eq!(panes.len(), 1);
3401        let first_pane = panes.first().cloned().unwrap();
3402        assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 0);
3403        window
3404            .update(cx, |workspace, cx| {
3405                workspace.open_path(
3406                    (worktree_id, "one.rs"),
3407                    Some(first_pane.downgrade()),
3408                    true,
3409                    cx,
3410                )
3411            })
3412            .unwrap()
3413            .await
3414            .unwrap();
3415        assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 1);
3416        let second_pane = window
3417            .update(cx, |workspace, cx| {
3418                workspace.split_and_clone(first_pane.clone(), workspace::SplitDirection::Right, cx)
3419            })
3420            .unwrap()
3421            .unwrap();
3422        assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 1);
3423        assert!(window
3424            .update(cx, |_, cx| second_pane
3425                .focus_handle(cx)
3426                .contains_focused(cx))
3427            .unwrap());
3428        let search_bar = window.build_view(cx, |_| ProjectSearchBar::new());
3429        window
3430            .update(cx, {
3431                let search_bar = search_bar.clone();
3432                let pane = first_pane.clone();
3433                move |workspace, cx| {
3434                    assert_eq!(workspace.panes().len(), 2);
3435                    pane.update(cx, move |pane, cx| {
3436                        pane.toolbar()
3437                            .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
3438                    });
3439                }
3440            })
3441            .unwrap();
3442
3443        // Add a project search item to the second pane
3444        window
3445            .update(cx, {
3446                let search_bar = search_bar.clone();
3447                let pane = second_pane.clone();
3448                move |workspace, cx| {
3449                    assert_eq!(workspace.panes().len(), 2);
3450                    pane.update(cx, move |pane, cx| {
3451                        pane.toolbar()
3452                            .update(cx, |toolbar, cx| toolbar.add_item(search_bar, cx))
3453                    });
3454
3455                    ProjectSearchView::new_search(workspace, &workspace::NewSearch, cx)
3456                }
3457            })
3458            .unwrap();
3459
3460        cx.run_until_parked();
3461        assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 2);
3462        assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 1);
3463
3464        // Focus the first pane
3465        window
3466            .update(cx, |workspace, cx| {
3467                assert_eq!(workspace.active_pane(), &second_pane);
3468                second_pane.update(cx, |this, cx| {
3469                    assert_eq!(this.active_item_index(), 1);
3470                    this.activate_prev_item(false, cx);
3471                    assert_eq!(this.active_item_index(), 0);
3472                });
3473                workspace.activate_pane_in_direction(workspace::SplitDirection::Left, cx);
3474            })
3475            .unwrap();
3476        window
3477            .update(cx, |workspace, cx| {
3478                assert_eq!(workspace.active_pane(), &first_pane);
3479                assert_eq!(first_pane.read(cx).items_len(), 1);
3480                assert_eq!(second_pane.read(cx).items_len(), 2);
3481            })
3482            .unwrap();
3483
3484        // Deploy a new search
3485        cx.dispatch_action(window.into(), DeploySearch::find());
3486
3487        // Both panes should now have a project search in them
3488        window
3489            .update(cx, |workspace, cx| {
3490                assert_eq!(workspace.active_pane(), &first_pane);
3491                first_pane.update(cx, |this, _| {
3492                    assert_eq!(this.active_item_index(), 1);
3493                    assert_eq!(this.items_len(), 2);
3494                });
3495                second_pane.update(cx, |this, cx| {
3496                    assert!(!cx.focus_handle().contains_focused(cx));
3497                    assert_eq!(this.items_len(), 2);
3498                });
3499            })
3500            .unwrap();
3501
3502        // Focus the second pane's non-search item
3503        window
3504            .update(cx, |_workspace, cx| {
3505                second_pane.update(cx, |pane, cx| pane.activate_next_item(true, cx));
3506            })
3507            .unwrap();
3508
3509        // Deploy a new search
3510        cx.dispatch_action(window.into(), DeploySearch::find());
3511
3512        // The project search view should now be focused in the second pane
3513        // And the number of items should be unchanged.
3514        window
3515            .update(cx, |_workspace, cx| {
3516                second_pane.update(cx, |pane, _cx| {
3517                    assert!(pane
3518                        .active_item()
3519                        .unwrap()
3520                        .downcast::<ProjectSearchView>()
3521                        .is_some());
3522
3523                    assert_eq!(pane.items_len(), 2);
3524                });
3525            })
3526            .unwrap();
3527    }
3528
3529    #[gpui::test]
3530    async fn test_scroll_search_results_to_top(cx: &mut TestAppContext) {
3531        init_test(cx);
3532
3533        // We need many lines in the search results to be able to scroll the window
3534        let fs = FakeFs::new(cx.background_executor.clone());
3535        fs.insert_tree(
3536            "/dir",
3537            json!({
3538                "1.txt": "\n\n\n\n\n A \n\n\n\n\n",
3539                "2.txt": "\n\n\n\n\n A \n\n\n\n\n",
3540                "3.rs": "\n\n\n\n\n A \n\n\n\n\n",
3541                "4.rs": "\n\n\n\n\n A \n\n\n\n\n",
3542                "5.rs": "\n\n\n\n\n A \n\n\n\n\n",
3543                "6.rs": "\n\n\n\n\n A \n\n\n\n\n",
3544                "7.rs": "\n\n\n\n\n A \n\n\n\n\n",
3545                "8.rs": "\n\n\n\n\n A \n\n\n\n\n",
3546                "9.rs": "\n\n\n\n\n A \n\n\n\n\n",
3547                "a.rs": "\n\n\n\n\n A \n\n\n\n\n",
3548                "b.rs": "\n\n\n\n\n B \n\n\n\n\n",
3549                "c.rs": "\n\n\n\n\n B \n\n\n\n\n",
3550                "d.rs": "\n\n\n\n\n B \n\n\n\n\n",
3551                "e.rs": "\n\n\n\n\n B \n\n\n\n\n",
3552                "f.rs": "\n\n\n\n\n B \n\n\n\n\n",
3553                "g.rs": "\n\n\n\n\n B \n\n\n\n\n",
3554                "h.rs": "\n\n\n\n\n B \n\n\n\n\n",
3555                "i.rs": "\n\n\n\n\n B \n\n\n\n\n",
3556                "j.rs": "\n\n\n\n\n B \n\n\n\n\n",
3557                "k.rs": "\n\n\n\n\n B \n\n\n\n\n",
3558            }),
3559        )
3560        .await;
3561        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
3562        let window = cx.add_window(|cx| Workspace::test_new(project.clone(), cx));
3563        let workspace = window.root(cx).unwrap();
3564        let search = cx.new_model(|cx| ProjectSearch::new(project, cx));
3565        let search_view = cx.add_window(|cx| {
3566            ProjectSearchView::new(workspace.downgrade(), search.clone(), cx, None)
3567        });
3568
3569        // First search
3570        perform_search(search_view, "A", cx);
3571        search_view
3572            .update(cx, |search_view, cx| {
3573                search_view.results_editor.update(cx, |results_editor, cx| {
3574                    // Results are correct and scrolled to the top
3575                    assert_eq!(
3576                        results_editor.display_text(cx).match_indices(" A ").count(),
3577                        10
3578                    );
3579                    assert_eq!(results_editor.scroll_position(cx), Point::default());
3580
3581                    // Scroll results all the way down
3582                    results_editor.scroll(Point::new(0., f32::MAX), Some(Axis::Vertical), cx);
3583                });
3584            })
3585            .expect("unable to update search view");
3586
3587        // Second search
3588        perform_search(search_view, "B", cx);
3589        search_view
3590            .update(cx, |search_view, cx| {
3591                search_view.results_editor.update(cx, |results_editor, cx| {
3592                    // Results are correct...
3593                    assert_eq!(
3594                        results_editor.display_text(cx).match_indices(" B ").count(),
3595                        10
3596                    );
3597                    // ...and scrolled back to the top
3598                    assert_eq!(results_editor.scroll_position(cx), Point::default());
3599                });
3600            })
3601            .expect("unable to update search view");
3602    }
3603
3604    #[gpui::test]
3605    async fn test_buffer_search_query_reused(cx: &mut TestAppContext) {
3606        init_test(cx);
3607
3608        let fs = FakeFs::new(cx.background_executor.clone());
3609        fs.insert_tree(
3610            "/dir",
3611            json!({
3612                "one.rs": "const ONE: usize = 1;",
3613            }),
3614        )
3615        .await;
3616        let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
3617        let worktree_id = project.update(cx, |this, cx| {
3618            this.worktrees(cx).next().unwrap().read(cx).id()
3619        });
3620        let window = cx.add_window(|cx| Workspace::test_new(project.clone(), cx));
3621        let workspace = window.root(cx).unwrap();
3622        let mut cx = VisualTestContext::from_window(*window.deref(), cx);
3623
3624        let editor = workspace
3625            .update(&mut cx, |workspace, cx| {
3626                workspace.open_path((worktree_id, "one.rs"), None, true, cx)
3627            })
3628            .await
3629            .unwrap()
3630            .downcast::<Editor>()
3631            .unwrap();
3632
3633        let buffer_search_bar = cx.new_view(|cx| {
3634            let mut search_bar = BufferSearchBar::new(cx);
3635            search_bar.set_active_pane_item(Some(&editor), cx);
3636            search_bar.show(cx);
3637            search_bar
3638        });
3639
3640        let panes: Vec<_> = window
3641            .update(&mut cx, |this, _| this.panes().to_owned())
3642            .unwrap();
3643        assert_eq!(panes.len(), 1);
3644        let pane = panes.first().cloned().unwrap();
3645        pane.update(&mut cx, |pane, cx| {
3646            pane.toolbar().update(cx, |toolbar, cx| {
3647                toolbar.add_item(buffer_search_bar.clone(), cx);
3648            })
3649        });
3650
3651        let buffer_search_query = "search bar query";
3652        buffer_search_bar
3653            .update(&mut cx, |buffer_search_bar, cx| {
3654                buffer_search_bar.focus_handle(cx).focus(cx);
3655                buffer_search_bar.search(buffer_search_query, None, cx)
3656            })
3657            .await
3658            .unwrap();
3659
3660        workspace.update(&mut cx, |workspace, cx| {
3661            ProjectSearchView::new_search(workspace, &workspace::NewSearch, cx)
3662        });
3663        cx.run_until_parked();
3664        let project_search_view = pane
3665            .update(&mut cx, |pane, _| {
3666                pane.active_item()
3667                    .and_then(|item| item.downcast::<ProjectSearchView>())
3668            })
3669            .expect("should open a project search view after spawning a new search");
3670        project_search_view.update(&mut cx, |search_view, cx| {
3671            assert_eq!(
3672                search_view.search_query_text(cx),
3673                buffer_search_query,
3674                "Project search should take the query from the buffer search bar since it got focused and had a query inside"
3675            );
3676        });
3677    }
3678
3679    fn init_test(cx: &mut TestAppContext) {
3680        cx.update(|cx| {
3681            let settings = SettingsStore::test(cx);
3682            cx.set_global(settings);
3683
3684            theme::init(theme::LoadThemes::JustBase, cx);
3685
3686            language::init(cx);
3687            client::init_settings(cx);
3688            editor::init(cx);
3689            workspace::init_settings(cx);
3690            Project::init_settings(cx);
3691            crate::init(cx);
3692        });
3693    }
3694
3695    fn perform_search(
3696        search_view: WindowHandle<ProjectSearchView>,
3697        text: impl Into<Arc<str>>,
3698        cx: &mut TestAppContext,
3699    ) {
3700        search_view
3701            .update(cx, |search_view, cx| {
3702                search_view
3703                    .query_editor
3704                    .update(cx, |query_editor, cx| query_editor.set_text(text, cx));
3705                search_view.search(cx);
3706            })
3707            .unwrap();
3708        cx.background_executor.run_until_parked();
3709    }
3710}