1use crate::{
2 BufferSearchBar, FocusSearch, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext,
3 SearchOption, SearchOptions, SearchSource, SelectNextMatch, SelectPreviousMatch,
4 ToggleCaseSensitive, ToggleIncludeIgnored, ToggleRegex, ToggleReplace, ToggleWholeWord,
5 buffer_search::Deploy,
6 search_bar::{ActionButtonState, input_base_styles, render_action_button, render_text_input},
7};
8use anyhow::Context as _;
9use collections::HashMap;
10use editor::{
11 Anchor, Editor, EditorEvent, EditorSettings, MAX_TAB_TITLE_LEN, MultiBuffer, PathKey,
12 SelectionEffects,
13 actions::{Backtab, SelectAll, Tab},
14 items::active_match_index,
15 multibuffer_context_lines,
16 scroll::Autoscroll,
17};
18use futures::{StreamExt, stream::FuturesOrdered};
19use gpui::{
20 Action, AnyElement, AnyView, App, Axis, Context, Entity, EntityId, EventEmitter, FocusHandle,
21 Focusable, Global, Hsla, InteractiveElement, IntoElement, KeyContext, ParentElement, Point,
22 Render, SharedString, Styled, Subscription, Task, UpdateGlobal, WeakEntity, Window, actions,
23 div,
24};
25use language::{Buffer, Language};
26use menu::Confirm;
27use project::{
28 Project, ProjectPath,
29 search::{SearchInputKind, SearchQuery},
30 search_history::SearchHistoryCursor,
31};
32use settings::Settings;
33use std::{
34 any::{Any, TypeId},
35 mem,
36 ops::{Not, Range},
37 pin::pin,
38 sync::Arc,
39};
40use ui::{IconButtonShape, KeyBinding, Toggleable, Tooltip, prelude::*, utils::SearchInputWidth};
41use util::{ResultExt as _, paths::PathMatcher, rel_path::RelPath};
42use workspace::{
43 DeploySearch, ItemNavHistory, NewSearch, ToolbarItemEvent, ToolbarItemLocation,
44 ToolbarItemView, Workspace, WorkspaceId,
45 item::{BreadcrumbText, Item, ItemEvent, ItemHandle, SaveOptions},
46 searchable::{Direction, SearchableItem, SearchableItemHandle},
47};
48
49actions!(
50 project_search,
51 [
52 /// Searches in a new project search tab.
53 SearchInNew,
54 /// Toggles focus between the search bar and the search results.
55 ToggleFocus,
56 /// Moves to the next input field.
57 NextField,
58 /// Toggles the search filters panel.
59 ToggleFilters,
60 /// Toggles collapse/expand state of all search result excerpts.
61 ToggleAllSearchResults
62 ]
63);
64
65#[derive(Default)]
66struct ActiveSettings(HashMap<WeakEntity<Project>, ProjectSearchSettings>);
67
68impl Global for ActiveSettings {}
69
70pub fn init(cx: &mut App) {
71 cx.set_global(ActiveSettings::default());
72 cx.observe_new(|workspace: &mut Workspace, _window, _cx| {
73 register_workspace_action(workspace, move |search_bar, _: &Deploy, window, cx| {
74 search_bar.focus_search(window, cx);
75 });
76 register_workspace_action(workspace, move |search_bar, _: &FocusSearch, window, cx| {
77 search_bar.focus_search(window, cx);
78 });
79 register_workspace_action(
80 workspace,
81 move |search_bar, _: &ToggleFilters, window, cx| {
82 search_bar.toggle_filters(window, cx);
83 },
84 );
85 register_workspace_action(
86 workspace,
87 move |search_bar, _: &ToggleCaseSensitive, window, cx| {
88 search_bar.toggle_search_option(SearchOptions::CASE_SENSITIVE, window, cx);
89 },
90 );
91 register_workspace_action(
92 workspace,
93 move |search_bar, _: &ToggleWholeWord, window, cx| {
94 search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, window, cx);
95 },
96 );
97 register_workspace_action(workspace, move |search_bar, _: &ToggleRegex, window, cx| {
98 search_bar.toggle_search_option(SearchOptions::REGEX, window, cx);
99 });
100 register_workspace_action(
101 workspace,
102 move |search_bar, action: &ToggleReplace, window, cx| {
103 search_bar.toggle_replace(action, window, cx)
104 },
105 );
106 register_workspace_action(
107 workspace,
108 move |search_bar, action: &SelectPreviousMatch, window, cx| {
109 search_bar.select_prev_match(action, window, cx)
110 },
111 );
112 register_workspace_action(
113 workspace,
114 move |search_bar, action: &SelectNextMatch, window, cx| {
115 search_bar.select_next_match(action, window, cx)
116 },
117 );
118
119 // Only handle search_in_new if there is a search present
120 register_workspace_action_for_present_search(workspace, |workspace, action, window, cx| {
121 ProjectSearchView::search_in_new(workspace, action, window, cx)
122 });
123
124 register_workspace_action_for_present_search(
125 workspace,
126 |workspace, action: &ToggleAllSearchResults, window, cx| {
127 if let Some(search_view) = workspace
128 .active_item(cx)
129 .and_then(|item| item.downcast::<ProjectSearchView>())
130 {
131 search_view.update(cx, |search_view, cx| {
132 search_view.toggle_all_search_results(action, window, cx);
133 });
134 }
135 },
136 );
137
138 register_workspace_action_for_present_search(
139 workspace,
140 |workspace, _: &menu::Cancel, window, cx| {
141 if let Some(project_search_bar) = workspace
142 .active_pane()
143 .read(cx)
144 .toolbar()
145 .read(cx)
146 .item_of_type::<ProjectSearchBar>()
147 {
148 project_search_bar.update(cx, |project_search_bar, cx| {
149 let search_is_focused = project_search_bar
150 .active_project_search
151 .as_ref()
152 .is_some_and(|search_view| {
153 search_view
154 .read(cx)
155 .query_editor
156 .read(cx)
157 .focus_handle(cx)
158 .is_focused(window)
159 });
160 if search_is_focused {
161 project_search_bar.move_focus_to_results(window, cx);
162 } else {
163 project_search_bar.focus_search(window, cx)
164 }
165 });
166 } else {
167 cx.propagate();
168 }
169 },
170 );
171
172 // Both on present and dismissed search, we need to unconditionally handle those actions to focus from the editor.
173 workspace.register_action(move |workspace, action: &DeploySearch, window, cx| {
174 if workspace.has_active_modal(window, cx) && !workspace.hide_modal(window, cx) {
175 cx.propagate();
176 return;
177 }
178 ProjectSearchView::deploy_search(workspace, action, window, cx);
179 cx.notify();
180 });
181 workspace.register_action(move |workspace, action: &NewSearch, window, cx| {
182 if workspace.has_active_modal(window, cx) && !workspace.hide_modal(window, cx) {
183 cx.propagate();
184 return;
185 }
186 ProjectSearchView::new_search(workspace, action, window, cx);
187 cx.notify();
188 });
189 })
190 .detach();
191}
192
193fn contains_uppercase(str: &str) -> bool {
194 str.chars().any(|c| c.is_uppercase())
195}
196
197pub struct ProjectSearch {
198 project: Entity<Project>,
199 excerpts: Entity<MultiBuffer>,
200 pending_search: Option<Task<Option<()>>>,
201 match_ranges: Vec<Range<Anchor>>,
202 active_query: Option<SearchQuery>,
203 last_search_query_text: Option<String>,
204 search_id: usize,
205 no_results: Option<bool>,
206 limit_reached: bool,
207 search_history_cursor: SearchHistoryCursor,
208 search_included_history_cursor: SearchHistoryCursor,
209 search_excluded_history_cursor: SearchHistoryCursor,
210}
211
212#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
213enum InputPanel {
214 Query,
215 Replacement,
216 Exclude,
217 Include,
218}
219
220pub struct ProjectSearchView {
221 workspace: WeakEntity<Workspace>,
222 focus_handle: FocusHandle,
223 entity: Entity<ProjectSearch>,
224 query_editor: Entity<Editor>,
225 replacement_editor: Entity<Editor>,
226 results_editor: Entity<Editor>,
227 search_options: SearchOptions,
228 panels_with_errors: HashMap<InputPanel, String>,
229 active_match_index: Option<usize>,
230 search_id: usize,
231 included_files_editor: Entity<Editor>,
232 excluded_files_editor: Entity<Editor>,
233 filters_enabled: bool,
234 replace_enabled: bool,
235 included_opened_only: bool,
236 regex_language: Option<Arc<Language>>,
237 results_collapsed: bool,
238 _subscriptions: Vec<Subscription>,
239}
240
241#[derive(Debug, Clone)]
242pub struct ProjectSearchSettings {
243 search_options: SearchOptions,
244 filters_enabled: bool,
245}
246
247pub struct ProjectSearchBar {
248 active_project_search: Option<Entity<ProjectSearchView>>,
249 subscription: Option<Subscription>,
250}
251
252impl ProjectSearch {
253 pub fn new(project: Entity<Project>, cx: &mut Context<Self>) -> Self {
254 let capability = project.read(cx).capability();
255
256 Self {
257 project,
258 excerpts: cx.new(|_| MultiBuffer::new(capability)),
259 pending_search: Default::default(),
260 match_ranges: Default::default(),
261 active_query: None,
262 last_search_query_text: None,
263 search_id: 0,
264 no_results: None,
265 limit_reached: false,
266 search_history_cursor: Default::default(),
267 search_included_history_cursor: Default::default(),
268 search_excluded_history_cursor: Default::default(),
269 }
270 }
271
272 fn clone(&self, cx: &mut Context<Self>) -> Entity<Self> {
273 cx.new(|cx| Self {
274 project: self.project.clone(),
275 excerpts: self
276 .excerpts
277 .update(cx, |excerpts, cx| cx.new(|cx| excerpts.clone(cx))),
278 pending_search: Default::default(),
279 match_ranges: self.match_ranges.clone(),
280 active_query: self.active_query.clone(),
281 last_search_query_text: self.last_search_query_text.clone(),
282 search_id: self.search_id,
283 no_results: self.no_results,
284 limit_reached: self.limit_reached,
285 search_history_cursor: self.search_history_cursor.clone(),
286 search_included_history_cursor: self.search_included_history_cursor.clone(),
287 search_excluded_history_cursor: self.search_excluded_history_cursor.clone(),
288 })
289 }
290 fn cursor(&self, kind: SearchInputKind) -> &SearchHistoryCursor {
291 match kind {
292 SearchInputKind::Query => &self.search_history_cursor,
293 SearchInputKind::Include => &self.search_included_history_cursor,
294 SearchInputKind::Exclude => &self.search_excluded_history_cursor,
295 }
296 }
297 fn cursor_mut(&mut self, kind: SearchInputKind) -> &mut SearchHistoryCursor {
298 match kind {
299 SearchInputKind::Query => &mut self.search_history_cursor,
300 SearchInputKind::Include => &mut self.search_included_history_cursor,
301 SearchInputKind::Exclude => &mut self.search_excluded_history_cursor,
302 }
303 }
304
305 fn search(&mut self, query: SearchQuery, cx: &mut Context<Self>) {
306 let search = self.project.update(cx, |project, cx| {
307 project
308 .search_history_mut(SearchInputKind::Query)
309 .add(&mut self.search_history_cursor, query.as_str().to_string());
310 let included = query.as_inner().files_to_include().sources().join(",");
311 if !included.is_empty() {
312 project
313 .search_history_mut(SearchInputKind::Include)
314 .add(&mut self.search_included_history_cursor, included);
315 }
316 let excluded = query.as_inner().files_to_exclude().sources().join(",");
317 if !excluded.is_empty() {
318 project
319 .search_history_mut(SearchInputKind::Exclude)
320 .add(&mut self.search_excluded_history_cursor, excluded);
321 }
322 project.search(query.clone(), cx)
323 });
324 self.last_search_query_text = Some(query.as_str().to_string());
325 self.search_id += 1;
326 self.active_query = Some(query);
327 self.match_ranges.clear();
328 self.pending_search = Some(cx.spawn(async move |project_search, cx| {
329 let mut matches = pin!(search.ready_chunks(1024));
330 project_search
331 .update(cx, |project_search, cx| {
332 project_search.match_ranges.clear();
333 project_search
334 .excerpts
335 .update(cx, |excerpts, cx| excerpts.clear(cx));
336 project_search.no_results = Some(true);
337 project_search.limit_reached = false;
338 })
339 .ok()?;
340
341 let mut limit_reached = false;
342 while let Some(results) = matches.next().await {
343 let (buffers_with_ranges, has_reached_limit) = cx
344 .background_executor()
345 .spawn(async move {
346 let mut limit_reached = false;
347 let mut buffers_with_ranges = Vec::with_capacity(results.len());
348 for result in results {
349 match result {
350 project::search::SearchResult::Buffer { buffer, ranges } => {
351 buffers_with_ranges.push((buffer, ranges));
352 }
353 project::search::SearchResult::LimitReached => {
354 limit_reached = true;
355 }
356 }
357 }
358 (buffers_with_ranges, limit_reached)
359 })
360 .await;
361 limit_reached |= has_reached_limit;
362 let mut new_ranges = project_search
363 .update(cx, |project_search, cx| {
364 project_search.excerpts.update(cx, |excerpts, cx| {
365 buffers_with_ranges
366 .into_iter()
367 .map(|(buffer, ranges)| {
368 excerpts.set_anchored_excerpts_for_path(
369 PathKey::for_buffer(&buffer, cx),
370 buffer,
371 ranges,
372 multibuffer_context_lines(cx),
373 cx,
374 )
375 })
376 .collect::<FuturesOrdered<_>>()
377 })
378 })
379 .ok()?;
380 while let Some(new_ranges) = new_ranges.next().await {
381 project_search
382 .update(cx, |project_search, cx| {
383 project_search.match_ranges.extend(new_ranges);
384 cx.notify();
385 })
386 .ok()?;
387 }
388 }
389
390 project_search
391 .update(cx, |project_search, cx| {
392 if !project_search.match_ranges.is_empty() {
393 project_search.no_results = Some(false);
394 }
395 project_search.limit_reached = limit_reached;
396 project_search.pending_search.take();
397 cx.notify();
398 })
399 .ok()?;
400
401 None
402 }));
403 cx.notify();
404 }
405}
406
407#[derive(Clone, Debug, PartialEq, Eq)]
408pub enum ViewEvent {
409 UpdateTab,
410 Activate,
411 EditorEvent(editor::EditorEvent),
412 Dismiss,
413}
414
415impl EventEmitter<ViewEvent> for ProjectSearchView {}
416
417impl Render for ProjectSearchView {
418 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
419 if self.has_matches() {
420 div()
421 .flex_1()
422 .size_full()
423 .track_focus(&self.focus_handle(cx))
424 .child(self.results_editor.clone())
425 } else {
426 let model = self.entity.read(cx);
427 let has_no_results = model.no_results.unwrap_or(false);
428 let is_search_underway = model.pending_search.is_some();
429
430 let heading_text = if is_search_underway {
431 "Searching…"
432 } else if has_no_results {
433 "No Results"
434 } else {
435 "Search All Files"
436 };
437
438 let heading_text = div()
439 .justify_center()
440 .child(Label::new(heading_text).size(LabelSize::Large));
441
442 let page_content: Option<AnyElement> = if let Some(no_results) = model.no_results {
443 if model.pending_search.is_none() && no_results {
444 Some(
445 Label::new("No results found in this project for the provided query")
446 .size(LabelSize::Small)
447 .into_any_element(),
448 )
449 } else {
450 None
451 }
452 } else {
453 Some(self.landing_text_minor(cx).into_any_element())
454 };
455
456 let page_content = page_content.map(|text| div().child(text));
457
458 h_flex()
459 .size_full()
460 .items_center()
461 .justify_center()
462 .overflow_hidden()
463 .bg(cx.theme().colors().editor_background)
464 .track_focus(&self.focus_handle(cx))
465 .child(
466 v_flex()
467 .id("project-search-landing-page")
468 .overflow_y_scroll()
469 .gap_1()
470 .child(heading_text)
471 .children(page_content),
472 )
473 }
474 }
475}
476
477impl Focusable for ProjectSearchView {
478 fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
479 self.focus_handle.clone()
480 }
481}
482
483impl Item for ProjectSearchView {
484 type Event = ViewEvent;
485 fn tab_tooltip_text(&self, cx: &App) -> Option<SharedString> {
486 let query_text = self.query_editor.read(cx).text(cx);
487
488 query_text
489 .is_empty()
490 .not()
491 .then(|| query_text.into())
492 .or_else(|| Some("Project Search".into()))
493 }
494
495 fn act_as_type<'a>(
496 &'a self,
497 type_id: TypeId,
498 self_handle: &'a Entity<Self>,
499 _: &'a App,
500 ) -> Option<AnyView> {
501 if type_id == TypeId::of::<Self>() {
502 Some(self_handle.clone().into())
503 } else if type_id == TypeId::of::<Editor>() {
504 Some(self.results_editor.clone().into())
505 } else {
506 None
507 }
508 }
509 fn as_searchable(&self, _: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
510 Some(Box::new(self.results_editor.clone()))
511 }
512
513 fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
514 self.results_editor
515 .update(cx, |editor, cx| editor.deactivated(window, cx));
516 }
517
518 fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
519 Some(Icon::new(IconName::MagnifyingGlass))
520 }
521
522 fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString {
523 let last_query: Option<SharedString> = self
524 .entity
525 .read(cx)
526 .last_search_query_text
527 .as_ref()
528 .map(|query| {
529 let query = query.replace('\n', "");
530 let query_text = util::truncate_and_trailoff(&query, MAX_TAB_TITLE_LEN);
531 query_text.into()
532 });
533
534 last_query
535 .filter(|query| !query.is_empty())
536 .unwrap_or_else(|| "Project Search".into())
537 }
538
539 fn telemetry_event_text(&self) -> Option<&'static str> {
540 Some("Project Search Opened")
541 }
542
543 fn for_each_project_item(
544 &self,
545 cx: &App,
546 f: &mut dyn FnMut(EntityId, &dyn project::ProjectItem),
547 ) {
548 self.results_editor.for_each_project_item(cx, f)
549 }
550
551 fn can_save(&self, _: &App) -> bool {
552 true
553 }
554
555 fn is_dirty(&self, cx: &App) -> bool {
556 self.results_editor.read(cx).is_dirty(cx)
557 }
558
559 fn has_conflict(&self, cx: &App) -> bool {
560 self.results_editor.read(cx).has_conflict(cx)
561 }
562
563 fn save(
564 &mut self,
565 options: SaveOptions,
566 project: Entity<Project>,
567 window: &mut Window,
568 cx: &mut Context<Self>,
569 ) -> Task<anyhow::Result<()>> {
570 self.results_editor
571 .update(cx, |editor, cx| editor.save(options, project, window, cx))
572 }
573
574 fn save_as(
575 &mut self,
576 _: Entity<Project>,
577 _: ProjectPath,
578 _window: &mut Window,
579 _: &mut Context<Self>,
580 ) -> Task<anyhow::Result<()>> {
581 unreachable!("save_as should not have been called")
582 }
583
584 fn reload(
585 &mut self,
586 project: Entity<Project>,
587 window: &mut Window,
588 cx: &mut Context<Self>,
589 ) -> Task<anyhow::Result<()>> {
590 self.results_editor
591 .update(cx, |editor, cx| editor.reload(project, window, cx))
592 }
593
594 fn can_split(&self) -> bool {
595 true
596 }
597
598 fn clone_on_split(
599 &self,
600 _workspace_id: Option<WorkspaceId>,
601 window: &mut Window,
602 cx: &mut Context<Self>,
603 ) -> Task<Option<Entity<Self>>>
604 where
605 Self: Sized,
606 {
607 let model = self.entity.update(cx, |model, cx| model.clone(cx));
608 Task::ready(Some(cx.new(|cx| {
609 Self::new(self.workspace.clone(), model, window, cx, None)
610 })))
611 }
612
613 fn added_to_workspace(
614 &mut self,
615 workspace: &mut Workspace,
616 window: &mut Window,
617 cx: &mut Context<Self>,
618 ) {
619 self.results_editor.update(cx, |editor, cx| {
620 editor.added_to_workspace(workspace, window, cx)
621 });
622 }
623
624 fn set_nav_history(
625 &mut self,
626 nav_history: ItemNavHistory,
627 _: &mut Window,
628 cx: &mut Context<Self>,
629 ) {
630 self.results_editor.update(cx, |editor, _| {
631 editor.set_nav_history(Some(nav_history));
632 });
633 }
634
635 fn navigate(
636 &mut self,
637 data: Box<dyn Any>,
638 window: &mut Window,
639 cx: &mut Context<Self>,
640 ) -> bool {
641 self.results_editor
642 .update(cx, |editor, cx| editor.navigate(data, window, cx))
643 }
644
645 fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
646 match event {
647 ViewEvent::UpdateTab => {
648 f(ItemEvent::UpdateBreadcrumbs);
649 f(ItemEvent::UpdateTab);
650 }
651 ViewEvent::EditorEvent(editor_event) => {
652 Editor::to_item_events(editor_event, f);
653 }
654 ViewEvent::Dismiss => f(ItemEvent::CloseItem),
655 _ => {}
656 }
657 }
658
659 fn breadcrumb_location(&self, _: &App) -> ToolbarItemLocation {
660 if self.has_matches() {
661 ToolbarItemLocation::Secondary
662 } else {
663 ToolbarItemLocation::Hidden
664 }
665 }
666
667 fn breadcrumbs(&self, theme: &theme::Theme, cx: &App) -> Option<Vec<BreadcrumbText>> {
668 self.results_editor.breadcrumbs(theme, cx)
669 }
670
671 fn breadcrumb_prefix(
672 &self,
673 _window: &mut Window,
674 cx: &mut Context<Self>,
675 ) -> Option<gpui::AnyElement> {
676 if !self.has_matches() {
677 return None;
678 }
679
680 let is_collapsed = self.results_collapsed;
681
682 let (icon, tooltip_label) = if is_collapsed {
683 (IconName::ChevronUpDown, "Expand All Search Results")
684 } else {
685 (IconName::ChevronDownUp, "Collapse All Search Results")
686 };
687
688 let focus_handle = self.query_editor.focus_handle(cx);
689
690 Some(
691 IconButton::new("project-search-collapse-expand", icon)
692 .shape(IconButtonShape::Square)
693 .icon_size(IconSize::Small)
694 .tooltip(move |_, cx| {
695 Tooltip::for_action_in(
696 tooltip_label,
697 &ToggleAllSearchResults,
698 &focus_handle,
699 cx,
700 )
701 })
702 .on_click(cx.listener(|this, _, window, cx| {
703 this.toggle_all_search_results(&ToggleAllSearchResults, window, cx);
704 }))
705 .into_any_element(),
706 )
707 }
708}
709
710impl ProjectSearchView {
711 pub fn get_matches(&self, cx: &App) -> Vec<Range<Anchor>> {
712 self.entity.read(cx).match_ranges.clone()
713 }
714
715 fn toggle_filters(&mut self, cx: &mut Context<Self>) {
716 self.filters_enabled = !self.filters_enabled;
717 ActiveSettings::update_global(cx, |settings, cx| {
718 settings.0.insert(
719 self.entity.read(cx).project.downgrade(),
720 self.current_settings(),
721 );
722 });
723 }
724
725 fn current_settings(&self) -> ProjectSearchSettings {
726 ProjectSearchSettings {
727 search_options: self.search_options,
728 filters_enabled: self.filters_enabled,
729 }
730 }
731
732 fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut Context<Self>) {
733 self.search_options.toggle(option);
734 ActiveSettings::update_global(cx, |settings, cx| {
735 settings.0.insert(
736 self.entity.read(cx).project.downgrade(),
737 self.current_settings(),
738 );
739 });
740 self.adjust_query_regex_language(cx);
741 }
742
743 fn toggle_opened_only(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {
744 self.included_opened_only = !self.included_opened_only;
745 }
746
747 pub fn replacement(&self, cx: &App) -> String {
748 self.replacement_editor.read(cx).text(cx)
749 }
750
751 fn replace_next(&mut self, _: &ReplaceNext, window: &mut Window, cx: &mut Context<Self>) {
752 if let Some(last_search_query_text) = &self.entity.read(cx).last_search_query_text
753 && self.query_editor.read(cx).text(cx) != *last_search_query_text
754 {
755 // search query has changed, restart search and bail
756 self.search(cx);
757 return;
758 }
759 if self.entity.read(cx).match_ranges.is_empty() {
760 return;
761 }
762 let Some(active_index) = self.active_match_index else {
763 return;
764 };
765
766 let query = self.entity.read(cx).active_query.clone();
767 if let Some(query) = query {
768 let query = query.with_replacement(self.replacement(cx));
769
770 // TODO: Do we need the clone here?
771 let mat = self.entity.read(cx).match_ranges[active_index].clone();
772 self.results_editor.update(cx, |editor, cx| {
773 editor.replace(&mat, &query, window, cx);
774 });
775 self.select_match(Direction::Next, window, cx)
776 }
777 }
778 fn replace_all(&mut self, _: &ReplaceAll, window: &mut Window, cx: &mut Context<Self>) {
779 if let Some(last_search_query_text) = &self.entity.read(cx).last_search_query_text
780 && self.query_editor.read(cx).text(cx) != *last_search_query_text
781 {
782 // search query has changed, restart search and bail
783 self.search(cx);
784 return;
785 }
786 if self.active_match_index.is_none() {
787 return;
788 }
789 let Some(query) = self.entity.read(cx).active_query.as_ref() else {
790 return;
791 };
792 let query = query.clone().with_replacement(self.replacement(cx));
793
794 let match_ranges = self
795 .entity
796 .update(cx, |model, _| mem::take(&mut model.match_ranges));
797 if match_ranges.is_empty() {
798 return;
799 }
800
801 self.results_editor.update(cx, |editor, cx| {
802 editor.replace_all(&mut match_ranges.iter(), &query, window, cx);
803 });
804
805 self.entity.update(cx, |model, _cx| {
806 model.match_ranges = match_ranges;
807 });
808 }
809
810 fn toggle_all_search_results(
811 &mut self,
812 _: &ToggleAllSearchResults,
813 _window: &mut Window,
814 cx: &mut Context<Self>,
815 ) {
816 self.results_collapsed = !self.results_collapsed;
817 self.update_results_visibility(cx);
818 }
819
820 fn update_results_visibility(&mut self, cx: &mut Context<Self>) {
821 self.results_editor.update(cx, |editor, cx| {
822 let multibuffer = editor.buffer().read(cx);
823 let buffer_ids = multibuffer.excerpt_buffer_ids();
824
825 if self.results_collapsed {
826 for buffer_id in buffer_ids {
827 editor.fold_buffer(buffer_id, cx);
828 }
829 } else {
830 for buffer_id in buffer_ids {
831 editor.unfold_buffer(buffer_id, cx);
832 }
833 }
834 });
835 cx.notify();
836 }
837
838 pub fn new(
839 workspace: WeakEntity<Workspace>,
840 entity: Entity<ProjectSearch>,
841 window: &mut Window,
842 cx: &mut Context<Self>,
843 settings: Option<ProjectSearchSettings>,
844 ) -> Self {
845 let project;
846 let excerpts;
847 let mut replacement_text = None;
848 let mut query_text = String::new();
849 let mut subscriptions = Vec::new();
850
851 // Read in settings if available
852 let (mut options, filters_enabled) = if let Some(settings) = settings {
853 (settings.search_options, settings.filters_enabled)
854 } else {
855 let search_options =
856 SearchOptions::from_settings(&EditorSettings::get_global(cx).search);
857 (search_options, false)
858 };
859
860 {
861 let entity = entity.read(cx);
862 project = entity.project.clone();
863 excerpts = entity.excerpts.clone();
864 if let Some(active_query) = entity.active_query.as_ref() {
865 query_text = active_query.as_str().to_string();
866 replacement_text = active_query.replacement().map(ToOwned::to_owned);
867 options = SearchOptions::from_query(active_query);
868 }
869 }
870 subscriptions.push(cx.observe_in(&entity, window, |this, _, window, cx| {
871 this.entity_changed(window, cx)
872 }));
873
874 let query_editor = cx.new(|cx| {
875 let mut editor = Editor::single_line(window, cx);
876 editor.set_placeholder_text("Search all files…", window, cx);
877 editor.set_text(query_text, window, cx);
878 editor
879 });
880 // Subscribe to query_editor in order to reraise editor events for workspace item activation purposes
881 subscriptions.push(
882 cx.subscribe(&query_editor, |this, _, event: &EditorEvent, cx| {
883 if let EditorEvent::Edited { .. } = event
884 && EditorSettings::get_global(cx).use_smartcase_search
885 {
886 let query = this.search_query_text(cx);
887 if !query.is_empty()
888 && this.search_options.contains(SearchOptions::CASE_SENSITIVE)
889 != contains_uppercase(&query)
890 {
891 this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
892 }
893 }
894 cx.emit(ViewEvent::EditorEvent(event.clone()))
895 }),
896 );
897 let replacement_editor = cx.new(|cx| {
898 let mut editor = Editor::single_line(window, cx);
899 editor.set_placeholder_text("Replace in project…", window, cx);
900 if let Some(text) = replacement_text {
901 editor.set_text(text, window, cx);
902 }
903 editor
904 });
905 let results_editor = cx.new(|cx| {
906 let mut editor = Editor::for_multibuffer(excerpts, Some(project.clone()), window, cx);
907 editor.set_searchable(false);
908 editor.set_in_project_search(true);
909 editor
910 });
911 subscriptions.push(cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab)));
912
913 subscriptions.push(
914 cx.subscribe(&results_editor, |this, _, event: &EditorEvent, cx| {
915 if matches!(event, editor::EditorEvent::SelectionsChanged { .. }) {
916 this.update_match_index(cx);
917 }
918 // Reraise editor events for workspace item activation purposes
919 cx.emit(ViewEvent::EditorEvent(event.clone()));
920 }),
921 );
922
923 let included_files_editor = cx.new(|cx| {
924 let mut editor = Editor::single_line(window, cx);
925 editor.set_placeholder_text("Include: crates/**/*.toml", window, cx);
926
927 editor
928 });
929 // Subscribe to include_files_editor in order to reraise editor events for workspace item activation purposes
930 subscriptions.push(
931 cx.subscribe(&included_files_editor, |_, _, event: &EditorEvent, cx| {
932 cx.emit(ViewEvent::EditorEvent(event.clone()))
933 }),
934 );
935
936 let excluded_files_editor = cx.new(|cx| {
937 let mut editor = Editor::single_line(window, cx);
938 editor.set_placeholder_text("Exclude: vendor/*, *.lock", window, cx);
939
940 editor
941 });
942 // Subscribe to excluded_files_editor in order to reraise editor events for workspace item activation purposes
943 subscriptions.push(
944 cx.subscribe(&excluded_files_editor, |_, _, event: &EditorEvent, cx| {
945 cx.emit(ViewEvent::EditorEvent(event.clone()))
946 }),
947 );
948
949 let focus_handle = cx.focus_handle();
950 subscriptions.push(cx.on_focus(&focus_handle, window, |_, window, cx| {
951 cx.on_next_frame(window, |this, window, cx| {
952 if this.focus_handle.is_focused(window) {
953 if this.has_matches() {
954 this.results_editor.focus_handle(cx).focus(window);
955 } else {
956 this.query_editor.focus_handle(cx).focus(window);
957 }
958 }
959 });
960 }));
961
962 let languages = project.read(cx).languages().clone();
963 cx.spawn(async move |project_search_view, cx| {
964 let regex_language = languages
965 .language_for_name("regex")
966 .await
967 .context("loading regex language")?;
968 project_search_view
969 .update(cx, |project_search_view, cx| {
970 project_search_view.regex_language = Some(regex_language);
971 project_search_view.adjust_query_regex_language(cx);
972 })
973 .ok();
974 anyhow::Ok(())
975 })
976 .detach_and_log_err(cx);
977
978 // Check if Worktrees have all been previously indexed
979 let mut this = ProjectSearchView {
980 workspace,
981 focus_handle,
982 replacement_editor,
983 search_id: entity.read(cx).search_id,
984 entity,
985 query_editor,
986 results_editor,
987 search_options: options,
988 panels_with_errors: HashMap::default(),
989 active_match_index: None,
990 included_files_editor,
991 excluded_files_editor,
992 filters_enabled,
993 replace_enabled: false,
994 included_opened_only: false,
995 regex_language: None,
996 results_collapsed: false,
997 _subscriptions: subscriptions,
998 };
999
1000 this.entity_changed(window, cx);
1001 this
1002 }
1003
1004 pub fn new_search_in_directory(
1005 workspace: &mut Workspace,
1006 dir_path: &RelPath,
1007 window: &mut Window,
1008 cx: &mut Context<Workspace>,
1009 ) {
1010 let filter_str = dir_path.display(workspace.path_style(cx));
1011
1012 let weak_workspace = cx.entity().downgrade();
1013
1014 let entity = cx.new(|cx| ProjectSearch::new(workspace.project().clone(), cx));
1015 let search = cx.new(|cx| ProjectSearchView::new(weak_workspace, entity, window, cx, None));
1016 workspace.add_item_to_active_pane(Box::new(search.clone()), None, true, window, cx);
1017 search.update(cx, |search, cx| {
1018 search
1019 .included_files_editor
1020 .update(cx, |editor, cx| editor.set_text(filter_str, window, cx));
1021 search.filters_enabled = true;
1022 search.focus_query_editor(window, cx)
1023 });
1024 }
1025
1026 /// Re-activate the most recently activated search in this pane or the most recent if it has been closed.
1027 /// If no search exists in the workspace, create a new one.
1028 pub fn deploy_search(
1029 workspace: &mut Workspace,
1030 action: &workspace::DeploySearch,
1031 window: &mut Window,
1032 cx: &mut Context<Workspace>,
1033 ) {
1034 let existing = workspace
1035 .active_pane()
1036 .read(cx)
1037 .items()
1038 .find_map(|item| item.downcast::<ProjectSearchView>());
1039
1040 Self::existing_or_new_search(workspace, existing, action, window, cx);
1041 }
1042
1043 fn search_in_new(
1044 workspace: &mut Workspace,
1045 _: &SearchInNew,
1046 window: &mut Window,
1047 cx: &mut Context<Workspace>,
1048 ) {
1049 if let Some(search_view) = workspace
1050 .active_item(cx)
1051 .and_then(|item| item.downcast::<ProjectSearchView>())
1052 {
1053 let new_query = search_view.update(cx, |search_view, cx| {
1054 let open_buffers = if search_view.included_opened_only {
1055 Some(search_view.open_buffers(cx, workspace))
1056 } else {
1057 None
1058 };
1059 let new_query = search_view.build_search_query(cx, open_buffers);
1060 if new_query.is_some()
1061 && let Some(old_query) = search_view.entity.read(cx).active_query.clone()
1062 {
1063 search_view.query_editor.update(cx, |editor, cx| {
1064 editor.set_text(old_query.as_str(), window, cx);
1065 });
1066 search_view.search_options = SearchOptions::from_query(&old_query);
1067 search_view.adjust_query_regex_language(cx);
1068 }
1069 new_query
1070 });
1071 if let Some(new_query) = new_query {
1072 let entity = cx.new(|cx| {
1073 let mut entity = ProjectSearch::new(workspace.project().clone(), cx);
1074 entity.search(new_query, cx);
1075 entity
1076 });
1077 let weak_workspace = cx.entity().downgrade();
1078 workspace.add_item_to_active_pane(
1079 Box::new(cx.new(|cx| {
1080 ProjectSearchView::new(weak_workspace, entity, window, cx, None)
1081 })),
1082 None,
1083 true,
1084 window,
1085 cx,
1086 );
1087 }
1088 }
1089 }
1090
1091 // Add another search tab to the workspace.
1092 fn new_search(
1093 workspace: &mut Workspace,
1094 _: &workspace::NewSearch,
1095 window: &mut Window,
1096 cx: &mut Context<Workspace>,
1097 ) {
1098 Self::existing_or_new_search(workspace, None, &DeploySearch::find(), window, cx)
1099 }
1100
1101 fn existing_or_new_search(
1102 workspace: &mut Workspace,
1103 existing: Option<Entity<ProjectSearchView>>,
1104 action: &workspace::DeploySearch,
1105 window: &mut Window,
1106 cx: &mut Context<Workspace>,
1107 ) {
1108 let query = workspace.active_item(cx).and_then(|item| {
1109 if let Some(buffer_search_query) = buffer_search_query(workspace, item.as_ref(), cx) {
1110 return Some(buffer_search_query);
1111 }
1112
1113 let editor = item.act_as::<Editor>(cx)?;
1114 let query = editor.query_suggestion(window, cx);
1115 if query.is_empty() { None } else { Some(query) }
1116 });
1117
1118 let search = if let Some(existing) = existing {
1119 workspace.activate_item(&existing, true, true, window, cx);
1120 existing
1121 } else {
1122 let settings = cx
1123 .global::<ActiveSettings>()
1124 .0
1125 .get(&workspace.project().downgrade());
1126
1127 let settings = settings.cloned();
1128
1129 let weak_workspace = cx.entity().downgrade();
1130
1131 let project_search = cx.new(|cx| ProjectSearch::new(workspace.project().clone(), cx));
1132 let project_search_view = cx.new(|cx| {
1133 ProjectSearchView::new(weak_workspace, project_search, window, cx, settings)
1134 });
1135
1136 workspace.add_item_to_active_pane(
1137 Box::new(project_search_view.clone()),
1138 None,
1139 true,
1140 window,
1141 cx,
1142 );
1143 project_search_view
1144 };
1145
1146 search.update(cx, |search, cx| {
1147 search.replace_enabled = action.replace_enabled;
1148 if let Some(query) = query {
1149 search.set_query(&query, window, cx);
1150 }
1151 if let Some(included_files) = action.included_files.as_deref() {
1152 search
1153 .included_files_editor
1154 .update(cx, |editor, cx| editor.set_text(included_files, window, cx));
1155 search.filters_enabled = true;
1156 }
1157 if let Some(excluded_files) = action.excluded_files.as_deref() {
1158 search
1159 .excluded_files_editor
1160 .update(cx, |editor, cx| editor.set_text(excluded_files, window, cx));
1161 search.filters_enabled = true;
1162 }
1163 search.focus_query_editor(window, cx)
1164 });
1165 }
1166
1167 fn prompt_to_save_if_dirty_then_search(
1168 &mut self,
1169 window: &mut Window,
1170 cx: &mut Context<Self>,
1171 ) -> Task<anyhow::Result<()>> {
1172 let project = self.entity.read(cx).project.clone();
1173
1174 let can_autosave = self.results_editor.can_autosave(cx);
1175 let autosave_setting = self.results_editor.workspace_settings(cx).autosave;
1176
1177 let will_autosave = can_autosave && autosave_setting.should_save_on_close();
1178
1179 let is_dirty = self.is_dirty(cx);
1180
1181 cx.spawn_in(window, async move |this, cx| {
1182 let skip_save_on_close = this
1183 .read_with(cx, |this, cx| {
1184 this.workspace.read_with(cx, |workspace, cx| {
1185 workspace::Pane::skip_save_on_close(&this.results_editor, workspace, cx)
1186 })
1187 })?
1188 .unwrap_or(false);
1189
1190 let should_prompt_to_save = !skip_save_on_close && !will_autosave && is_dirty;
1191
1192 let should_search = if should_prompt_to_save {
1193 let options = &["Save", "Don't Save", "Cancel"];
1194 let result_channel = this.update_in(cx, |_, window, cx| {
1195 window.prompt(
1196 gpui::PromptLevel::Warning,
1197 "Project search buffer contains unsaved edits. Do you want to save it?",
1198 None,
1199 options,
1200 cx,
1201 )
1202 })?;
1203 let result = result_channel.await?;
1204 let should_save = result == 0;
1205 if should_save {
1206 this.update_in(cx, |this, window, cx| {
1207 this.save(
1208 SaveOptions {
1209 format: true,
1210 autosave: false,
1211 },
1212 project,
1213 window,
1214 cx,
1215 )
1216 })?
1217 .await
1218 .log_err();
1219 }
1220
1221 result != 2
1222 } else {
1223 true
1224 };
1225 if should_search {
1226 this.update(cx, |this, cx| {
1227 this.search(cx);
1228 })?;
1229 }
1230 anyhow::Ok(())
1231 })
1232 }
1233
1234 fn search(&mut self, cx: &mut Context<Self>) {
1235 let open_buffers = if self.included_opened_only {
1236 self.workspace
1237 .update(cx, |workspace, cx| self.open_buffers(cx, workspace))
1238 .ok()
1239 } else {
1240 None
1241 };
1242 if let Some(query) = self.build_search_query(cx, open_buffers) {
1243 self.entity.update(cx, |model, cx| model.search(query, cx));
1244 }
1245 }
1246
1247 pub fn search_query_text(&self, cx: &App) -> String {
1248 self.query_editor.read(cx).text(cx)
1249 }
1250
1251 fn build_search_query(
1252 &mut self,
1253 cx: &mut Context<Self>,
1254 open_buffers: Option<Vec<Entity<Buffer>>>,
1255 ) -> Option<SearchQuery> {
1256 // Do not bail early in this function, as we want to fill out `self.panels_with_errors`.
1257
1258 let text = self.search_query_text(cx);
1259 let included_files = self
1260 .filters_enabled
1261 .then(|| {
1262 match self.parse_path_matches(self.included_files_editor.read(cx).text(cx), cx) {
1263 Ok(included_files) => {
1264 let should_unmark_error =
1265 self.panels_with_errors.remove(&InputPanel::Include);
1266 if should_unmark_error.is_some() {
1267 cx.notify();
1268 }
1269 included_files
1270 }
1271 Err(e) => {
1272 let should_mark_error = self
1273 .panels_with_errors
1274 .insert(InputPanel::Include, e.to_string());
1275 if should_mark_error.is_none() {
1276 cx.notify();
1277 }
1278 PathMatcher::default()
1279 }
1280 }
1281 })
1282 .unwrap_or(PathMatcher::default());
1283 let excluded_files = self
1284 .filters_enabled
1285 .then(|| {
1286 match self.parse_path_matches(self.excluded_files_editor.read(cx).text(cx), cx) {
1287 Ok(excluded_files) => {
1288 let should_unmark_error =
1289 self.panels_with_errors.remove(&InputPanel::Exclude);
1290 if should_unmark_error.is_some() {
1291 cx.notify();
1292 }
1293
1294 excluded_files
1295 }
1296 Err(e) => {
1297 let should_mark_error = self
1298 .panels_with_errors
1299 .insert(InputPanel::Exclude, e.to_string());
1300 if should_mark_error.is_none() {
1301 cx.notify();
1302 }
1303 PathMatcher::default()
1304 }
1305 }
1306 })
1307 .unwrap_or(PathMatcher::default());
1308
1309 // If the project contains multiple visible worktrees, we match the
1310 // include/exclude patterns against full paths to allow them to be
1311 // disambiguated. For single worktree projects we use worktree relative
1312 // paths for convenience.
1313 let match_full_paths = self
1314 .entity
1315 .read(cx)
1316 .project
1317 .read(cx)
1318 .visible_worktrees(cx)
1319 .count()
1320 > 1;
1321
1322 let query = if self.search_options.contains(SearchOptions::REGEX) {
1323 match SearchQuery::regex(
1324 text,
1325 self.search_options.contains(SearchOptions::WHOLE_WORD),
1326 self.search_options.contains(SearchOptions::CASE_SENSITIVE),
1327 self.search_options.contains(SearchOptions::INCLUDE_IGNORED),
1328 self.search_options
1329 .contains(SearchOptions::ONE_MATCH_PER_LINE),
1330 included_files,
1331 excluded_files,
1332 match_full_paths,
1333 open_buffers,
1334 ) {
1335 Ok(query) => {
1336 let should_unmark_error = self.panels_with_errors.remove(&InputPanel::Query);
1337 if should_unmark_error.is_some() {
1338 cx.notify();
1339 }
1340
1341 Some(query)
1342 }
1343 Err(e) => {
1344 let should_mark_error = self
1345 .panels_with_errors
1346 .insert(InputPanel::Query, e.to_string());
1347 if should_mark_error.is_none() {
1348 cx.notify();
1349 }
1350
1351 None
1352 }
1353 }
1354 } else {
1355 match SearchQuery::text(
1356 text,
1357 self.search_options.contains(SearchOptions::WHOLE_WORD),
1358 self.search_options.contains(SearchOptions::CASE_SENSITIVE),
1359 self.search_options.contains(SearchOptions::INCLUDE_IGNORED),
1360 included_files,
1361 excluded_files,
1362 match_full_paths,
1363 open_buffers,
1364 ) {
1365 Ok(query) => {
1366 let should_unmark_error = self.panels_with_errors.remove(&InputPanel::Query);
1367 if should_unmark_error.is_some() {
1368 cx.notify();
1369 }
1370
1371 Some(query)
1372 }
1373 Err(e) => {
1374 let should_mark_error = self
1375 .panels_with_errors
1376 .insert(InputPanel::Query, e.to_string());
1377 if should_mark_error.is_none() {
1378 cx.notify();
1379 }
1380
1381 None
1382 }
1383 }
1384 };
1385 if !self.panels_with_errors.is_empty() {
1386 return None;
1387 }
1388 if query.as_ref().is_some_and(|query| query.is_empty()) {
1389 return None;
1390 }
1391 query
1392 }
1393
1394 fn open_buffers(&self, cx: &App, workspace: &Workspace) -> Vec<Entity<Buffer>> {
1395 let mut buffers = Vec::new();
1396 for editor in workspace.items_of_type::<Editor>(cx) {
1397 if let Some(buffer) = editor.read(cx).buffer().read(cx).as_singleton() {
1398 buffers.push(buffer);
1399 }
1400 }
1401 buffers
1402 }
1403
1404 fn parse_path_matches(&self, text: String, cx: &App) -> anyhow::Result<PathMatcher> {
1405 let path_style = self.entity.read(cx).project.read(cx).path_style(cx);
1406 let queries = text
1407 .split(',')
1408 .map(str::trim)
1409 .filter(|maybe_glob_str| !maybe_glob_str.is_empty())
1410 .map(str::to_owned)
1411 .collect::<Vec<_>>();
1412 Ok(PathMatcher::new(&queries, path_style)?)
1413 }
1414
1415 fn select_match(&mut self, direction: Direction, window: &mut Window, cx: &mut Context<Self>) {
1416 if let Some(index) = self.active_match_index {
1417 let match_ranges = self.entity.read(cx).match_ranges.clone();
1418
1419 if !EditorSettings::get_global(cx).search_wrap
1420 && ((direction == Direction::Next && index + 1 >= match_ranges.len())
1421 || (direction == Direction::Prev && index == 0))
1422 {
1423 crate::show_no_more_matches(window, cx);
1424 return;
1425 }
1426
1427 let new_index = self.results_editor.update(cx, |editor, cx| {
1428 editor.match_index_for_direction(&match_ranges, index, direction, 1, window, cx)
1429 });
1430
1431 let range_to_select = match_ranges[new_index].clone();
1432 self.results_editor.update(cx, |editor, cx| {
1433 let range_to_select = editor.range_for_match(&range_to_select);
1434 let autoscroll = if EditorSettings::get_global(cx).search.center_on_match {
1435 Autoscroll::center()
1436 } else {
1437 Autoscroll::fit()
1438 };
1439 editor.unfold_ranges(std::slice::from_ref(&range_to_select), false, true, cx);
1440 editor.change_selections(SelectionEffects::scroll(autoscroll), window, cx, |s| {
1441 s.select_ranges([range_to_select])
1442 });
1443 });
1444 }
1445 }
1446
1447 fn focus_query_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1448 self.query_editor.update(cx, |query_editor, cx| {
1449 query_editor.select_all(&SelectAll, window, cx);
1450 });
1451 let editor_handle = self.query_editor.focus_handle(cx);
1452 window.focus(&editor_handle);
1453 }
1454
1455 fn set_query(&mut self, query: &str, window: &mut Window, cx: &mut Context<Self>) {
1456 self.set_search_editor(SearchInputKind::Query, query, window, cx);
1457 if EditorSettings::get_global(cx).use_smartcase_search
1458 && !query.is_empty()
1459 && self.search_options.contains(SearchOptions::CASE_SENSITIVE)
1460 != contains_uppercase(query)
1461 {
1462 self.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx)
1463 }
1464 }
1465
1466 fn set_search_editor(
1467 &mut self,
1468 kind: SearchInputKind,
1469 text: &str,
1470 window: &mut Window,
1471 cx: &mut Context<Self>,
1472 ) {
1473 let editor = match kind {
1474 SearchInputKind::Query => &self.query_editor,
1475 SearchInputKind::Include => &self.included_files_editor,
1476
1477 SearchInputKind::Exclude => &self.excluded_files_editor,
1478 };
1479 editor.update(cx, |included_editor, cx| {
1480 included_editor.set_text(text, window, cx)
1481 });
1482 }
1483
1484 fn focus_results_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1485 self.query_editor.update(cx, |query_editor, cx| {
1486 let cursor = query_editor.selections.newest_anchor().head();
1487 query_editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1488 s.select_ranges([cursor..cursor])
1489 });
1490 });
1491 let results_handle = self.results_editor.focus_handle(cx);
1492 window.focus(&results_handle);
1493 }
1494
1495 fn entity_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1496 let match_ranges = self.entity.read(cx).match_ranges.clone();
1497
1498 if match_ranges.is_empty() {
1499 self.active_match_index = None;
1500 self.results_editor.update(cx, |editor, cx| {
1501 editor.clear_background_highlights::<Self>(cx);
1502 });
1503 } else {
1504 self.active_match_index = Some(0);
1505 self.update_match_index(cx);
1506 let prev_search_id = mem::replace(&mut self.search_id, self.entity.read(cx).search_id);
1507 let is_new_search = self.search_id != prev_search_id;
1508 self.results_editor.update(cx, |editor, cx| {
1509 if is_new_search {
1510 let range_to_select = match_ranges
1511 .first()
1512 .map(|range| editor.range_for_match(range));
1513 editor.change_selections(Default::default(), window, cx, |s| {
1514 s.select_ranges(range_to_select)
1515 });
1516 editor.scroll(Point::default(), Some(Axis::Vertical), window, cx);
1517 }
1518 editor.highlight_background::<Self>(
1519 &match_ranges,
1520 |theme| theme.colors().search_match_background,
1521 cx,
1522 );
1523 });
1524 if is_new_search && self.query_editor.focus_handle(cx).is_focused(window) {
1525 self.focus_results_editor(window, cx);
1526 }
1527 }
1528
1529 cx.emit(ViewEvent::UpdateTab);
1530 cx.notify();
1531 }
1532
1533 fn update_match_index(&mut self, cx: &mut Context<Self>) {
1534 let results_editor = self.results_editor.read(cx);
1535 let new_index = active_match_index(
1536 Direction::Next,
1537 &self.entity.read(cx).match_ranges,
1538 &results_editor.selections.newest_anchor().head(),
1539 &results_editor.buffer().read(cx).snapshot(cx),
1540 );
1541 if self.active_match_index != new_index {
1542 self.active_match_index = new_index;
1543 cx.notify();
1544 }
1545 }
1546
1547 pub fn has_matches(&self) -> bool {
1548 self.active_match_index.is_some()
1549 }
1550
1551 fn landing_text_minor(&self, cx: &App) -> impl IntoElement {
1552 let focus_handle = self.focus_handle.clone();
1553 v_flex()
1554 .gap_1()
1555 .child(
1556 Label::new("Hit enter to search. For more options:")
1557 .color(Color::Muted)
1558 .mb_2(),
1559 )
1560 .child(
1561 Button::new("filter-paths", "Include/exclude specific paths")
1562 .icon(IconName::Filter)
1563 .icon_position(IconPosition::Start)
1564 .icon_size(IconSize::Small)
1565 .key_binding(KeyBinding::for_action_in(&ToggleFilters, &focus_handle, cx))
1566 .on_click(|_event, window, cx| {
1567 window.dispatch_action(ToggleFilters.boxed_clone(), cx)
1568 }),
1569 )
1570 .child(
1571 Button::new("find-replace", "Find and replace")
1572 .icon(IconName::Replace)
1573 .icon_position(IconPosition::Start)
1574 .icon_size(IconSize::Small)
1575 .key_binding(KeyBinding::for_action_in(&ToggleReplace, &focus_handle, cx))
1576 .on_click(|_event, window, cx| {
1577 window.dispatch_action(ToggleReplace.boxed_clone(), cx)
1578 }),
1579 )
1580 .child(
1581 Button::new("regex", "Match with regex")
1582 .icon(IconName::Regex)
1583 .icon_position(IconPosition::Start)
1584 .icon_size(IconSize::Small)
1585 .key_binding(KeyBinding::for_action_in(&ToggleRegex, &focus_handle, cx))
1586 .on_click(|_event, window, cx| {
1587 window.dispatch_action(ToggleRegex.boxed_clone(), cx)
1588 }),
1589 )
1590 .child(
1591 Button::new("match-case", "Match case")
1592 .icon(IconName::CaseSensitive)
1593 .icon_position(IconPosition::Start)
1594 .icon_size(IconSize::Small)
1595 .key_binding(KeyBinding::for_action_in(
1596 &ToggleCaseSensitive,
1597 &focus_handle,
1598 cx,
1599 ))
1600 .on_click(|_event, window, cx| {
1601 window.dispatch_action(ToggleCaseSensitive.boxed_clone(), cx)
1602 }),
1603 )
1604 .child(
1605 Button::new("match-whole-words", "Match whole words")
1606 .icon(IconName::WholeWord)
1607 .icon_position(IconPosition::Start)
1608 .icon_size(IconSize::Small)
1609 .key_binding(KeyBinding::for_action_in(
1610 &ToggleWholeWord,
1611 &focus_handle,
1612 cx,
1613 ))
1614 .on_click(|_event, window, cx| {
1615 window.dispatch_action(ToggleWholeWord.boxed_clone(), cx)
1616 }),
1617 )
1618 }
1619
1620 fn border_color_for(&self, panel: InputPanel, cx: &App) -> Hsla {
1621 if self.panels_with_errors.contains_key(&panel) {
1622 Color::Error.color(cx)
1623 } else {
1624 cx.theme().colors().border
1625 }
1626 }
1627
1628 fn move_focus_to_results(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1629 if !self.results_editor.focus_handle(cx).is_focused(window)
1630 && !self.entity.read(cx).match_ranges.is_empty()
1631 {
1632 cx.stop_propagation();
1633 self.focus_results_editor(window, cx)
1634 }
1635 }
1636
1637 #[cfg(any(test, feature = "test-support"))]
1638 pub fn results_editor(&self) -> &Entity<Editor> {
1639 &self.results_editor
1640 }
1641
1642 fn adjust_query_regex_language(&self, cx: &mut App) {
1643 let enable = self.search_options.contains(SearchOptions::REGEX);
1644 let query_buffer = self
1645 .query_editor
1646 .read(cx)
1647 .buffer()
1648 .read(cx)
1649 .as_singleton()
1650 .expect("query editor should be backed by a singleton buffer");
1651 if enable {
1652 if let Some(regex_language) = self.regex_language.clone() {
1653 query_buffer.update(cx, |query_buffer, cx| {
1654 query_buffer.set_language(Some(regex_language), cx);
1655 })
1656 }
1657 } else {
1658 query_buffer.update(cx, |query_buffer, cx| {
1659 query_buffer.set_language(None, cx);
1660 })
1661 }
1662 }
1663}
1664
1665fn buffer_search_query(
1666 workspace: &mut Workspace,
1667 item: &dyn ItemHandle,
1668 cx: &mut Context<Workspace>,
1669) -> Option<String> {
1670 let buffer_search_bar = workspace
1671 .pane_for(item)
1672 .and_then(|pane| {
1673 pane.read(cx)
1674 .toolbar()
1675 .read(cx)
1676 .item_of_type::<BufferSearchBar>()
1677 })?
1678 .read(cx);
1679 if buffer_search_bar.query_editor_focused() {
1680 let buffer_search_query = buffer_search_bar.query(cx);
1681 if !buffer_search_query.is_empty() {
1682 return Some(buffer_search_query);
1683 }
1684 }
1685 None
1686}
1687
1688impl Default for ProjectSearchBar {
1689 fn default() -> Self {
1690 Self::new()
1691 }
1692}
1693
1694impl ProjectSearchBar {
1695 pub fn new() -> Self {
1696 Self {
1697 active_project_search: None,
1698 subscription: None,
1699 }
1700 }
1701
1702 fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
1703 if let Some(search_view) = self.active_project_search.as_ref() {
1704 search_view.update(cx, |search_view, cx| {
1705 if !search_view
1706 .replacement_editor
1707 .focus_handle(cx)
1708 .is_focused(window)
1709 {
1710 cx.stop_propagation();
1711 search_view
1712 .prompt_to_save_if_dirty_then_search(window, cx)
1713 .detach_and_log_err(cx);
1714 }
1715 });
1716 }
1717 }
1718
1719 fn tab(&mut self, _: &Tab, window: &mut Window, cx: &mut Context<Self>) {
1720 self.cycle_field(Direction::Next, window, cx);
1721 }
1722
1723 fn backtab(&mut self, _: &Backtab, window: &mut Window, cx: &mut Context<Self>) {
1724 self.cycle_field(Direction::Prev, window, cx);
1725 }
1726
1727 fn focus_search(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1728 if let Some(search_view) = self.active_project_search.as_ref() {
1729 search_view.update(cx, |search_view, cx| {
1730 search_view.query_editor.focus_handle(cx).focus(window);
1731 });
1732 }
1733 }
1734
1735 fn cycle_field(&mut self, direction: Direction, window: &mut Window, cx: &mut Context<Self>) {
1736 let active_project_search = match &self.active_project_search {
1737 Some(active_project_search) => active_project_search,
1738 None => return,
1739 };
1740
1741 active_project_search.update(cx, |project_view, cx| {
1742 let mut views = vec![project_view.query_editor.focus_handle(cx)];
1743 if project_view.replace_enabled {
1744 views.push(project_view.replacement_editor.focus_handle(cx));
1745 }
1746 if project_view.filters_enabled {
1747 views.extend([
1748 project_view.included_files_editor.focus_handle(cx),
1749 project_view.excluded_files_editor.focus_handle(cx),
1750 ]);
1751 }
1752 let current_index = match views.iter().position(|focus| focus.is_focused(window)) {
1753 Some(index) => index,
1754 None => return,
1755 };
1756
1757 let new_index = match direction {
1758 Direction::Next => (current_index + 1) % views.len(),
1759 Direction::Prev if current_index == 0 => views.len() - 1,
1760 Direction::Prev => (current_index - 1) % views.len(),
1761 };
1762 let next_focus_handle = &views[new_index];
1763 window.focus(next_focus_handle);
1764 cx.stop_propagation();
1765 });
1766 }
1767
1768 pub(crate) fn toggle_search_option(
1769 &mut self,
1770 option: SearchOptions,
1771 window: &mut Window,
1772 cx: &mut Context<Self>,
1773 ) -> bool {
1774 if self.active_project_search.is_none() {
1775 return false;
1776 }
1777
1778 cx.spawn_in(window, async move |this, cx| {
1779 let task = this.update_in(cx, |this, window, cx| {
1780 let search_view = this.active_project_search.as_ref()?;
1781 search_view.update(cx, |search_view, cx| {
1782 search_view.toggle_search_option(option, cx);
1783 search_view
1784 .entity
1785 .read(cx)
1786 .active_query
1787 .is_some()
1788 .then(|| search_view.prompt_to_save_if_dirty_then_search(window, cx))
1789 })
1790 })?;
1791 if let Some(task) = task {
1792 task.await?;
1793 }
1794 this.update(cx, |_, cx| {
1795 cx.notify();
1796 })?;
1797 anyhow::Ok(())
1798 })
1799 .detach();
1800 true
1801 }
1802
1803 fn toggle_replace(&mut self, _: &ToggleReplace, window: &mut Window, cx: &mut Context<Self>) {
1804 if let Some(search) = &self.active_project_search {
1805 search.update(cx, |this, cx| {
1806 this.replace_enabled = !this.replace_enabled;
1807 let editor_to_focus = if this.replace_enabled {
1808 this.replacement_editor.focus_handle(cx)
1809 } else {
1810 this.query_editor.focus_handle(cx)
1811 };
1812 window.focus(&editor_to_focus);
1813 cx.notify();
1814 });
1815 }
1816 }
1817
1818 fn toggle_filters(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
1819 if let Some(search_view) = self.active_project_search.as_ref() {
1820 search_view.update(cx, |search_view, cx| {
1821 search_view.toggle_filters(cx);
1822 search_view
1823 .included_files_editor
1824 .update(cx, |_, cx| cx.notify());
1825 search_view
1826 .excluded_files_editor
1827 .update(cx, |_, cx| cx.notify());
1828 window.refresh();
1829 cx.notify();
1830 });
1831 cx.notify();
1832 true
1833 } else {
1834 false
1835 }
1836 }
1837
1838 fn toggle_opened_only(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
1839 if self.active_project_search.is_none() {
1840 return false;
1841 }
1842
1843 cx.spawn_in(window, async move |this, cx| {
1844 let task = this.update_in(cx, |this, window, cx| {
1845 let search_view = this.active_project_search.as_ref()?;
1846 search_view.update(cx, |search_view, cx| {
1847 search_view.toggle_opened_only(window, cx);
1848 search_view
1849 .entity
1850 .read(cx)
1851 .active_query
1852 .is_some()
1853 .then(|| search_view.prompt_to_save_if_dirty_then_search(window, cx))
1854 })
1855 })?;
1856 if let Some(task) = task {
1857 task.await?;
1858 }
1859 this.update(cx, |_, cx| {
1860 cx.notify();
1861 })?;
1862 anyhow::Ok(())
1863 })
1864 .detach();
1865 true
1866 }
1867
1868 fn is_opened_only_enabled(&self, cx: &App) -> bool {
1869 if let Some(search_view) = self.active_project_search.as_ref() {
1870 search_view.read(cx).included_opened_only
1871 } else {
1872 false
1873 }
1874 }
1875
1876 fn move_focus_to_results(&self, window: &mut Window, cx: &mut Context<Self>) {
1877 if let Some(search_view) = self.active_project_search.as_ref() {
1878 search_view.update(cx, |search_view, cx| {
1879 search_view.move_focus_to_results(window, cx);
1880 });
1881 cx.notify();
1882 }
1883 }
1884
1885 fn next_history_query(
1886 &mut self,
1887 _: &NextHistoryQuery,
1888 window: &mut Window,
1889 cx: &mut Context<Self>,
1890 ) {
1891 if let Some(search_view) = self.active_project_search.as_ref() {
1892 search_view.update(cx, |search_view, cx| {
1893 for (editor, kind) in [
1894 (search_view.query_editor.clone(), SearchInputKind::Query),
1895 (
1896 search_view.included_files_editor.clone(),
1897 SearchInputKind::Include,
1898 ),
1899 (
1900 search_view.excluded_files_editor.clone(),
1901 SearchInputKind::Exclude,
1902 ),
1903 ] {
1904 if editor.focus_handle(cx).is_focused(window) {
1905 let new_query = search_view.entity.update(cx, |model, cx| {
1906 let project = model.project.clone();
1907
1908 if let Some(new_query) = project.update(cx, |project, _| {
1909 project
1910 .search_history_mut(kind)
1911 .next(model.cursor_mut(kind))
1912 .map(str::to_string)
1913 }) {
1914 new_query
1915 } else {
1916 model.cursor_mut(kind).reset();
1917 String::new()
1918 }
1919 });
1920 search_view.set_search_editor(kind, &new_query, window, cx);
1921 }
1922 }
1923 });
1924 }
1925 }
1926
1927 fn previous_history_query(
1928 &mut self,
1929 _: &PreviousHistoryQuery,
1930 window: &mut Window,
1931 cx: &mut Context<Self>,
1932 ) {
1933 if let Some(search_view) = self.active_project_search.as_ref() {
1934 search_view.update(cx, |search_view, cx| {
1935 for (editor, kind) in [
1936 (search_view.query_editor.clone(), SearchInputKind::Query),
1937 (
1938 search_view.included_files_editor.clone(),
1939 SearchInputKind::Include,
1940 ),
1941 (
1942 search_view.excluded_files_editor.clone(),
1943 SearchInputKind::Exclude,
1944 ),
1945 ] {
1946 if editor.focus_handle(cx).is_focused(window) {
1947 if editor.read(cx).text(cx).is_empty()
1948 && let Some(new_query) = search_view
1949 .entity
1950 .read(cx)
1951 .project
1952 .read(cx)
1953 .search_history(kind)
1954 .current(search_view.entity.read(cx).cursor(kind))
1955 .map(str::to_string)
1956 {
1957 search_view.set_search_editor(kind, &new_query, window, cx);
1958 return;
1959 }
1960
1961 if let Some(new_query) = search_view.entity.update(cx, |model, cx| {
1962 let project = model.project.clone();
1963 project.update(cx, |project, _| {
1964 project
1965 .search_history_mut(kind)
1966 .previous(model.cursor_mut(kind))
1967 .map(str::to_string)
1968 })
1969 }) {
1970 search_view.set_search_editor(kind, &new_query, window, cx);
1971 }
1972 }
1973 }
1974 });
1975 }
1976 }
1977
1978 fn select_next_match(
1979 &mut self,
1980 _: &SelectNextMatch,
1981 window: &mut Window,
1982 cx: &mut Context<Self>,
1983 ) {
1984 if let Some(search) = self.active_project_search.as_ref() {
1985 search.update(cx, |this, cx| {
1986 this.select_match(Direction::Next, window, cx);
1987 })
1988 }
1989 }
1990
1991 fn select_prev_match(
1992 &mut self,
1993 _: &SelectPreviousMatch,
1994 window: &mut Window,
1995 cx: &mut Context<Self>,
1996 ) {
1997 if let Some(search) = self.active_project_search.as_ref() {
1998 search.update(cx, |this, cx| {
1999 this.select_match(Direction::Prev, window, cx);
2000 })
2001 }
2002 }
2003}
2004
2005impl Render for ProjectSearchBar {
2006 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2007 let Some(search) = self.active_project_search.clone() else {
2008 return div();
2009 };
2010 let search = search.read(cx);
2011 let focus_handle = search.focus_handle(cx);
2012
2013 let container_width = window.viewport_size().width;
2014 let input_width = SearchInputWidth::calc_width(container_width);
2015
2016 let input_base_styles = |panel: InputPanel| {
2017 input_base_styles(search.border_color_for(panel, cx), |div| match panel {
2018 InputPanel::Query | InputPanel::Replacement => div.w(input_width),
2019 InputPanel::Include | InputPanel::Exclude => div.flex_grow(),
2020 })
2021 };
2022 let theme_colors = cx.theme().colors();
2023 let project_search = search.entity.read(cx);
2024 let limit_reached = project_search.limit_reached;
2025
2026 let color_override = match (
2027 &project_search.pending_search,
2028 project_search.no_results,
2029 &project_search.active_query,
2030 &project_search.last_search_query_text,
2031 ) {
2032 (None, Some(true), Some(q), Some(p)) if q.as_str() == p => Some(Color::Error),
2033 _ => None,
2034 };
2035
2036 let match_text = search
2037 .active_match_index
2038 .and_then(|index| {
2039 let index = index + 1;
2040 let match_quantity = project_search.match_ranges.len();
2041 if match_quantity > 0 {
2042 debug_assert!(match_quantity >= index);
2043 if limit_reached {
2044 Some(format!("{index}/{match_quantity}+"))
2045 } else {
2046 Some(format!("{index}/{match_quantity}"))
2047 }
2048 } else {
2049 None
2050 }
2051 })
2052 .unwrap_or_else(|| "0/0".to_string());
2053
2054 let query_focus = search.query_editor.focus_handle(cx);
2055
2056 let query_column = input_base_styles(InputPanel::Query)
2057 .on_action(cx.listener(|this, action, window, cx| this.confirm(action, window, cx)))
2058 .on_action(cx.listener(|this, action, window, cx| {
2059 this.previous_history_query(action, window, cx)
2060 }))
2061 .on_action(
2062 cx.listener(|this, action, window, cx| this.next_history_query(action, window, cx)),
2063 )
2064 .child(render_text_input(&search.query_editor, color_override, cx))
2065 .child(
2066 h_flex()
2067 .gap_1()
2068 .child(SearchOption::CaseSensitive.as_button(
2069 search.search_options,
2070 SearchSource::Project(cx),
2071 focus_handle.clone(),
2072 ))
2073 .child(SearchOption::WholeWord.as_button(
2074 search.search_options,
2075 SearchSource::Project(cx),
2076 focus_handle.clone(),
2077 ))
2078 .child(SearchOption::Regex.as_button(
2079 search.search_options,
2080 SearchSource::Project(cx),
2081 focus_handle.clone(),
2082 )),
2083 );
2084
2085 let matches_column = h_flex()
2086 .ml_1()
2087 .pl_1p5()
2088 .border_l_1()
2089 .border_color(theme_colors.border_variant)
2090 .child(render_action_button(
2091 "project-search-nav-button",
2092 IconName::ChevronLeft,
2093 search
2094 .active_match_index
2095 .is_none()
2096 .then_some(ActionButtonState::Disabled),
2097 "Select Previous Match",
2098 &SelectPreviousMatch,
2099 query_focus.clone(),
2100 ))
2101 .child(render_action_button(
2102 "project-search-nav-button",
2103 IconName::ChevronRight,
2104 search
2105 .active_match_index
2106 .is_none()
2107 .then_some(ActionButtonState::Disabled),
2108 "Select Next Match",
2109 &SelectNextMatch,
2110 query_focus,
2111 ))
2112 .child(
2113 div()
2114 .id("matches")
2115 .ml_2()
2116 .min_w(rems_from_px(40.))
2117 .child(Label::new(match_text).size(LabelSize::Small).color(
2118 if search.active_match_index.is_some() {
2119 Color::Default
2120 } else {
2121 Color::Disabled
2122 },
2123 ))
2124 .when(limit_reached, |el| {
2125 el.tooltip(Tooltip::text(
2126 "Search limits reached.\nTry narrowing your search.",
2127 ))
2128 }),
2129 );
2130
2131 let mode_column = h_flex()
2132 .gap_1()
2133 .min_w_64()
2134 .child(
2135 IconButton::new("project-search-filter-button", IconName::Filter)
2136 .shape(IconButtonShape::Square)
2137 .tooltip(|_window, cx| {
2138 Tooltip::for_action("Toggle Filters", &ToggleFilters, cx)
2139 })
2140 .on_click(cx.listener(|this, _, window, cx| {
2141 this.toggle_filters(window, cx);
2142 }))
2143 .toggle_state(
2144 self.active_project_search
2145 .as_ref()
2146 .map(|search| search.read(cx).filters_enabled)
2147 .unwrap_or_default(),
2148 )
2149 .tooltip({
2150 let focus_handle = focus_handle.clone();
2151 move |_window, cx| {
2152 Tooltip::for_action_in(
2153 "Toggle Filters",
2154 &ToggleFilters,
2155 &focus_handle,
2156 cx,
2157 )
2158 }
2159 }),
2160 )
2161 .child(render_action_button(
2162 "project-search",
2163 IconName::Replace,
2164 self.active_project_search
2165 .as_ref()
2166 .map(|search| search.read(cx).replace_enabled)
2167 .and_then(|enabled| enabled.then_some(ActionButtonState::Toggled)),
2168 "Toggle Replace",
2169 &ToggleReplace,
2170 focus_handle.clone(),
2171 ))
2172 .child(matches_column);
2173
2174 let search_line = h_flex()
2175 .w_full()
2176 .gap_2()
2177 .child(query_column)
2178 .child(mode_column);
2179
2180 let replace_line = search.replace_enabled.then(|| {
2181 let replace_column = input_base_styles(InputPanel::Replacement)
2182 .child(render_text_input(&search.replacement_editor, None, cx));
2183
2184 let focus_handle = search.replacement_editor.read(cx).focus_handle(cx);
2185
2186 let replace_actions = h_flex()
2187 .min_w_64()
2188 .gap_1()
2189 .child(render_action_button(
2190 "project-search-replace-button",
2191 IconName::ReplaceNext,
2192 Default::default(),
2193 "Replace Next Match",
2194 &ReplaceNext,
2195 focus_handle.clone(),
2196 ))
2197 .child(render_action_button(
2198 "project-search-replace-button",
2199 IconName::ReplaceAll,
2200 Default::default(),
2201 "Replace All Matches",
2202 &ReplaceAll,
2203 focus_handle,
2204 ));
2205
2206 h_flex()
2207 .w_full()
2208 .gap_2()
2209 .child(replace_column)
2210 .child(replace_actions)
2211 });
2212
2213 let filter_line = search.filters_enabled.then(|| {
2214 let include = input_base_styles(InputPanel::Include)
2215 .on_action(cx.listener(|this, action, window, cx| {
2216 this.previous_history_query(action, window, cx)
2217 }))
2218 .on_action(cx.listener(|this, action, window, cx| {
2219 this.next_history_query(action, window, cx)
2220 }))
2221 .child(render_text_input(&search.included_files_editor, None, cx));
2222 let exclude = input_base_styles(InputPanel::Exclude)
2223 .on_action(cx.listener(|this, action, window, cx| {
2224 this.previous_history_query(action, window, cx)
2225 }))
2226 .on_action(cx.listener(|this, action, window, cx| {
2227 this.next_history_query(action, window, cx)
2228 }))
2229 .child(render_text_input(&search.excluded_files_editor, None, cx));
2230 let mode_column = h_flex()
2231 .gap_1()
2232 .min_w_64()
2233 .child(
2234 IconButton::new("project-search-opened-only", IconName::FolderSearch)
2235 .shape(IconButtonShape::Square)
2236 .toggle_state(self.is_opened_only_enabled(cx))
2237 .tooltip(Tooltip::text("Only Search Open Files"))
2238 .on_click(cx.listener(|this, _, window, cx| {
2239 this.toggle_opened_only(window, cx);
2240 })),
2241 )
2242 .child(SearchOption::IncludeIgnored.as_button(
2243 search.search_options,
2244 SearchSource::Project(cx),
2245 focus_handle.clone(),
2246 ));
2247 h_flex()
2248 .w_full()
2249 .gap_2()
2250 .child(
2251 h_flex()
2252 .gap_2()
2253 .w(input_width)
2254 .child(include)
2255 .child(exclude),
2256 )
2257 .child(mode_column)
2258 });
2259
2260 let mut key_context = KeyContext::default();
2261 key_context.add("ProjectSearchBar");
2262 if search
2263 .replacement_editor
2264 .focus_handle(cx)
2265 .is_focused(window)
2266 {
2267 key_context.add("in_replace");
2268 }
2269
2270 let query_error_line = search
2271 .panels_with_errors
2272 .get(&InputPanel::Query)
2273 .map(|error| {
2274 Label::new(error)
2275 .size(LabelSize::Small)
2276 .color(Color::Error)
2277 .mt_neg_1()
2278 .ml_2()
2279 });
2280
2281 let filter_error_line = search
2282 .panels_with_errors
2283 .get(&InputPanel::Include)
2284 .or_else(|| search.panels_with_errors.get(&InputPanel::Exclude))
2285 .map(|error| {
2286 Label::new(error)
2287 .size(LabelSize::Small)
2288 .color(Color::Error)
2289 .mt_neg_1()
2290 .ml_2()
2291 });
2292
2293 v_flex()
2294 .gap_2()
2295 .py(px(1.0))
2296 .w_full()
2297 .key_context(key_context)
2298 .on_action(cx.listener(|this, _: &ToggleFocus, window, cx| {
2299 this.move_focus_to_results(window, cx)
2300 }))
2301 .on_action(cx.listener(|this, _: &ToggleFilters, window, cx| {
2302 this.toggle_filters(window, cx);
2303 }))
2304 .capture_action(cx.listener(Self::tab))
2305 .capture_action(cx.listener(Self::backtab))
2306 .on_action(cx.listener(|this, action, window, cx| this.confirm(action, window, cx)))
2307 .on_action(cx.listener(|this, action, window, cx| {
2308 this.toggle_replace(action, window, cx);
2309 }))
2310 .on_action(cx.listener(|this, _: &ToggleWholeWord, window, cx| {
2311 this.toggle_search_option(SearchOptions::WHOLE_WORD, window, cx);
2312 }))
2313 .on_action(cx.listener(|this, _: &ToggleCaseSensitive, window, cx| {
2314 this.toggle_search_option(SearchOptions::CASE_SENSITIVE, window, cx);
2315 }))
2316 .on_action(cx.listener(|this, action, window, cx| {
2317 if let Some(search) = this.active_project_search.as_ref() {
2318 search.update(cx, |this, cx| {
2319 this.replace_next(action, window, cx);
2320 })
2321 }
2322 }))
2323 .on_action(cx.listener(|this, action, window, cx| {
2324 if let Some(search) = this.active_project_search.as_ref() {
2325 search.update(cx, |this, cx| {
2326 this.replace_all(action, window, cx);
2327 })
2328 }
2329 }))
2330 .when(search.filters_enabled, |this| {
2331 this.on_action(cx.listener(|this, _: &ToggleIncludeIgnored, window, cx| {
2332 this.toggle_search_option(SearchOptions::INCLUDE_IGNORED, window, cx);
2333 }))
2334 })
2335 .on_action(cx.listener(Self::select_next_match))
2336 .on_action(cx.listener(Self::select_prev_match))
2337 .child(search_line)
2338 .children(query_error_line)
2339 .children(replace_line)
2340 .children(filter_line)
2341 .children(filter_error_line)
2342 }
2343}
2344
2345impl EventEmitter<ToolbarItemEvent> for ProjectSearchBar {}
2346
2347impl ToolbarItemView for ProjectSearchBar {
2348 fn set_active_pane_item(
2349 &mut self,
2350 active_pane_item: Option<&dyn ItemHandle>,
2351 _: &mut Window,
2352 cx: &mut Context<Self>,
2353 ) -> ToolbarItemLocation {
2354 cx.notify();
2355 self.subscription = None;
2356 self.active_project_search = None;
2357 if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
2358 self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
2359 self.active_project_search = Some(search);
2360 ToolbarItemLocation::PrimaryLeft {}
2361 } else {
2362 ToolbarItemLocation::Hidden
2363 }
2364 }
2365}
2366
2367fn register_workspace_action<A: Action>(
2368 workspace: &mut Workspace,
2369 callback: fn(&mut ProjectSearchBar, &A, &mut Window, &mut Context<ProjectSearchBar>),
2370) {
2371 workspace.register_action(move |workspace, action: &A, window, cx| {
2372 if workspace.has_active_modal(window, cx) && !workspace.hide_modal(window, cx) {
2373 cx.propagate();
2374 return;
2375 }
2376
2377 workspace.active_pane().update(cx, |pane, cx| {
2378 pane.toolbar().update(cx, move |workspace, cx| {
2379 if let Some(search_bar) = workspace.item_of_type::<ProjectSearchBar>() {
2380 search_bar.update(cx, move |search_bar, cx| {
2381 if search_bar.active_project_search.is_some() {
2382 callback(search_bar, action, window, cx);
2383 cx.notify();
2384 } else {
2385 cx.propagate();
2386 }
2387 });
2388 }
2389 });
2390 })
2391 });
2392}
2393
2394fn register_workspace_action_for_present_search<A: Action>(
2395 workspace: &mut Workspace,
2396 callback: fn(&mut Workspace, &A, &mut Window, &mut Context<Workspace>),
2397) {
2398 workspace.register_action(move |workspace, action: &A, window, cx| {
2399 if workspace.has_active_modal(window, cx) && !workspace.hide_modal(window, cx) {
2400 cx.propagate();
2401 return;
2402 }
2403
2404 let should_notify = workspace
2405 .active_pane()
2406 .read(cx)
2407 .toolbar()
2408 .read(cx)
2409 .item_of_type::<ProjectSearchBar>()
2410 .map(|search_bar| search_bar.read(cx).active_project_search.is_some())
2411 .unwrap_or(false);
2412 if should_notify {
2413 callback(workspace, action, window, cx);
2414 cx.notify();
2415 } else {
2416 cx.propagate();
2417 }
2418 });
2419}
2420
2421#[cfg(any(test, feature = "test-support"))]
2422pub fn perform_project_search(
2423 search_view: &Entity<ProjectSearchView>,
2424 text: impl Into<std::sync::Arc<str>>,
2425 cx: &mut gpui::VisualTestContext,
2426) {
2427 cx.run_until_parked();
2428 search_view.update_in(cx, |search_view, window, cx| {
2429 search_view.query_editor.update(cx, |query_editor, cx| {
2430 query_editor.set_text(text, window, cx)
2431 });
2432 search_view.search(cx);
2433 });
2434 cx.run_until_parked();
2435}
2436
2437#[cfg(test)]
2438pub mod tests {
2439 use std::{
2440 ops::Deref as _,
2441 path::PathBuf,
2442 sync::{
2443 Arc,
2444 atomic::{self, AtomicUsize},
2445 },
2446 time::Duration,
2447 };
2448
2449 use super::*;
2450 use editor::{DisplayPoint, display_map::DisplayRow};
2451 use gpui::{Action, TestAppContext, VisualTestContext, WindowHandle};
2452 use language::{FakeLspAdapter, rust_lang};
2453 use project::FakeFs;
2454 use serde_json::json;
2455 use settings::{InlayHintSettingsContent, SettingsStore};
2456 use util::{path, paths::PathStyle, rel_path::rel_path};
2457 use util_macros::perf;
2458 use workspace::DeploySearch;
2459
2460 #[perf]
2461 #[gpui::test]
2462 async fn test_project_search(cx: &mut TestAppContext) {
2463 init_test(cx);
2464
2465 let fs = FakeFs::new(cx.background_executor.clone());
2466 fs.insert_tree(
2467 path!("/dir"),
2468 json!({
2469 "one.rs": "const ONE: usize = 1;",
2470 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2471 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2472 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2473 }),
2474 )
2475 .await;
2476 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
2477 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
2478 let workspace = window.root(cx).unwrap();
2479 let search = cx.new(|cx| ProjectSearch::new(project.clone(), cx));
2480 let search_view = cx.add_window(|window, cx| {
2481 ProjectSearchView::new(workspace.downgrade(), search.clone(), window, cx, None)
2482 });
2483
2484 perform_search(search_view, "TWO", cx);
2485 search_view.update(cx, |search_view, window, cx| {
2486 assert_eq!(
2487 search_view
2488 .results_editor
2489 .update(cx, |editor, cx| editor.display_text(cx)),
2490 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
2491 );
2492 let match_background_color = cx.theme().colors().search_match_background;
2493 let selection_background_color = cx.theme().colors().editor_document_highlight_bracket_background;
2494 assert_eq!(
2495 search_view
2496 .results_editor
2497 .update(cx, |editor, cx| editor.all_text_background_highlights(window, cx)),
2498 &[
2499 (
2500 DisplayPoint::new(DisplayRow(2), 32)..DisplayPoint::new(DisplayRow(2), 35),
2501 match_background_color
2502 ),
2503 (
2504 DisplayPoint::new(DisplayRow(2), 37)..DisplayPoint::new(DisplayRow(2), 40),
2505 selection_background_color
2506 ),
2507 (
2508 DisplayPoint::new(DisplayRow(2), 37)..DisplayPoint::new(DisplayRow(2), 40),
2509 match_background_color
2510 ),
2511 (
2512 DisplayPoint::new(DisplayRow(5), 6)..DisplayPoint::new(DisplayRow(5), 9),
2513 selection_background_color
2514 ),
2515 (
2516 DisplayPoint::new(DisplayRow(5), 6)..DisplayPoint::new(DisplayRow(5), 9),
2517 match_background_color
2518 ),
2519
2520 ]
2521 );
2522 assert_eq!(search_view.active_match_index, Some(0));
2523 assert_eq!(
2524 search_view
2525 .results_editor
2526 .update(cx, |editor, cx| editor.selections.display_ranges(&editor.display_snapshot(cx))),
2527 [DisplayPoint::new(DisplayRow(2), 32)..DisplayPoint::new(DisplayRow(2), 35)]
2528 );
2529
2530 search_view.select_match(Direction::Next, window, cx);
2531 }).unwrap();
2532
2533 search_view
2534 .update(cx, |search_view, window, cx| {
2535 assert_eq!(search_view.active_match_index, Some(1));
2536 assert_eq!(
2537 search_view.results_editor.update(cx, |editor, cx| editor
2538 .selections
2539 .display_ranges(&editor.display_snapshot(cx))),
2540 [DisplayPoint::new(DisplayRow(2), 37)..DisplayPoint::new(DisplayRow(2), 40)]
2541 );
2542 search_view.select_match(Direction::Next, window, cx);
2543 })
2544 .unwrap();
2545
2546 search_view
2547 .update(cx, |search_view, window, cx| {
2548 assert_eq!(search_view.active_match_index, Some(2));
2549 assert_eq!(
2550 search_view.results_editor.update(cx, |editor, cx| editor
2551 .selections
2552 .display_ranges(&editor.display_snapshot(cx))),
2553 [DisplayPoint::new(DisplayRow(5), 6)..DisplayPoint::new(DisplayRow(5), 9)]
2554 );
2555 search_view.select_match(Direction::Next, window, cx);
2556 })
2557 .unwrap();
2558
2559 search_view
2560 .update(cx, |search_view, window, cx| {
2561 assert_eq!(search_view.active_match_index, Some(0));
2562 assert_eq!(
2563 search_view.results_editor.update(cx, |editor, cx| editor
2564 .selections
2565 .display_ranges(&editor.display_snapshot(cx))),
2566 [DisplayPoint::new(DisplayRow(2), 32)..DisplayPoint::new(DisplayRow(2), 35)]
2567 );
2568 search_view.select_match(Direction::Prev, window, cx);
2569 })
2570 .unwrap();
2571
2572 search_view
2573 .update(cx, |search_view, window, cx| {
2574 assert_eq!(search_view.active_match_index, Some(2));
2575 assert_eq!(
2576 search_view.results_editor.update(cx, |editor, cx| editor
2577 .selections
2578 .display_ranges(&editor.display_snapshot(cx))),
2579 [DisplayPoint::new(DisplayRow(5), 6)..DisplayPoint::new(DisplayRow(5), 9)]
2580 );
2581 search_view.select_match(Direction::Prev, window, cx);
2582 })
2583 .unwrap();
2584
2585 search_view
2586 .update(cx, |search_view, _, cx| {
2587 assert_eq!(search_view.active_match_index, Some(1));
2588 assert_eq!(
2589 search_view.results_editor.update(cx, |editor, cx| editor
2590 .selections
2591 .display_ranges(&editor.display_snapshot(cx))),
2592 [DisplayPoint::new(DisplayRow(2), 37)..DisplayPoint::new(DisplayRow(2), 40)]
2593 );
2594 })
2595 .unwrap();
2596 }
2597
2598 #[perf]
2599 #[gpui::test]
2600 async fn test_deploy_project_search_focus(cx: &mut TestAppContext) {
2601 init_test(cx);
2602
2603 let fs = FakeFs::new(cx.background_executor.clone());
2604 fs.insert_tree(
2605 "/dir",
2606 json!({
2607 "one.rs": "const ONE: usize = 1;",
2608 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2609 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2610 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2611 }),
2612 )
2613 .await;
2614 let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
2615 let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
2616 let workspace = window;
2617 let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new());
2618
2619 let active_item = cx.read(|cx| {
2620 workspace
2621 .read(cx)
2622 .unwrap()
2623 .active_pane()
2624 .read(cx)
2625 .active_item()
2626 .and_then(|item| item.downcast::<ProjectSearchView>())
2627 });
2628 assert!(
2629 active_item.is_none(),
2630 "Expected no search panel to be active"
2631 );
2632
2633 window
2634 .update(cx, move |workspace, window, cx| {
2635 assert_eq!(workspace.panes().len(), 1);
2636 workspace.panes()[0].update(cx, |pane, cx| {
2637 pane.toolbar()
2638 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
2639 });
2640
2641 ProjectSearchView::deploy_search(
2642 workspace,
2643 &workspace::DeploySearch::find(),
2644 window,
2645 cx,
2646 )
2647 })
2648 .unwrap();
2649
2650 let Some(search_view) = cx.read(|cx| {
2651 workspace
2652 .read(cx)
2653 .unwrap()
2654 .active_pane()
2655 .read(cx)
2656 .active_item()
2657 .and_then(|item| item.downcast::<ProjectSearchView>())
2658 }) else {
2659 panic!("Search view expected to appear after new search event trigger")
2660 };
2661
2662 cx.spawn(|mut cx| async move {
2663 window
2664 .update(&mut cx, |_, window, cx| {
2665 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
2666 })
2667 .unwrap();
2668 })
2669 .detach();
2670 cx.background_executor.run_until_parked();
2671 window
2672 .update(cx, |_, window, cx| {
2673 search_view.update(cx, |search_view, cx| {
2674 assert!(
2675 search_view.query_editor.focus_handle(cx).is_focused(window),
2676 "Empty search view should be focused after the toggle focus event: no results panel to focus on",
2677 );
2678 });
2679 }).unwrap();
2680
2681 window
2682 .update(cx, |_, window, cx| {
2683 search_view.update(cx, |search_view, cx| {
2684 let query_editor = &search_view.query_editor;
2685 assert!(
2686 query_editor.focus_handle(cx).is_focused(window),
2687 "Search view should be focused after the new search view is activated",
2688 );
2689 let query_text = query_editor.read(cx).text(cx);
2690 assert!(
2691 query_text.is_empty(),
2692 "New search query should be empty but got '{query_text}'",
2693 );
2694 let results_text = search_view
2695 .results_editor
2696 .update(cx, |editor, cx| editor.display_text(cx));
2697 assert!(
2698 results_text.is_empty(),
2699 "Empty search view should have no results but got '{results_text}'"
2700 );
2701 });
2702 })
2703 .unwrap();
2704
2705 window
2706 .update(cx, |_, window, cx| {
2707 search_view.update(cx, |search_view, cx| {
2708 search_view.query_editor.update(cx, |query_editor, cx| {
2709 query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", window, cx)
2710 });
2711 search_view.search(cx);
2712 });
2713 })
2714 .unwrap();
2715 cx.background_executor.run_until_parked();
2716 window
2717 .update(cx, |_, window, cx| {
2718 search_view.update(cx, |search_view, cx| {
2719 let results_text = search_view
2720 .results_editor
2721 .update(cx, |editor, cx| editor.display_text(cx));
2722 assert!(
2723 results_text.is_empty(),
2724 "Search view for mismatching query should have no results but got '{results_text}'"
2725 );
2726 assert!(
2727 search_view.query_editor.focus_handle(cx).is_focused(window),
2728 "Search view should be focused after mismatching query had been used in search",
2729 );
2730 });
2731 }).unwrap();
2732
2733 cx.spawn(|mut cx| async move {
2734 window.update(&mut cx, |_, window, cx| {
2735 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
2736 })
2737 })
2738 .detach();
2739 cx.background_executor.run_until_parked();
2740 window.update(cx, |_, window, cx| {
2741 search_view.update(cx, |search_view, cx| {
2742 assert!(
2743 search_view.query_editor.focus_handle(cx).is_focused(window),
2744 "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on",
2745 );
2746 });
2747 }).unwrap();
2748
2749 window
2750 .update(cx, |_, window, cx| {
2751 search_view.update(cx, |search_view, cx| {
2752 search_view.query_editor.update(cx, |query_editor, cx| {
2753 query_editor.set_text("TWO", window, cx)
2754 });
2755 search_view.search(cx);
2756 });
2757 })
2758 .unwrap();
2759 cx.background_executor.run_until_parked();
2760 window.update(cx, |_, window, cx| {
2761 search_view.update(cx, |search_view, cx| {
2762 assert_eq!(
2763 search_view
2764 .results_editor
2765 .update(cx, |editor, cx| editor.display_text(cx)),
2766 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
2767 "Search view results should match the query"
2768 );
2769 assert!(
2770 search_view.results_editor.focus_handle(cx).is_focused(window),
2771 "Search view with mismatching query should be focused after search results are available",
2772 );
2773 });
2774 }).unwrap();
2775 cx.spawn(|mut cx| async move {
2776 window
2777 .update(&mut cx, |_, window, cx| {
2778 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
2779 })
2780 .unwrap();
2781 })
2782 .detach();
2783 cx.background_executor.run_until_parked();
2784 window.update(cx, |_, window, cx| {
2785 search_view.update(cx, |search_view, cx| {
2786 assert!(
2787 search_view.results_editor.focus_handle(cx).is_focused(window),
2788 "Search view with matching query should still have its results editor focused after the toggle focus event",
2789 );
2790 });
2791 }).unwrap();
2792
2793 workspace
2794 .update(cx, |workspace, window, cx| {
2795 ProjectSearchView::deploy_search(
2796 workspace,
2797 &workspace::DeploySearch::find(),
2798 window,
2799 cx,
2800 )
2801 })
2802 .unwrap();
2803 window.update(cx, |_, window, cx| {
2804 search_view.update(cx, |search_view, cx| {
2805 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");
2806 assert_eq!(
2807 search_view
2808 .results_editor
2809 .update(cx, |editor, cx| editor.display_text(cx)),
2810 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
2811 "Results should be unchanged after search view 2nd open in a row"
2812 );
2813 assert!(
2814 search_view.query_editor.focus_handle(cx).is_focused(window),
2815 "Focus should be moved into query editor again after search view 2nd open in a row"
2816 );
2817 });
2818 }).unwrap();
2819
2820 cx.spawn(|mut cx| async move {
2821 window
2822 .update(&mut cx, |_, window, cx| {
2823 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
2824 })
2825 .unwrap();
2826 })
2827 .detach();
2828 cx.background_executor.run_until_parked();
2829 window.update(cx, |_, window, cx| {
2830 search_view.update(cx, |search_view, cx| {
2831 assert!(
2832 search_view.results_editor.focus_handle(cx).is_focused(window),
2833 "Search view with matching query should switch focus to the results editor after the toggle focus event",
2834 );
2835 });
2836 }).unwrap();
2837 }
2838
2839 #[perf]
2840 #[gpui::test]
2841 async fn test_filters_consider_toggle_state(cx: &mut TestAppContext) {
2842 init_test(cx);
2843
2844 let fs = FakeFs::new(cx.background_executor.clone());
2845 fs.insert_tree(
2846 "/dir",
2847 json!({
2848 "one.rs": "const ONE: usize = 1;",
2849 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2850 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2851 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2852 }),
2853 )
2854 .await;
2855 let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
2856 let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
2857 let workspace = window;
2858 let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new());
2859
2860 window
2861 .update(cx, move |workspace, window, cx| {
2862 workspace.panes()[0].update(cx, |pane, cx| {
2863 pane.toolbar()
2864 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
2865 });
2866
2867 ProjectSearchView::deploy_search(
2868 workspace,
2869 &workspace::DeploySearch::find(),
2870 window,
2871 cx,
2872 )
2873 })
2874 .unwrap();
2875
2876 let Some(search_view) = cx.read(|cx| {
2877 workspace
2878 .read(cx)
2879 .unwrap()
2880 .active_pane()
2881 .read(cx)
2882 .active_item()
2883 .and_then(|item| item.downcast::<ProjectSearchView>())
2884 }) else {
2885 panic!("Search view expected to appear after new search event trigger")
2886 };
2887
2888 cx.spawn(|mut cx| async move {
2889 window
2890 .update(&mut cx, |_, window, cx| {
2891 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
2892 })
2893 .unwrap();
2894 })
2895 .detach();
2896 cx.background_executor.run_until_parked();
2897
2898 window
2899 .update(cx, |_, window, cx| {
2900 search_view.update(cx, |search_view, cx| {
2901 search_view.query_editor.update(cx, |query_editor, cx| {
2902 query_editor.set_text("const FOUR", window, cx)
2903 });
2904 search_view.toggle_filters(cx);
2905 search_view
2906 .excluded_files_editor
2907 .update(cx, |exclude_editor, cx| {
2908 exclude_editor.set_text("four.rs", window, cx)
2909 });
2910 search_view.search(cx);
2911 });
2912 })
2913 .unwrap();
2914 cx.background_executor.run_until_parked();
2915 window
2916 .update(cx, |_, _, cx| {
2917 search_view.update(cx, |search_view, cx| {
2918 let results_text = search_view
2919 .results_editor
2920 .update(cx, |editor, cx| editor.display_text(cx));
2921 assert!(
2922 results_text.is_empty(),
2923 "Search view for query with the only match in an excluded file should have no results but got '{results_text}'"
2924 );
2925 });
2926 }).unwrap();
2927
2928 cx.spawn(|mut cx| async move {
2929 window.update(&mut cx, |_, window, cx| {
2930 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
2931 })
2932 })
2933 .detach();
2934 cx.background_executor.run_until_parked();
2935
2936 window
2937 .update(cx, |_, _, cx| {
2938 search_view.update(cx, |search_view, cx| {
2939 search_view.toggle_filters(cx);
2940 search_view.search(cx);
2941 });
2942 })
2943 .unwrap();
2944 cx.background_executor.run_until_parked();
2945 window
2946 .update(cx, |_, _, cx| {
2947 search_view.update(cx, |search_view, cx| {
2948 assert_eq!(
2949 search_view
2950 .results_editor
2951 .update(cx, |editor, cx| editor.display_text(cx)),
2952 "\n\nconst FOUR: usize = one::ONE + three::THREE;",
2953 "Search view results should contain the queried result in the previously excluded file with filters toggled off"
2954 );
2955 });
2956 })
2957 .unwrap();
2958 }
2959
2960 #[perf]
2961 #[gpui::test]
2962 async fn test_new_project_search_focus(cx: &mut TestAppContext) {
2963 init_test(cx);
2964
2965 let fs = FakeFs::new(cx.background_executor.clone());
2966 fs.insert_tree(
2967 path!("/dir"),
2968 json!({
2969 "one.rs": "const ONE: usize = 1;",
2970 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
2971 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
2972 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
2973 }),
2974 )
2975 .await;
2976 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
2977 let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
2978 let workspace = window;
2979 let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new());
2980
2981 let active_item = cx.read(|cx| {
2982 workspace
2983 .read(cx)
2984 .unwrap()
2985 .active_pane()
2986 .read(cx)
2987 .active_item()
2988 .and_then(|item| item.downcast::<ProjectSearchView>())
2989 });
2990 assert!(
2991 active_item.is_none(),
2992 "Expected no search panel to be active"
2993 );
2994
2995 window
2996 .update(cx, move |workspace, window, cx| {
2997 assert_eq!(workspace.panes().len(), 1);
2998 workspace.panes()[0].update(cx, |pane, cx| {
2999 pane.toolbar()
3000 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
3001 });
3002
3003 ProjectSearchView::new_search(workspace, &workspace::NewSearch, window, cx)
3004 })
3005 .unwrap();
3006
3007 let Some(search_view) = cx.read(|cx| {
3008 workspace
3009 .read(cx)
3010 .unwrap()
3011 .active_pane()
3012 .read(cx)
3013 .active_item()
3014 .and_then(|item| item.downcast::<ProjectSearchView>())
3015 }) else {
3016 panic!("Search view expected to appear after new search event trigger")
3017 };
3018
3019 cx.spawn(|mut cx| async move {
3020 window
3021 .update(&mut cx, |_, window, cx| {
3022 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
3023 })
3024 .unwrap();
3025 })
3026 .detach();
3027 cx.background_executor.run_until_parked();
3028
3029 window.update(cx, |_, window, cx| {
3030 search_view.update(cx, |search_view, cx| {
3031 assert!(
3032 search_view.query_editor.focus_handle(cx).is_focused(window),
3033 "Empty search view should be focused after the toggle focus event: no results panel to focus on",
3034 );
3035 });
3036 }).unwrap();
3037
3038 window
3039 .update(cx, |_, window, cx| {
3040 search_view.update(cx, |search_view, cx| {
3041 let query_editor = &search_view.query_editor;
3042 assert!(
3043 query_editor.focus_handle(cx).is_focused(window),
3044 "Search view should be focused after the new search view is activated",
3045 );
3046 let query_text = query_editor.read(cx).text(cx);
3047 assert!(
3048 query_text.is_empty(),
3049 "New search query should be empty but got '{query_text}'",
3050 );
3051 let results_text = search_view
3052 .results_editor
3053 .update(cx, |editor, cx| editor.display_text(cx));
3054 assert!(
3055 results_text.is_empty(),
3056 "Empty search view should have no results but got '{results_text}'"
3057 );
3058 });
3059 })
3060 .unwrap();
3061
3062 window
3063 .update(cx, |_, window, cx| {
3064 search_view.update(cx, |search_view, cx| {
3065 search_view.query_editor.update(cx, |query_editor, cx| {
3066 query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", window, cx)
3067 });
3068 search_view.search(cx);
3069 });
3070 })
3071 .unwrap();
3072
3073 cx.background_executor.run_until_parked();
3074 window
3075 .update(cx, |_, window, cx| {
3076 search_view.update(cx, |search_view, cx| {
3077 let results_text = search_view
3078 .results_editor
3079 .update(cx, |editor, cx| editor.display_text(cx));
3080 assert!(
3081 results_text.is_empty(),
3082 "Search view for mismatching query should have no results but got '{results_text}'"
3083 );
3084 assert!(
3085 search_view.query_editor.focus_handle(cx).is_focused(window),
3086 "Search view should be focused after mismatching query had been used in search",
3087 );
3088 });
3089 })
3090 .unwrap();
3091 cx.spawn(|mut cx| async move {
3092 window.update(&mut cx, |_, window, cx| {
3093 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
3094 })
3095 })
3096 .detach();
3097 cx.background_executor.run_until_parked();
3098 window.update(cx, |_, window, cx| {
3099 search_view.update(cx, |search_view, cx| {
3100 assert!(
3101 search_view.query_editor.focus_handle(cx).is_focused(window),
3102 "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on",
3103 );
3104 });
3105 }).unwrap();
3106
3107 window
3108 .update(cx, |_, window, cx| {
3109 search_view.update(cx, |search_view, cx| {
3110 search_view.query_editor.update(cx, |query_editor, cx| {
3111 query_editor.set_text("TWO", window, cx)
3112 });
3113 search_view.search(cx);
3114 })
3115 })
3116 .unwrap();
3117 cx.background_executor.run_until_parked();
3118 window.update(cx, |_, window, cx|
3119 search_view.update(cx, |search_view, cx| {
3120 assert_eq!(
3121 search_view
3122 .results_editor
3123 .update(cx, |editor, cx| editor.display_text(cx)),
3124 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
3125 "Search view results should match the query"
3126 );
3127 assert!(
3128 search_view.results_editor.focus_handle(cx).is_focused(window),
3129 "Search view with mismatching query should be focused after search results are available",
3130 );
3131 })).unwrap();
3132 cx.spawn(|mut cx| async move {
3133 window
3134 .update(&mut cx, |_, window, cx| {
3135 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
3136 })
3137 .unwrap();
3138 })
3139 .detach();
3140 cx.background_executor.run_until_parked();
3141 window.update(cx, |_, window, cx| {
3142 search_view.update(cx, |search_view, cx| {
3143 assert!(
3144 search_view.results_editor.focus_handle(cx).is_focused(window),
3145 "Search view with matching query should still have its results editor focused after the toggle focus event",
3146 );
3147 });
3148 }).unwrap();
3149
3150 workspace
3151 .update(cx, |workspace, window, cx| {
3152 ProjectSearchView::new_search(workspace, &workspace::NewSearch, window, cx)
3153 })
3154 .unwrap();
3155 cx.background_executor.run_until_parked();
3156 let Some(search_view_2) = cx.read(|cx| {
3157 workspace
3158 .read(cx)
3159 .unwrap()
3160 .active_pane()
3161 .read(cx)
3162 .active_item()
3163 .and_then(|item| item.downcast::<ProjectSearchView>())
3164 }) else {
3165 panic!("Search view expected to appear after new search event trigger")
3166 };
3167 assert!(
3168 search_view_2 != search_view,
3169 "New search view should be open after `workspace::NewSearch` event"
3170 );
3171
3172 window.update(cx, |_, window, cx| {
3173 search_view.update(cx, |search_view, cx| {
3174 assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO", "First search view should not have an updated query");
3175 assert_eq!(
3176 search_view
3177 .results_editor
3178 .update(cx, |editor, cx| editor.display_text(cx)),
3179 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
3180 "Results of the first search view should not update too"
3181 );
3182 assert!(
3183 !search_view.query_editor.focus_handle(cx).is_focused(window),
3184 "Focus should be moved away from the first search view"
3185 );
3186 });
3187 }).unwrap();
3188
3189 window.update(cx, |_, window, cx| {
3190 search_view_2.update(cx, |search_view_2, cx| {
3191 assert_eq!(
3192 search_view_2.query_editor.read(cx).text(cx),
3193 "two",
3194 "New search view should get the query from the text cursor was at during the event spawn (first search view's first result)"
3195 );
3196 assert_eq!(
3197 search_view_2
3198 .results_editor
3199 .update(cx, |editor, cx| editor.display_text(cx)),
3200 "",
3201 "No search results should be in the 2nd view yet, as we did not spawn a search for it"
3202 );
3203 assert!(
3204 search_view_2.query_editor.focus_handle(cx).is_focused(window),
3205 "Focus should be moved into query editor of the new window"
3206 );
3207 });
3208 }).unwrap();
3209
3210 window
3211 .update(cx, |_, window, cx| {
3212 search_view_2.update(cx, |search_view_2, cx| {
3213 search_view_2.query_editor.update(cx, |query_editor, cx| {
3214 query_editor.set_text("FOUR", window, cx)
3215 });
3216 search_view_2.search(cx);
3217 });
3218 })
3219 .unwrap();
3220
3221 cx.background_executor.run_until_parked();
3222 window.update(cx, |_, window, cx| {
3223 search_view_2.update(cx, |search_view_2, cx| {
3224 assert_eq!(
3225 search_view_2
3226 .results_editor
3227 .update(cx, |editor, cx| editor.display_text(cx)),
3228 "\n\nconst FOUR: usize = one::ONE + three::THREE;",
3229 "New search view with the updated query should have new search results"
3230 );
3231 assert!(
3232 search_view_2.results_editor.focus_handle(cx).is_focused(window),
3233 "Search view with mismatching query should be focused after search results are available",
3234 );
3235 });
3236 }).unwrap();
3237
3238 cx.spawn(|mut cx| async move {
3239 window
3240 .update(&mut cx, |_, window, cx| {
3241 window.dispatch_action(ToggleFocus.boxed_clone(), cx)
3242 })
3243 .unwrap();
3244 })
3245 .detach();
3246 cx.background_executor.run_until_parked();
3247 window.update(cx, |_, window, cx| {
3248 search_view_2.update(cx, |search_view_2, cx| {
3249 assert!(
3250 search_view_2.results_editor.focus_handle(cx).is_focused(window),
3251 "Search view with matching query should switch focus to the results editor after the toggle focus event",
3252 );
3253 });}).unwrap();
3254 }
3255
3256 #[perf]
3257 #[gpui::test]
3258 async fn test_new_project_search_in_directory(cx: &mut TestAppContext) {
3259 init_test(cx);
3260
3261 let fs = FakeFs::new(cx.background_executor.clone());
3262 fs.insert_tree(
3263 path!("/dir"),
3264 json!({
3265 "a": {
3266 "one.rs": "const ONE: usize = 1;",
3267 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
3268 },
3269 "b": {
3270 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
3271 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
3272 },
3273 }),
3274 )
3275 .await;
3276 let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
3277 let worktree_id = project.read_with(cx, |project, cx| {
3278 project.worktrees(cx).next().unwrap().read(cx).id()
3279 });
3280 let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
3281 let workspace = window.root(cx).unwrap();
3282 let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new());
3283
3284 let active_item = cx.read(|cx| {
3285 workspace
3286 .read(cx)
3287 .active_pane()
3288 .read(cx)
3289 .active_item()
3290 .and_then(|item| item.downcast::<ProjectSearchView>())
3291 });
3292 assert!(
3293 active_item.is_none(),
3294 "Expected no search panel to be active"
3295 );
3296
3297 window
3298 .update(cx, move |workspace, window, cx| {
3299 assert_eq!(workspace.panes().len(), 1);
3300 workspace.panes()[0].update(cx, move |pane, cx| {
3301 pane.toolbar()
3302 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
3303 });
3304 })
3305 .unwrap();
3306
3307 let a_dir_entry = cx.update(|cx| {
3308 workspace
3309 .read(cx)
3310 .project()
3311 .read(cx)
3312 .entry_for_path(&(worktree_id, rel_path("a")).into(), cx)
3313 .expect("no entry for /a/ directory")
3314 .clone()
3315 });
3316 assert!(a_dir_entry.is_dir());
3317 window
3318 .update(cx, |workspace, window, cx| {
3319 ProjectSearchView::new_search_in_directory(workspace, &a_dir_entry.path, window, cx)
3320 })
3321 .unwrap();
3322
3323 let Some(search_view) = cx.read(|cx| {
3324 workspace
3325 .read(cx)
3326 .active_pane()
3327 .read(cx)
3328 .active_item()
3329 .and_then(|item| item.downcast::<ProjectSearchView>())
3330 }) else {
3331 panic!("Search view expected to appear after new search in directory event trigger")
3332 };
3333 cx.background_executor.run_until_parked();
3334 window
3335 .update(cx, |_, window, cx| {
3336 search_view.update(cx, |search_view, cx| {
3337 assert!(
3338 search_view.query_editor.focus_handle(cx).is_focused(window),
3339 "On new search in directory, focus should be moved into query editor"
3340 );
3341 search_view.excluded_files_editor.update(cx, |editor, cx| {
3342 assert!(
3343 editor.display_text(cx).is_empty(),
3344 "New search in directory should not have any excluded files"
3345 );
3346 });
3347 search_view.included_files_editor.update(cx, |editor, cx| {
3348 assert_eq!(
3349 editor.display_text(cx),
3350 a_dir_entry.path.display(PathStyle::local()),
3351 "New search in directory should have included dir entry path"
3352 );
3353 });
3354 });
3355 })
3356 .unwrap();
3357 window
3358 .update(cx, |_, window, cx| {
3359 search_view.update(cx, |search_view, cx| {
3360 search_view.query_editor.update(cx, |query_editor, cx| {
3361 query_editor.set_text("const", window, cx)
3362 });
3363 search_view.search(cx);
3364 });
3365 })
3366 .unwrap();
3367 cx.background_executor.run_until_parked();
3368 window
3369 .update(cx, |_, _, cx| {
3370 search_view.update(cx, |search_view, cx| {
3371 assert_eq!(
3372 search_view
3373 .results_editor
3374 .update(cx, |editor, cx| editor.display_text(cx)),
3375 "\n\nconst ONE: usize = 1;\n\n\nconst TWO: usize = one::ONE + one::ONE;",
3376 "New search in directory should have a filter that matches a certain directory"
3377 );
3378 })
3379 })
3380 .unwrap();
3381 }
3382
3383 #[perf]
3384 #[gpui::test]
3385 async fn test_search_query_history(cx: &mut TestAppContext) {
3386 init_test(cx);
3387
3388 let fs = FakeFs::new(cx.background_executor.clone());
3389 fs.insert_tree(
3390 path!("/dir"),
3391 json!({
3392 "one.rs": "const ONE: usize = 1;",
3393 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
3394 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
3395 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
3396 }),
3397 )
3398 .await;
3399 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
3400 let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
3401 let workspace = window.root(cx).unwrap();
3402 let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new());
3403
3404 window
3405 .update(cx, {
3406 let search_bar = search_bar.clone();
3407 |workspace, window, cx| {
3408 assert_eq!(workspace.panes().len(), 1);
3409 workspace.panes()[0].update(cx, |pane, cx| {
3410 pane.toolbar()
3411 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
3412 });
3413
3414 ProjectSearchView::new_search(workspace, &workspace::NewSearch, window, cx)
3415 }
3416 })
3417 .unwrap();
3418
3419 let search_view = cx.read(|cx| {
3420 workspace
3421 .read(cx)
3422 .active_pane()
3423 .read(cx)
3424 .active_item()
3425 .and_then(|item| item.downcast::<ProjectSearchView>())
3426 .expect("Search view expected to appear after new search event trigger")
3427 });
3428
3429 // Add 3 search items into the history + another unsubmitted one.
3430 window
3431 .update(cx, |_, window, cx| {
3432 search_view.update(cx, |search_view, cx| {
3433 search_view.search_options = SearchOptions::CASE_SENSITIVE;
3434 search_view.query_editor.update(cx, |query_editor, cx| {
3435 query_editor.set_text("ONE", window, cx)
3436 });
3437 search_view.search(cx);
3438 });
3439 })
3440 .unwrap();
3441
3442 cx.background_executor.run_until_parked();
3443 window
3444 .update(cx, |_, window, cx| {
3445 search_view.update(cx, |search_view, cx| {
3446 search_view.query_editor.update(cx, |query_editor, cx| {
3447 query_editor.set_text("TWO", window, cx)
3448 });
3449 search_view.search(cx);
3450 });
3451 })
3452 .unwrap();
3453 cx.background_executor.run_until_parked();
3454 window
3455 .update(cx, |_, window, cx| {
3456 search_view.update(cx, |search_view, cx| {
3457 search_view.query_editor.update(cx, |query_editor, cx| {
3458 query_editor.set_text("THREE", window, cx)
3459 });
3460 search_view.search(cx);
3461 })
3462 })
3463 .unwrap();
3464 cx.background_executor.run_until_parked();
3465 window
3466 .update(cx, |_, window, cx| {
3467 search_view.update(cx, |search_view, cx| {
3468 search_view.query_editor.update(cx, |query_editor, cx| {
3469 query_editor.set_text("JUST_TEXT_INPUT", window, cx)
3470 });
3471 })
3472 })
3473 .unwrap();
3474 cx.background_executor.run_until_parked();
3475
3476 // Ensure that the latest input with search settings is active.
3477 window
3478 .update(cx, |_, _, cx| {
3479 search_view.update(cx, |search_view, cx| {
3480 assert_eq!(
3481 search_view.query_editor.read(cx).text(cx),
3482 "JUST_TEXT_INPUT"
3483 );
3484 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3485 });
3486 })
3487 .unwrap();
3488
3489 // Next history query after the latest should set the query to the empty string.
3490 window
3491 .update(cx, |_, window, cx| {
3492 search_bar.update(cx, |search_bar, cx| {
3493 search_bar.focus_search(window, cx);
3494 search_bar.next_history_query(&NextHistoryQuery, window, cx);
3495 })
3496 })
3497 .unwrap();
3498 window
3499 .update(cx, |_, _, cx| {
3500 search_view.update(cx, |search_view, cx| {
3501 assert_eq!(search_view.query_editor.read(cx).text(cx), "");
3502 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3503 });
3504 })
3505 .unwrap();
3506 window
3507 .update(cx, |_, window, cx| {
3508 search_bar.update(cx, |search_bar, cx| {
3509 search_bar.focus_search(window, cx);
3510 search_bar.next_history_query(&NextHistoryQuery, window, cx);
3511 })
3512 })
3513 .unwrap();
3514 window
3515 .update(cx, |_, _, cx| {
3516 search_view.update(cx, |search_view, cx| {
3517 assert_eq!(search_view.query_editor.read(cx).text(cx), "");
3518 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3519 });
3520 })
3521 .unwrap();
3522
3523 // First previous query for empty current query should set the query to the latest submitted one.
3524 window
3525 .update(cx, |_, window, cx| {
3526 search_bar.update(cx, |search_bar, cx| {
3527 search_bar.focus_search(window, cx);
3528 search_bar.previous_history_query(&PreviousHistoryQuery, window, cx);
3529 });
3530 })
3531 .unwrap();
3532 window
3533 .update(cx, |_, _, cx| {
3534 search_view.update(cx, |search_view, cx| {
3535 assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
3536 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3537 });
3538 })
3539 .unwrap();
3540
3541 // Further previous items should go over the history in reverse order.
3542 window
3543 .update(cx, |_, window, cx| {
3544 search_bar.update(cx, |search_bar, cx| {
3545 search_bar.focus_search(window, cx);
3546 search_bar.previous_history_query(&PreviousHistoryQuery, window, cx);
3547 });
3548 })
3549 .unwrap();
3550 window
3551 .update(cx, |_, _, cx| {
3552 search_view.update(cx, |search_view, cx| {
3553 assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
3554 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3555 });
3556 })
3557 .unwrap();
3558
3559 // Previous items should never go behind the first history item.
3560 window
3561 .update(cx, |_, window, cx| {
3562 search_bar.update(cx, |search_bar, cx| {
3563 search_bar.focus_search(window, cx);
3564 search_bar.previous_history_query(&PreviousHistoryQuery, window, cx);
3565 });
3566 })
3567 .unwrap();
3568 window
3569 .update(cx, |_, _, cx| {
3570 search_view.update(cx, |search_view, cx| {
3571 assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE");
3572 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3573 });
3574 })
3575 .unwrap();
3576 window
3577 .update(cx, |_, window, cx| {
3578 search_bar.update(cx, |search_bar, cx| {
3579 search_bar.focus_search(window, cx);
3580 search_bar.previous_history_query(&PreviousHistoryQuery, window, cx);
3581 });
3582 })
3583 .unwrap();
3584 window
3585 .update(cx, |_, _, cx| {
3586 search_view.update(cx, |search_view, cx| {
3587 assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE");
3588 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3589 });
3590 })
3591 .unwrap();
3592
3593 // Next items should go over the history in the original order.
3594 window
3595 .update(cx, |_, window, cx| {
3596 search_bar.update(cx, |search_bar, cx| {
3597 search_bar.focus_search(window, cx);
3598 search_bar.next_history_query(&NextHistoryQuery, window, cx);
3599 });
3600 })
3601 .unwrap();
3602 window
3603 .update(cx, |_, _, cx| {
3604 search_view.update(cx, |search_view, cx| {
3605 assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
3606 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3607 });
3608 })
3609 .unwrap();
3610
3611 window
3612 .update(cx, |_, window, cx| {
3613 search_view.update(cx, |search_view, cx| {
3614 search_view.query_editor.update(cx, |query_editor, cx| {
3615 query_editor.set_text("TWO_NEW", window, cx)
3616 });
3617 search_view.search(cx);
3618 });
3619 })
3620 .unwrap();
3621 cx.background_executor.run_until_parked();
3622 window
3623 .update(cx, |_, _, cx| {
3624 search_view.update(cx, |search_view, cx| {
3625 assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW");
3626 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3627 });
3628 })
3629 .unwrap();
3630
3631 // New search input should add another entry to history and move the selection to the end of the history.
3632 window
3633 .update(cx, |_, window, cx| {
3634 search_bar.update(cx, |search_bar, cx| {
3635 search_bar.focus_search(window, cx);
3636 search_bar.previous_history_query(&PreviousHistoryQuery, window, cx);
3637 });
3638 })
3639 .unwrap();
3640 window
3641 .update(cx, |_, _, cx| {
3642 search_view.update(cx, |search_view, cx| {
3643 assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
3644 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3645 });
3646 })
3647 .unwrap();
3648 window
3649 .update(cx, |_, window, cx| {
3650 search_bar.update(cx, |search_bar, cx| {
3651 search_bar.focus_search(window, cx);
3652 search_bar.previous_history_query(&PreviousHistoryQuery, window, cx);
3653 });
3654 })
3655 .unwrap();
3656 window
3657 .update(cx, |_, _, cx| {
3658 search_view.update(cx, |search_view, cx| {
3659 assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO");
3660 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3661 });
3662 })
3663 .unwrap();
3664 window
3665 .update(cx, |_, window, cx| {
3666 search_bar.update(cx, |search_bar, cx| {
3667 search_bar.focus_search(window, cx);
3668 search_bar.next_history_query(&NextHistoryQuery, window, cx);
3669 });
3670 })
3671 .unwrap();
3672 window
3673 .update(cx, |_, _, cx| {
3674 search_view.update(cx, |search_view, cx| {
3675 assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE");
3676 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3677 });
3678 })
3679 .unwrap();
3680 window
3681 .update(cx, |_, window, cx| {
3682 search_bar.update(cx, |search_bar, cx| {
3683 search_bar.focus_search(window, cx);
3684 search_bar.next_history_query(&NextHistoryQuery, window, cx);
3685 });
3686 })
3687 .unwrap();
3688 window
3689 .update(cx, |_, _, cx| {
3690 search_view.update(cx, |search_view, cx| {
3691 assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW");
3692 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3693 });
3694 })
3695 .unwrap();
3696 window
3697 .update(cx, |_, window, cx| {
3698 search_bar.update(cx, |search_bar, cx| {
3699 search_bar.focus_search(window, cx);
3700 search_bar.next_history_query(&NextHistoryQuery, window, cx);
3701 });
3702 })
3703 .unwrap();
3704 window
3705 .update(cx, |_, _, cx| {
3706 search_view.update(cx, |search_view, cx| {
3707 assert_eq!(search_view.query_editor.read(cx).text(cx), "");
3708 assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE);
3709 });
3710 })
3711 .unwrap();
3712 }
3713
3714 #[perf]
3715 #[gpui::test]
3716 async fn test_search_query_history_with_multiple_views(cx: &mut TestAppContext) {
3717 init_test(cx);
3718
3719 let fs = FakeFs::new(cx.background_executor.clone());
3720 fs.insert_tree(
3721 path!("/dir"),
3722 json!({
3723 "one.rs": "const ONE: usize = 1;",
3724 }),
3725 )
3726 .await;
3727 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
3728 let worktree_id = project.update(cx, |this, cx| {
3729 this.worktrees(cx).next().unwrap().read(cx).id()
3730 });
3731
3732 let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
3733 let workspace = window.root(cx).unwrap();
3734
3735 let panes: Vec<_> = window
3736 .update(cx, |this, _, _| this.panes().to_owned())
3737 .unwrap();
3738
3739 let search_bar_1 = window.build_entity(cx, |_, _| ProjectSearchBar::new());
3740 let search_bar_2 = window.build_entity(cx, |_, _| ProjectSearchBar::new());
3741
3742 assert_eq!(panes.len(), 1);
3743 let first_pane = panes.first().cloned().unwrap();
3744 assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 0);
3745 window
3746 .update(cx, |workspace, window, cx| {
3747 workspace.open_path(
3748 (worktree_id, rel_path("one.rs")),
3749 Some(first_pane.downgrade()),
3750 true,
3751 window,
3752 cx,
3753 )
3754 })
3755 .unwrap()
3756 .await
3757 .unwrap();
3758 assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 1);
3759
3760 // Add a project search item to the first pane
3761 window
3762 .update(cx, {
3763 let search_bar = search_bar_1.clone();
3764 |workspace, window, cx| {
3765 first_pane.update(cx, |pane, cx| {
3766 pane.toolbar()
3767 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
3768 });
3769
3770 ProjectSearchView::new_search(workspace, &workspace::NewSearch, window, cx)
3771 }
3772 })
3773 .unwrap();
3774 let search_view_1 = cx.read(|cx| {
3775 workspace
3776 .read(cx)
3777 .active_item(cx)
3778 .and_then(|item| item.downcast::<ProjectSearchView>())
3779 .expect("Search view expected to appear after new search event trigger")
3780 });
3781
3782 let second_pane = window
3783 .update(cx, |workspace, window, cx| {
3784 workspace.split_and_clone(
3785 first_pane.clone(),
3786 workspace::SplitDirection::Right,
3787 window,
3788 cx,
3789 )
3790 })
3791 .unwrap()
3792 .await
3793 .unwrap();
3794 assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 1);
3795
3796 assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 1);
3797 assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 2);
3798
3799 // Add a project search item to the second pane
3800 window
3801 .update(cx, {
3802 let search_bar = search_bar_2.clone();
3803 let pane = second_pane.clone();
3804 move |workspace, window, cx| {
3805 assert_eq!(workspace.panes().len(), 2);
3806 pane.update(cx, |pane, cx| {
3807 pane.toolbar()
3808 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
3809 });
3810
3811 ProjectSearchView::new_search(workspace, &workspace::NewSearch, window, cx)
3812 }
3813 })
3814 .unwrap();
3815
3816 let search_view_2 = cx.read(|cx| {
3817 workspace
3818 .read(cx)
3819 .active_item(cx)
3820 .and_then(|item| item.downcast::<ProjectSearchView>())
3821 .expect("Search view expected to appear after new search event trigger")
3822 });
3823
3824 cx.run_until_parked();
3825 assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 2);
3826 assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 2);
3827
3828 let update_search_view =
3829 |search_view: &Entity<ProjectSearchView>, query: &str, cx: &mut TestAppContext| {
3830 window
3831 .update(cx, |_, window, cx| {
3832 search_view.update(cx, |search_view, cx| {
3833 search_view.query_editor.update(cx, |query_editor, cx| {
3834 query_editor.set_text(query, window, cx)
3835 });
3836 search_view.search(cx);
3837 });
3838 })
3839 .unwrap();
3840 };
3841
3842 let active_query =
3843 |search_view: &Entity<ProjectSearchView>, cx: &mut TestAppContext| -> String {
3844 window
3845 .update(cx, |_, _, cx| {
3846 search_view.update(cx, |search_view, cx| {
3847 search_view.query_editor.read(cx).text(cx)
3848 })
3849 })
3850 .unwrap()
3851 };
3852
3853 let select_prev_history_item =
3854 |search_bar: &Entity<ProjectSearchBar>, cx: &mut TestAppContext| {
3855 window
3856 .update(cx, |_, window, cx| {
3857 search_bar.update(cx, |search_bar, cx| {
3858 search_bar.focus_search(window, cx);
3859 search_bar.previous_history_query(&PreviousHistoryQuery, window, cx);
3860 })
3861 })
3862 .unwrap();
3863 };
3864
3865 let select_next_history_item =
3866 |search_bar: &Entity<ProjectSearchBar>, cx: &mut TestAppContext| {
3867 window
3868 .update(cx, |_, window, cx| {
3869 search_bar.update(cx, |search_bar, cx| {
3870 search_bar.focus_search(window, cx);
3871 search_bar.next_history_query(&NextHistoryQuery, window, cx);
3872 })
3873 })
3874 .unwrap();
3875 };
3876
3877 update_search_view(&search_view_1, "ONE", cx);
3878 cx.background_executor.run_until_parked();
3879
3880 update_search_view(&search_view_2, "TWO", cx);
3881 cx.background_executor.run_until_parked();
3882
3883 assert_eq!(active_query(&search_view_1, cx), "ONE");
3884 assert_eq!(active_query(&search_view_2, cx), "TWO");
3885
3886 // Selecting previous history item should select the query from search view 1.
3887 select_prev_history_item(&search_bar_2, cx);
3888 assert_eq!(active_query(&search_view_2, cx), "ONE");
3889
3890 // Selecting the previous history item should not change the query as it is already the first item.
3891 select_prev_history_item(&search_bar_2, cx);
3892 assert_eq!(active_query(&search_view_2, cx), "ONE");
3893
3894 // Changing the query in search view 2 should not affect the history of search view 1.
3895 assert_eq!(active_query(&search_view_1, cx), "ONE");
3896
3897 // Deploying a new search in search view 2
3898 update_search_view(&search_view_2, "THREE", cx);
3899 cx.background_executor.run_until_parked();
3900
3901 select_next_history_item(&search_bar_2, cx);
3902 assert_eq!(active_query(&search_view_2, cx), "");
3903
3904 select_prev_history_item(&search_bar_2, cx);
3905 assert_eq!(active_query(&search_view_2, cx), "THREE");
3906
3907 select_prev_history_item(&search_bar_2, cx);
3908 assert_eq!(active_query(&search_view_2, cx), "TWO");
3909
3910 select_prev_history_item(&search_bar_2, cx);
3911 assert_eq!(active_query(&search_view_2, cx), "ONE");
3912
3913 select_prev_history_item(&search_bar_2, cx);
3914 assert_eq!(active_query(&search_view_2, cx), "ONE");
3915
3916 // Search view 1 should now see the query from search view 2.
3917 assert_eq!(active_query(&search_view_1, cx), "ONE");
3918
3919 select_next_history_item(&search_bar_2, cx);
3920 assert_eq!(active_query(&search_view_2, cx), "TWO");
3921
3922 // Here is the new query from search view 2
3923 select_next_history_item(&search_bar_2, cx);
3924 assert_eq!(active_query(&search_view_2, cx), "THREE");
3925
3926 select_next_history_item(&search_bar_2, cx);
3927 assert_eq!(active_query(&search_view_2, cx), "");
3928
3929 select_next_history_item(&search_bar_1, cx);
3930 assert_eq!(active_query(&search_view_1, cx), "TWO");
3931
3932 select_next_history_item(&search_bar_1, cx);
3933 assert_eq!(active_query(&search_view_1, cx), "THREE");
3934
3935 select_next_history_item(&search_bar_1, cx);
3936 assert_eq!(active_query(&search_view_1, cx), "");
3937 }
3938
3939 #[perf]
3940 #[gpui::test]
3941 async fn test_deploy_search_with_multiple_panes(cx: &mut TestAppContext) {
3942 init_test(cx);
3943
3944 // Setup 2 panes, both with a file open and one with a project search.
3945 let fs = FakeFs::new(cx.background_executor.clone());
3946 fs.insert_tree(
3947 path!("/dir"),
3948 json!({
3949 "one.rs": "const ONE: usize = 1;",
3950 }),
3951 )
3952 .await;
3953 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
3954 let worktree_id = project.update(cx, |this, cx| {
3955 this.worktrees(cx).next().unwrap().read(cx).id()
3956 });
3957 let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
3958 let panes: Vec<_> = window
3959 .update(cx, |this, _, _| this.panes().to_owned())
3960 .unwrap();
3961 assert_eq!(panes.len(), 1);
3962 let first_pane = panes.first().cloned().unwrap();
3963 assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 0);
3964 window
3965 .update(cx, |workspace, window, cx| {
3966 workspace.open_path(
3967 (worktree_id, rel_path("one.rs")),
3968 Some(first_pane.downgrade()),
3969 true,
3970 window,
3971 cx,
3972 )
3973 })
3974 .unwrap()
3975 .await
3976 .unwrap();
3977 assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 1);
3978 let second_pane = window
3979 .update(cx, |workspace, window, cx| {
3980 workspace.split_and_clone(
3981 first_pane.clone(),
3982 workspace::SplitDirection::Right,
3983 window,
3984 cx,
3985 )
3986 })
3987 .unwrap()
3988 .await
3989 .unwrap();
3990 assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 1);
3991 assert!(
3992 window
3993 .update(cx, |_, window, cx| second_pane
3994 .focus_handle(cx)
3995 .contains_focused(window, cx))
3996 .unwrap()
3997 );
3998 let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new());
3999 window
4000 .update(cx, {
4001 let search_bar = search_bar.clone();
4002 let pane = first_pane.clone();
4003 move |workspace, window, cx| {
4004 assert_eq!(workspace.panes().len(), 2);
4005 pane.update(cx, move |pane, cx| {
4006 pane.toolbar()
4007 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
4008 });
4009 }
4010 })
4011 .unwrap();
4012
4013 // Add a project search item to the second pane
4014 window
4015 .update(cx, {
4016 |workspace, window, cx| {
4017 assert_eq!(workspace.panes().len(), 2);
4018 second_pane.update(cx, |pane, cx| {
4019 pane.toolbar()
4020 .update(cx, |toolbar, cx| toolbar.add_item(search_bar, window, cx))
4021 });
4022
4023 ProjectSearchView::new_search(workspace, &workspace::NewSearch, window, cx)
4024 }
4025 })
4026 .unwrap();
4027
4028 cx.run_until_parked();
4029 assert_eq!(cx.update(|cx| second_pane.read(cx).items_len()), 2);
4030 assert_eq!(cx.update(|cx| first_pane.read(cx).items_len()), 1);
4031
4032 // Focus the first pane
4033 window
4034 .update(cx, |workspace, window, cx| {
4035 assert_eq!(workspace.active_pane(), &second_pane);
4036 second_pane.update(cx, |this, cx| {
4037 assert_eq!(this.active_item_index(), 1);
4038 this.activate_previous_item(&Default::default(), window, cx);
4039 assert_eq!(this.active_item_index(), 0);
4040 });
4041 workspace.activate_pane_in_direction(workspace::SplitDirection::Left, window, cx);
4042 })
4043 .unwrap();
4044 window
4045 .update(cx, |workspace, _, cx| {
4046 assert_eq!(workspace.active_pane(), &first_pane);
4047 assert_eq!(first_pane.read(cx).items_len(), 1);
4048 assert_eq!(second_pane.read(cx).items_len(), 2);
4049 })
4050 .unwrap();
4051
4052 // Deploy a new search
4053 cx.dispatch_action(window.into(), DeploySearch::find());
4054
4055 // Both panes should now have a project search in them
4056 window
4057 .update(cx, |workspace, window, cx| {
4058 assert_eq!(workspace.active_pane(), &first_pane);
4059 first_pane.read_with(cx, |this, _| {
4060 assert_eq!(this.active_item_index(), 1);
4061 assert_eq!(this.items_len(), 2);
4062 });
4063 second_pane.update(cx, |this, cx| {
4064 assert!(!cx.focus_handle().contains_focused(window, cx));
4065 assert_eq!(this.items_len(), 2);
4066 });
4067 })
4068 .unwrap();
4069
4070 // Focus the second pane's non-search item
4071 window
4072 .update(cx, |_workspace, window, cx| {
4073 second_pane.update(cx, |pane, cx| {
4074 pane.activate_next_item(&Default::default(), window, cx)
4075 });
4076 })
4077 .unwrap();
4078
4079 // Deploy a new search
4080 cx.dispatch_action(window.into(), DeploySearch::find());
4081
4082 // The project search view should now be focused in the second pane
4083 // And the number of items should be unchanged.
4084 window
4085 .update(cx, |_workspace, _, cx| {
4086 second_pane.update(cx, |pane, _cx| {
4087 assert!(
4088 pane.active_item()
4089 .unwrap()
4090 .downcast::<ProjectSearchView>()
4091 .is_some()
4092 );
4093
4094 assert_eq!(pane.items_len(), 2);
4095 });
4096 })
4097 .unwrap();
4098 }
4099
4100 #[perf]
4101 #[gpui::test]
4102 async fn test_scroll_search_results_to_top(cx: &mut TestAppContext) {
4103 init_test(cx);
4104
4105 // We need many lines in the search results to be able to scroll the window
4106 let fs = FakeFs::new(cx.background_executor.clone());
4107 fs.insert_tree(
4108 path!("/dir"),
4109 json!({
4110 "1.txt": "\n\n\n\n\n A \n\n\n\n\n",
4111 "2.txt": "\n\n\n\n\n A \n\n\n\n\n",
4112 "3.rs": "\n\n\n\n\n A \n\n\n\n\n",
4113 "4.rs": "\n\n\n\n\n A \n\n\n\n\n",
4114 "5.rs": "\n\n\n\n\n A \n\n\n\n\n",
4115 "6.rs": "\n\n\n\n\n A \n\n\n\n\n",
4116 "7.rs": "\n\n\n\n\n A \n\n\n\n\n",
4117 "8.rs": "\n\n\n\n\n A \n\n\n\n\n",
4118 "9.rs": "\n\n\n\n\n A \n\n\n\n\n",
4119 "a.rs": "\n\n\n\n\n A \n\n\n\n\n",
4120 "b.rs": "\n\n\n\n\n B \n\n\n\n\n",
4121 "c.rs": "\n\n\n\n\n B \n\n\n\n\n",
4122 "d.rs": "\n\n\n\n\n B \n\n\n\n\n",
4123 "e.rs": "\n\n\n\n\n B \n\n\n\n\n",
4124 "f.rs": "\n\n\n\n\n B \n\n\n\n\n",
4125 "g.rs": "\n\n\n\n\n B \n\n\n\n\n",
4126 "h.rs": "\n\n\n\n\n B \n\n\n\n\n",
4127 "i.rs": "\n\n\n\n\n B \n\n\n\n\n",
4128 "j.rs": "\n\n\n\n\n B \n\n\n\n\n",
4129 "k.rs": "\n\n\n\n\n B \n\n\n\n\n",
4130 }),
4131 )
4132 .await;
4133 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
4134 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
4135 let workspace = window.root(cx).unwrap();
4136 let search = cx.new(|cx| ProjectSearch::new(project, cx));
4137 let search_view = cx.add_window(|window, cx| {
4138 ProjectSearchView::new(workspace.downgrade(), search.clone(), window, cx, None)
4139 });
4140
4141 // First search
4142 perform_search(search_view, "A", cx);
4143 search_view
4144 .update(cx, |search_view, window, cx| {
4145 search_view.results_editor.update(cx, |results_editor, cx| {
4146 // Results are correct and scrolled to the top
4147 assert_eq!(
4148 results_editor.display_text(cx).match_indices(" A ").count(),
4149 10
4150 );
4151 assert_eq!(results_editor.scroll_position(cx), Point::default());
4152
4153 // Scroll results all the way down
4154 results_editor.scroll(
4155 Point::new(0., f64::MAX),
4156 Some(Axis::Vertical),
4157 window,
4158 cx,
4159 );
4160 });
4161 })
4162 .expect("unable to update search view");
4163
4164 // Second search
4165 perform_search(search_view, "B", cx);
4166 search_view
4167 .update(cx, |search_view, _, cx| {
4168 search_view.results_editor.update(cx, |results_editor, cx| {
4169 // Results are correct...
4170 assert_eq!(
4171 results_editor.display_text(cx).match_indices(" B ").count(),
4172 10
4173 );
4174 // ...and scrolled back to the top
4175 assert_eq!(results_editor.scroll_position(cx), Point::default());
4176 });
4177 })
4178 .expect("unable to update search view");
4179 }
4180
4181 #[perf]
4182 #[gpui::test]
4183 async fn test_buffer_search_query_reused(cx: &mut TestAppContext) {
4184 init_test(cx);
4185
4186 let fs = FakeFs::new(cx.background_executor.clone());
4187 fs.insert_tree(
4188 path!("/dir"),
4189 json!({
4190 "one.rs": "const ONE: usize = 1;",
4191 }),
4192 )
4193 .await;
4194 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
4195 let worktree_id = project.update(cx, |this, cx| {
4196 this.worktrees(cx).next().unwrap().read(cx).id()
4197 });
4198 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
4199 let workspace = window.root(cx).unwrap();
4200 let mut cx = VisualTestContext::from_window(*window.deref(), cx);
4201
4202 let editor = workspace
4203 .update_in(&mut cx, |workspace, window, cx| {
4204 workspace.open_path((worktree_id, rel_path("one.rs")), None, true, window, cx)
4205 })
4206 .await
4207 .unwrap()
4208 .downcast::<Editor>()
4209 .unwrap();
4210
4211 // Wait for the unstaged changes to be loaded
4212 cx.run_until_parked();
4213
4214 let buffer_search_bar = cx.new_window_entity(|window, cx| {
4215 let mut search_bar =
4216 BufferSearchBar::new(Some(project.read(cx).languages().clone()), window, cx);
4217 search_bar.set_active_pane_item(Some(&editor), window, cx);
4218 search_bar.show(window, cx);
4219 search_bar
4220 });
4221
4222 let panes: Vec<_> = window
4223 .update(&mut cx, |this, _, _| this.panes().to_owned())
4224 .unwrap();
4225 assert_eq!(panes.len(), 1);
4226 let pane = panes.first().cloned().unwrap();
4227 pane.update_in(&mut cx, |pane, window, cx| {
4228 pane.toolbar().update(cx, |toolbar, cx| {
4229 toolbar.add_item(buffer_search_bar.clone(), window, cx);
4230 })
4231 });
4232
4233 let buffer_search_query = "search bar query";
4234 buffer_search_bar
4235 .update_in(&mut cx, |buffer_search_bar, window, cx| {
4236 buffer_search_bar.focus_handle(cx).focus(window);
4237 buffer_search_bar.search(buffer_search_query, None, true, window, cx)
4238 })
4239 .await
4240 .unwrap();
4241
4242 workspace.update_in(&mut cx, |workspace, window, cx| {
4243 ProjectSearchView::new_search(workspace, &workspace::NewSearch, window, cx)
4244 });
4245 cx.run_until_parked();
4246 let project_search_view = pane
4247 .read_with(&cx, |pane, _| {
4248 pane.active_item()
4249 .and_then(|item| item.downcast::<ProjectSearchView>())
4250 })
4251 .expect("should open a project search view after spawning a new search");
4252 project_search_view.update(&mut cx, |search_view, cx| {
4253 assert_eq!(
4254 search_view.search_query_text(cx),
4255 buffer_search_query,
4256 "Project search should take the query from the buffer search bar since it got focused and had a query inside"
4257 );
4258 });
4259 }
4260
4261 #[gpui::test]
4262 async fn test_search_dismisses_modal(cx: &mut TestAppContext) {
4263 init_test(cx);
4264
4265 let fs = FakeFs::new(cx.background_executor.clone());
4266 fs.insert_tree(
4267 path!("/dir"),
4268 json!({
4269 "one.rs": "const ONE: usize = 1;",
4270 }),
4271 )
4272 .await;
4273 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
4274 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
4275
4276 struct EmptyModalView {
4277 focus_handle: gpui::FocusHandle,
4278 }
4279 impl EventEmitter<gpui::DismissEvent> for EmptyModalView {}
4280 impl Render for EmptyModalView {
4281 fn render(&mut self, _: &mut Window, _: &mut Context<'_, Self>) -> impl IntoElement {
4282 div()
4283 }
4284 }
4285 impl Focusable for EmptyModalView {
4286 fn focus_handle(&self, _cx: &App) -> gpui::FocusHandle {
4287 self.focus_handle.clone()
4288 }
4289 }
4290 impl workspace::ModalView for EmptyModalView {}
4291
4292 window
4293 .update(cx, |workspace, window, cx| {
4294 workspace.toggle_modal(window, cx, |_, cx| EmptyModalView {
4295 focus_handle: cx.focus_handle(),
4296 });
4297 assert!(workspace.has_active_modal(window, cx));
4298 })
4299 .unwrap();
4300
4301 cx.dispatch_action(window.into(), Deploy::find());
4302
4303 window
4304 .update(cx, |workspace, window, cx| {
4305 assert!(!workspace.has_active_modal(window, cx));
4306 workspace.toggle_modal(window, cx, |_, cx| EmptyModalView {
4307 focus_handle: cx.focus_handle(),
4308 });
4309 assert!(workspace.has_active_modal(window, cx));
4310 })
4311 .unwrap();
4312
4313 cx.dispatch_action(window.into(), DeploySearch::find());
4314
4315 window
4316 .update(cx, |workspace, window, cx| {
4317 assert!(!workspace.has_active_modal(window, cx));
4318 })
4319 .unwrap();
4320 }
4321
4322 #[perf]
4323 #[gpui::test]
4324 async fn test_search_with_inlays(cx: &mut TestAppContext) {
4325 init_test(cx);
4326 cx.update(|cx| {
4327 SettingsStore::update_global(cx, |store, cx| {
4328 store.update_user_settings(cx, |settings| {
4329 settings.project.all_languages.defaults.inlay_hints =
4330 Some(InlayHintSettingsContent {
4331 enabled: Some(true),
4332 ..InlayHintSettingsContent::default()
4333 })
4334 });
4335 });
4336 });
4337
4338 let fs = FakeFs::new(cx.background_executor.clone());
4339 fs.insert_tree(
4340 path!("/dir"),
4341 // `\n` , a trailing line on the end, is important for the test case
4342 json!({
4343 "main.rs": "fn main() { let a = 2; }\n",
4344 }),
4345 )
4346 .await;
4347
4348 let requests_count = Arc::new(AtomicUsize::new(0));
4349 let closure_requests_count = requests_count.clone();
4350 let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
4351 let language_registry = project.read_with(cx, |project, _| project.languages().clone());
4352 let language = rust_lang();
4353 language_registry.add(language);
4354 let mut fake_servers = language_registry.register_fake_lsp(
4355 "Rust",
4356 FakeLspAdapter {
4357 capabilities: lsp::ServerCapabilities {
4358 inlay_hint_provider: Some(lsp::OneOf::Left(true)),
4359 ..lsp::ServerCapabilities::default()
4360 },
4361 initializer: Some(Box::new(move |fake_server| {
4362 let requests_count = closure_requests_count.clone();
4363 fake_server.set_request_handler::<lsp::request::InlayHintRequest, _, _>({
4364 move |_, _| {
4365 let requests_count = requests_count.clone();
4366 async move {
4367 requests_count.fetch_add(1, atomic::Ordering::Release);
4368 Ok(Some(vec![lsp::InlayHint {
4369 position: lsp::Position::new(0, 17),
4370 label: lsp::InlayHintLabel::String(": i32".to_owned()),
4371 kind: Some(lsp::InlayHintKind::TYPE),
4372 text_edits: None,
4373 tooltip: None,
4374 padding_left: None,
4375 padding_right: None,
4376 data: None,
4377 }]))
4378 }
4379 }
4380 });
4381 })),
4382 ..FakeLspAdapter::default()
4383 },
4384 );
4385
4386 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
4387 let workspace = window.root(cx).unwrap();
4388 let search = cx.new(|cx| ProjectSearch::new(project.clone(), cx));
4389 let search_view = cx.add_window(|window, cx| {
4390 ProjectSearchView::new(workspace.downgrade(), search.clone(), window, cx, None)
4391 });
4392
4393 perform_search(search_view, "let ", cx);
4394 let fake_server = fake_servers.next().await.unwrap();
4395 cx.executor().advance_clock(Duration::from_secs(1));
4396 cx.executor().run_until_parked();
4397 search_view
4398 .update(cx, |search_view, _, cx| {
4399 assert_eq!(
4400 search_view
4401 .results_editor
4402 .update(cx, |editor, cx| editor.display_text(cx)),
4403 "\n\nfn main() { let a: i32 = 2; }\n"
4404 );
4405 })
4406 .unwrap();
4407 assert_eq!(
4408 requests_count.load(atomic::Ordering::Acquire),
4409 1,
4410 "New hints should have been queried",
4411 );
4412
4413 // Can do the 2nd search without any panics
4414 perform_search(search_view, "let ", cx);
4415 cx.executor().advance_clock(Duration::from_secs(1));
4416 cx.executor().run_until_parked();
4417 search_view
4418 .update(cx, |search_view, _, cx| {
4419 assert_eq!(
4420 search_view
4421 .results_editor
4422 .update(cx, |editor, cx| editor.display_text(cx)),
4423 "\n\nfn main() { let a: i32 = 2; }\n"
4424 );
4425 })
4426 .unwrap();
4427 assert_eq!(
4428 requests_count.load(atomic::Ordering::Acquire),
4429 2,
4430 "We did drop the previous buffer when cleared the old project search results, hence another query was made",
4431 );
4432
4433 let singleton_editor = window
4434 .update(cx, |workspace, window, cx| {
4435 workspace.open_abs_path(
4436 PathBuf::from(path!("/dir/main.rs")),
4437 workspace::OpenOptions::default(),
4438 window,
4439 cx,
4440 )
4441 })
4442 .unwrap()
4443 .await
4444 .unwrap()
4445 .downcast::<Editor>()
4446 .unwrap();
4447 cx.executor().advance_clock(Duration::from_millis(100));
4448 cx.executor().run_until_parked();
4449 singleton_editor.update(cx, |editor, cx| {
4450 assert_eq!(
4451 editor.display_text(cx),
4452 "fn main() { let a: i32 = 2; }\n",
4453 "Newly opened editor should have the correct text with hints",
4454 );
4455 });
4456 assert_eq!(
4457 requests_count.load(atomic::Ordering::Acquire),
4458 2,
4459 "Opening the same buffer again should reuse the cached hints",
4460 );
4461
4462 window
4463 .update(cx, |_, window, cx| {
4464 singleton_editor.update(cx, |editor, cx| {
4465 editor.handle_input("test", window, cx);
4466 });
4467 })
4468 .unwrap();
4469
4470 cx.executor().advance_clock(Duration::from_secs(1));
4471 cx.executor().run_until_parked();
4472 singleton_editor.update(cx, |editor, cx| {
4473 assert_eq!(
4474 editor.display_text(cx),
4475 "testfn main() { l: i32et a = 2; }\n",
4476 "Newly opened editor should have the correct text with hints",
4477 );
4478 });
4479 assert_eq!(
4480 requests_count.load(atomic::Ordering::Acquire),
4481 3,
4482 "We have edited the buffer and should send a new request",
4483 );
4484
4485 window
4486 .update(cx, |_, window, cx| {
4487 singleton_editor.update(cx, |editor, cx| {
4488 editor.undo(&editor::actions::Undo, window, cx);
4489 });
4490 })
4491 .unwrap();
4492 cx.executor().advance_clock(Duration::from_secs(1));
4493 cx.executor().run_until_parked();
4494 assert_eq!(
4495 requests_count.load(atomic::Ordering::Acquire),
4496 4,
4497 "We have edited the buffer again and should send a new request again",
4498 );
4499 singleton_editor.update(cx, |editor, cx| {
4500 assert_eq!(
4501 editor.display_text(cx),
4502 "fn main() { let a: i32 = 2; }\n",
4503 "Newly opened editor should have the correct text with hints",
4504 );
4505 });
4506 project.update(cx, |_, cx| {
4507 cx.emit(project::Event::RefreshInlayHints {
4508 server_id: fake_server.server.server_id(),
4509 request_id: Some(1),
4510 });
4511 });
4512 cx.executor().advance_clock(Duration::from_secs(1));
4513 cx.executor().run_until_parked();
4514 assert_eq!(
4515 requests_count.load(atomic::Ordering::Acquire),
4516 5,
4517 "After a simulated server refresh request, we should have sent another request",
4518 );
4519
4520 perform_search(search_view, "let ", cx);
4521 cx.executor().advance_clock(Duration::from_secs(1));
4522 cx.executor().run_until_parked();
4523 assert_eq!(
4524 requests_count.load(atomic::Ordering::Acquire),
4525 5,
4526 "New project search should reuse the cached hints",
4527 );
4528 search_view
4529 .update(cx, |search_view, _, cx| {
4530 assert_eq!(
4531 search_view
4532 .results_editor
4533 .update(cx, |editor, cx| editor.display_text(cx)),
4534 "\n\nfn main() { let a: i32 = 2; }\n"
4535 );
4536 })
4537 .unwrap();
4538 }
4539
4540 fn init_test(cx: &mut TestAppContext) {
4541 cx.update(|cx| {
4542 let settings = SettingsStore::test(cx);
4543 cx.set_global(settings);
4544
4545 theme::init(theme::LoadThemes::JustBase, cx);
4546
4547 editor::init(cx);
4548 crate::init(cx);
4549 });
4550 }
4551
4552 fn perform_search(
4553 search_view: WindowHandle<ProjectSearchView>,
4554 text: impl Into<Arc<str>>,
4555 cx: &mut TestAppContext,
4556 ) {
4557 search_view
4558 .update(cx, |search_view, window, cx| {
4559 search_view.query_editor.update(cx, |query_editor, cx| {
4560 query_editor.set_text(text, window, cx)
4561 });
4562 search_view.search(cx);
4563 })
4564 .unwrap();
4565 // Ensure editor highlights appear after the search is done
4566 cx.executor().advance_clock(
4567 editor::SELECTION_HIGHLIGHT_DEBOUNCE_TIMEOUT + Duration::from_millis(100),
4568 );
4569 cx.background_executor.run_until_parked();
4570 }
4571}