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