1use crate::{
2 active_match_index, match_index_for_direction, Direction, SearchOption, SelectMatch,
3 ToggleSearchOption,
4};
5use collections::HashMap;
6use editor::{Anchor, Autoscroll, Editor, MultiBuffer, SelectAll};
7use gpui::{
8 action, elements::*, keymap::Binding, platform::CursorStyle, AppContext, ElementBox, Entity,
9 ModelContext, ModelHandle, MutableAppContext, RenderContext, Subscription, Task, View,
10 ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
11};
12use project::{search::SearchQuery, Project};
13use std::{
14 any::{Any, TypeId},
15 ops::Range,
16 path::PathBuf,
17};
18use util::ResultExt as _;
19use workspace::{
20 Item, ItemNavHistory, Pane, Settings, ToolbarItemLocation, ToolbarItemView, Workspace,
21};
22
23action!(Deploy);
24action!(Search);
25action!(SearchInNew);
26action!(ToggleFocus);
27
28const MAX_TAB_TITLE_LEN: usize = 24;
29
30#[derive(Default)]
31struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
32
33pub fn init(cx: &mut MutableAppContext) {
34 cx.set_global(ActiveSearches::default());
35 cx.add_bindings([
36 Binding::new("cmd-shift-F", ToggleFocus, Some("Pane")),
37 Binding::new("cmd-f", ToggleFocus, Some("Pane")),
38 Binding::new("cmd-shift-F", Deploy, Some("Workspace")),
39 Binding::new("enter", Search, Some("ProjectSearchBar")),
40 Binding::new("cmd-enter", SearchInNew, Some("ProjectSearchBar")),
41 Binding::new("cmd-g", SelectMatch(Direction::Next), Some("Pane")),
42 Binding::new("cmd-shift-G", SelectMatch(Direction::Prev), Some("Pane")),
43 ]);
44 cx.add_action(ProjectSearchView::deploy);
45 cx.add_action(ProjectSearchBar::search);
46 cx.add_action(ProjectSearchBar::search_in_new);
47 cx.add_action(ProjectSearchBar::toggle_search_option);
48 cx.add_action(ProjectSearchBar::select_match);
49 cx.add_action(ProjectSearchBar::toggle_focus);
50 cx.capture_action(ProjectSearchBar::tab);
51}
52
53struct ProjectSearch {
54 project: ModelHandle<Project>,
55 excerpts: ModelHandle<MultiBuffer>,
56 pending_search: Option<Task<Option<()>>>,
57 match_ranges: Vec<Range<Anchor>>,
58 active_query: Option<SearchQuery>,
59}
60
61pub struct ProjectSearchView {
62 model: ModelHandle<ProjectSearch>,
63 query_editor: ViewHandle<Editor>,
64 results_editor: ViewHandle<Editor>,
65 case_sensitive: bool,
66 whole_word: bool,
67 regex: bool,
68 query_contains_error: bool,
69 active_match_index: Option<usize>,
70}
71
72pub struct ProjectSearchBar {
73 active_project_search: Option<ViewHandle<ProjectSearchView>>,
74 subscription: Option<Subscription>,
75}
76
77impl Entity for ProjectSearch {
78 type Event = ();
79}
80
81impl ProjectSearch {
82 fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
83 let replica_id = project.read(cx).replica_id();
84 Self {
85 project,
86 excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
87 pending_search: Default::default(),
88 match_ranges: Default::default(),
89 active_query: None,
90 }
91 }
92
93 fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
94 cx.add_model(|cx| Self {
95 project: self.project.clone(),
96 excerpts: self
97 .excerpts
98 .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
99 pending_search: Default::default(),
100 match_ranges: self.match_ranges.clone(),
101 active_query: self.active_query.clone(),
102 })
103 }
104
105 fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
106 let search = self
107 .project
108 .update(cx, |project, cx| project.search(query.clone(), cx));
109 self.active_query = Some(query);
110 self.match_ranges.clear();
111 self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
112 let matches = search.await.log_err()?;
113 if let Some(this) = this.upgrade(&cx) {
114 this.update(&mut cx, |this, cx| {
115 this.match_ranges.clear();
116 let mut matches = matches.into_iter().collect::<Vec<_>>();
117 matches
118 .sort_by_key(|(buffer, _)| buffer.read(cx).file().map(|file| file.path()));
119 this.excerpts.update(cx, |excerpts, cx| {
120 excerpts.clear(cx);
121 for (buffer, buffer_matches) in matches {
122 let ranges_to_highlight = excerpts.push_excerpts_with_context_lines(
123 buffer,
124 buffer_matches.clone(),
125 1,
126 cx,
127 );
128 this.match_ranges.extend(ranges_to_highlight);
129 }
130 });
131 this.pending_search.take();
132 cx.notify();
133 });
134 }
135 None
136 }));
137 cx.notify();
138 }
139}
140
141pub enum ViewEvent {
142 UpdateTab,
143}
144
145impl Entity for ProjectSearchView {
146 type Event = ViewEvent;
147}
148
149impl View for ProjectSearchView {
150 fn ui_name() -> &'static str {
151 "ProjectSearchView"
152 }
153
154 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
155 let model = &self.model.read(cx);
156 if model.match_ranges.is_empty() {
157 let theme = &cx.global::<Settings>().theme;
158 let text = if self.query_editor.read(cx).text(cx).is_empty() {
159 ""
160 } else if model.pending_search.is_some() {
161 "Searching..."
162 } else {
163 "No results"
164 };
165 Label::new(text.to_string(), theme.search.results_status.clone())
166 .aligned()
167 .contained()
168 .with_background_color(theme.editor.background)
169 .flex(1., true)
170 .boxed()
171 } else {
172 ChildView::new(&self.results_editor).flex(1., true).boxed()
173 }
174 }
175
176 fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
177 let handle = cx.weak_handle();
178 cx.update_global(|state: &mut ActiveSearches, cx| {
179 state
180 .0
181 .insert(self.model.read(cx).project.downgrade(), handle)
182 });
183
184 if self.model.read(cx).match_ranges.is_empty() {
185 cx.focus(&self.query_editor);
186 } else {
187 self.focus_results_editor(cx);
188 }
189 }
190}
191
192impl Item for ProjectSearchView {
193 fn act_as_type(
194 &self,
195 type_id: TypeId,
196 self_handle: &ViewHandle<Self>,
197 _: &gpui::AppContext,
198 ) -> Option<gpui::AnyViewHandle> {
199 if type_id == TypeId::of::<Self>() {
200 Some(self_handle.into())
201 } else if type_id == TypeId::of::<Editor>() {
202 Some((&self.results_editor).into())
203 } else {
204 None
205 }
206 }
207
208 fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
209 self.results_editor
210 .update(cx, |editor, cx| editor.deactivated(cx));
211 }
212
213 fn tab_content(&self, tab_theme: &theme::Tab, cx: &gpui::AppContext) -> ElementBox {
214 let settings = cx.global::<Settings>();
215 let search_theme = &settings.theme.search;
216 Flex::row()
217 .with_child(
218 Svg::new("icons/magnifier.svg")
219 .with_color(tab_theme.label.text.color)
220 .constrained()
221 .with_width(search_theme.tab_icon_width)
222 .aligned()
223 .boxed(),
224 )
225 .with_children(self.model.read(cx).active_query.as_ref().map(|query| {
226 let query_text = if query.as_str().len() > MAX_TAB_TITLE_LEN {
227 query.as_str()[..MAX_TAB_TITLE_LEN].to_string() + "…"
228 } else {
229 query.as_str().to_string()
230 };
231
232 Label::new(query_text, tab_theme.label.clone())
233 .aligned()
234 .contained()
235 .with_margin_left(search_theme.tab_icon_spacing)
236 .boxed()
237 }))
238 .boxed()
239 }
240
241 fn project_path(&self, _: &gpui::AppContext) -> Option<project::ProjectPath> {
242 None
243 }
244
245 fn project_entry_id(&self, _: &AppContext) -> Option<project::ProjectEntryId> {
246 None
247 }
248
249 fn can_save(&self, _: &gpui::AppContext) -> bool {
250 true
251 }
252
253 fn is_dirty(&self, cx: &AppContext) -> bool {
254 self.results_editor.read(cx).is_dirty(cx)
255 }
256
257 fn has_conflict(&self, cx: &AppContext) -> bool {
258 self.results_editor.read(cx).has_conflict(cx)
259 }
260
261 fn save(
262 &mut self,
263 project: ModelHandle<Project>,
264 cx: &mut ViewContext<Self>,
265 ) -> Task<anyhow::Result<()>> {
266 self.results_editor
267 .update(cx, |editor, cx| editor.save(project, cx))
268 }
269
270 fn can_save_as(&self, _: &gpui::AppContext) -> bool {
271 false
272 }
273
274 fn save_as(
275 &mut self,
276 _: ModelHandle<Project>,
277 _: PathBuf,
278 _: &mut ViewContext<Self>,
279 ) -> Task<anyhow::Result<()>> {
280 unreachable!("save_as should not have been called")
281 }
282
283 fn reload(
284 &mut self,
285 project: ModelHandle<Project>,
286 cx: &mut ViewContext<Self>,
287 ) -> Task<anyhow::Result<()>> {
288 self.results_editor
289 .update(cx, |editor, cx| editor.reload(project, cx))
290 }
291
292 fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
293 where
294 Self: Sized,
295 {
296 let model = self.model.update(cx, |model, cx| model.clone(cx));
297 Some(Self::new(model, cx))
298 }
299
300 fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
301 self.results_editor.update(cx, |editor, _| {
302 editor.set_nav_history(Some(nav_history));
303 });
304 }
305
306 fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
307 self.results_editor
308 .update(cx, |editor, cx| editor.navigate(data, cx))
309 }
310
311 fn should_update_tab_on_event(event: &ViewEvent) -> bool {
312 matches!(event, ViewEvent::UpdateTab)
313 }
314}
315
316impl ProjectSearchView {
317 fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
318 let project;
319 let excerpts;
320 let mut query_text = String::new();
321 let mut regex = false;
322 let mut case_sensitive = false;
323 let mut whole_word = false;
324
325 {
326 let model = model.read(cx);
327 project = model.project.clone();
328 excerpts = model.excerpts.clone();
329 if let Some(active_query) = model.active_query.as_ref() {
330 query_text = active_query.as_str().to_string();
331 regex = active_query.is_regex();
332 case_sensitive = active_query.case_sensitive();
333 whole_word = active_query.whole_word();
334 }
335 }
336 cx.observe(&model, |this, _, cx| this.model_changed(true, cx))
337 .detach();
338
339 let query_editor = cx.add_view(|cx| {
340 let mut editor =
341 Editor::single_line(Some(|theme| theme.search.editor.input.clone()), cx);
342 editor.set_text(query_text, cx);
343 editor
344 });
345
346 let results_editor = cx.add_view(|cx| {
347 let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
348 editor.set_searchable(false);
349 editor
350 });
351 cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
352 .detach();
353 cx.subscribe(&results_editor, |this, _, event, cx| {
354 if matches!(event, editor::Event::SelectionsChanged { .. }) {
355 this.update_match_index(cx);
356 }
357 })
358 .detach();
359
360 let mut this = ProjectSearchView {
361 model,
362 query_editor,
363 results_editor,
364 case_sensitive,
365 whole_word,
366 regex,
367 query_contains_error: false,
368 active_match_index: None,
369 };
370 this.model_changed(false, cx);
371 this
372 }
373
374 // Re-activate the most recently activated search or the most recent if it has been closed.
375 // If no search exists in the workspace, create a new one.
376 fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
377 // Clean up entries for dropped projects
378 cx.update_global(|state: &mut ActiveSearches, cx| {
379 state.0.retain(|project, _| project.is_upgradable(cx))
380 });
381
382 let active_search = cx
383 .global::<ActiveSearches>()
384 .0
385 .get(&workspace.project().downgrade());
386
387 let existing = active_search
388 .and_then(|active_search| {
389 workspace
390 .items_of_type::<ProjectSearchView>(cx)
391 .find(|search| search == active_search)
392 })
393 .or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
394
395 if let Some(existing) = existing {
396 workspace.activate_item(&existing, cx);
397 } else {
398 let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
399 workspace.add_item(
400 Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
401 cx,
402 );
403 }
404 }
405
406 fn search(&mut self, cx: &mut ViewContext<Self>) {
407 if let Some(query) = self.build_search_query(cx) {
408 self.model.update(cx, |model, cx| model.search(query, cx));
409 }
410 }
411
412 fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
413 let text = self.query_editor.read(cx).text(cx);
414 if self.regex {
415 match SearchQuery::regex(text, self.whole_word, self.case_sensitive) {
416 Ok(query) => Some(query),
417 Err(_) => {
418 self.query_contains_error = true;
419 cx.notify();
420 None
421 }
422 }
423 } else {
424 Some(SearchQuery::text(
425 text,
426 self.whole_word,
427 self.case_sensitive,
428 ))
429 }
430 }
431
432 fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
433 if let Some(index) = self.active_match_index {
434 let model = self.model.read(cx);
435 let results_editor = self.results_editor.read(cx);
436 let new_index = match_index_for_direction(
437 &model.match_ranges,
438 &results_editor.newest_anchor_selection().head(),
439 index,
440 direction,
441 &results_editor.buffer().read(cx).read(cx),
442 );
443 let range_to_select = model.match_ranges[new_index].clone();
444 self.results_editor.update(cx, |editor, cx| {
445 editor.unfold_ranges([range_to_select.clone()], false, cx);
446 editor.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
447 });
448 }
449 }
450
451 fn focus_query_editor(&self, cx: &mut ViewContext<Self>) {
452 self.query_editor.update(cx, |query_editor, cx| {
453 query_editor.select_all(&SelectAll, cx);
454 });
455 cx.focus(&self.query_editor);
456 }
457
458 fn focus_results_editor(&self, cx: &mut ViewContext<Self>) {
459 self.query_editor.update(cx, |query_editor, cx| {
460 let cursor = query_editor.newest_anchor_selection().head();
461 query_editor.select_ranges([cursor.clone()..cursor], None, cx);
462 });
463 cx.focus(&self.results_editor);
464 }
465
466 fn model_changed(&mut self, reset_selections: bool, cx: &mut ViewContext<Self>) {
467 let match_ranges = self.model.read(cx).match_ranges.clone();
468 if match_ranges.is_empty() {
469 self.active_match_index = None;
470 } else {
471 self.results_editor.update(cx, |editor, cx| {
472 if reset_selections {
473 editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx);
474 }
475 let theme = &cx.global::<Settings>().theme.search;
476 editor.highlight_background::<Self>(match_ranges, theme.match_background, cx);
477 });
478 if self.query_editor.is_focused(cx) {
479 self.focus_results_editor(cx);
480 }
481 }
482
483 cx.emit(ViewEvent::UpdateTab);
484 cx.notify();
485 }
486
487 fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
488 let results_editor = self.results_editor.read(cx);
489 let new_index = active_match_index(
490 &self.model.read(cx).match_ranges,
491 &results_editor.newest_anchor_selection().head(),
492 &results_editor.buffer().read(cx).read(cx),
493 );
494 if self.active_match_index != new_index {
495 self.active_match_index = new_index;
496 cx.notify();
497 }
498 }
499
500 pub fn has_matches(&self) -> bool {
501 self.active_match_index.is_some()
502 }
503}
504
505impl ProjectSearchBar {
506 pub fn new() -> Self {
507 Self {
508 active_project_search: Default::default(),
509 subscription: Default::default(),
510 }
511 }
512
513 fn search(&mut self, _: &Search, cx: &mut ViewContext<Self>) {
514 if let Some(search_view) = self.active_project_search.as_ref() {
515 search_view.update(cx, |search_view, cx| search_view.search(cx));
516 }
517 }
518
519 fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
520 if let Some(search_view) = workspace
521 .active_item(cx)
522 .and_then(|item| item.downcast::<ProjectSearchView>())
523 {
524 let new_query = search_view.update(cx, |search_view, cx| {
525 let new_query = search_view.build_search_query(cx);
526 if new_query.is_some() {
527 if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
528 search_view.query_editor.update(cx, |editor, cx| {
529 editor.set_text(old_query.as_str(), cx);
530 });
531 search_view.regex = old_query.is_regex();
532 search_view.whole_word = old_query.whole_word();
533 search_view.case_sensitive = old_query.case_sensitive();
534 }
535 }
536 new_query
537 });
538 if let Some(new_query) = new_query {
539 let model = cx.add_model(|cx| {
540 let mut model = ProjectSearch::new(workspace.project().clone(), cx);
541 model.search(new_query, cx);
542 model
543 });
544 workspace.add_item(
545 Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
546 cx,
547 );
548 }
549 }
550 }
551
552 fn select_match(
553 pane: &mut Pane,
554 &SelectMatch(direction): &SelectMatch,
555 cx: &mut ViewContext<Pane>,
556 ) {
557 if let Some(search_view) = pane
558 .active_item()
559 .and_then(|item| item.downcast::<ProjectSearchView>())
560 {
561 search_view.update(cx, |search_view, cx| {
562 search_view.select_match(direction, cx);
563 });
564 } else {
565 cx.propagate_action();
566 }
567 }
568
569 fn toggle_focus(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext<Pane>) {
570 if let Some(search_view) = pane
571 .active_item()
572 .and_then(|item| item.downcast::<ProjectSearchView>())
573 {
574 search_view.update(cx, |search_view, cx| {
575 if search_view.query_editor.is_focused(cx) {
576 if !search_view.model.read(cx).match_ranges.is_empty() {
577 search_view.focus_results_editor(cx);
578 }
579 } else {
580 search_view.focus_query_editor(cx);
581 }
582 });
583 } else {
584 cx.propagate_action();
585 }
586 }
587
588 fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
589 if let Some(search_view) = self.active_project_search.as_ref() {
590 search_view.update(cx, |search_view, cx| {
591 if search_view.query_editor.is_focused(cx) {
592 if !search_view.model.read(cx).match_ranges.is_empty() {
593 search_view.focus_results_editor(cx);
594 }
595 } else {
596 cx.propagate_action();
597 }
598 });
599 } else {
600 cx.propagate_action();
601 }
602 }
603
604 fn toggle_search_option(
605 &mut self,
606 ToggleSearchOption(option): &ToggleSearchOption,
607 cx: &mut ViewContext<Self>,
608 ) {
609 if let Some(search_view) = self.active_project_search.as_ref() {
610 search_view.update(cx, |search_view, cx| {
611 let value = match option {
612 SearchOption::WholeWord => &mut search_view.whole_word,
613 SearchOption::CaseSensitive => &mut search_view.case_sensitive,
614 SearchOption::Regex => &mut search_view.regex,
615 };
616 *value = !*value;
617 search_view.search(cx);
618 });
619 cx.notify();
620 }
621 }
622
623 fn render_nav_button(
624 &self,
625 icon: &str,
626 direction: Direction,
627 cx: &mut RenderContext<Self>,
628 ) -> ElementBox {
629 enum NavButton {}
630 MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, cx| {
631 let theme = &cx.global::<Settings>().theme.search;
632 let style = if state.hovered {
633 &theme.hovered_option_button
634 } else {
635 &theme.option_button
636 };
637 Label::new(icon.to_string(), style.text.clone())
638 .contained()
639 .with_style(style.container)
640 .boxed()
641 })
642 .on_click(move |cx| cx.dispatch_action(SelectMatch(direction)))
643 .with_cursor_style(CursorStyle::PointingHand)
644 .boxed()
645 }
646
647 fn render_option_button(
648 &self,
649 icon: &str,
650 option: SearchOption,
651 cx: &mut RenderContext<Self>,
652 ) -> ElementBox {
653 let is_active = self.is_option_enabled(option, cx);
654 MouseEventHandler::new::<ProjectSearchBar, _, _>(option as usize, cx, |state, cx| {
655 let theme = &cx.global::<Settings>().theme.search;
656 let style = match (is_active, state.hovered) {
657 (false, false) => &theme.option_button,
658 (false, true) => &theme.hovered_option_button,
659 (true, false) => &theme.active_option_button,
660 (true, true) => &theme.active_hovered_option_button,
661 };
662 Label::new(icon.to_string(), style.text.clone())
663 .contained()
664 .with_style(style.container)
665 .boxed()
666 })
667 .on_click(move |cx| cx.dispatch_action(ToggleSearchOption(option)))
668 .with_cursor_style(CursorStyle::PointingHand)
669 .boxed()
670 }
671
672 fn is_option_enabled(&self, option: SearchOption, cx: &AppContext) -> bool {
673 if let Some(search) = self.active_project_search.as_ref() {
674 let search = search.read(cx);
675 match option {
676 SearchOption::WholeWord => search.whole_word,
677 SearchOption::CaseSensitive => search.case_sensitive,
678 SearchOption::Regex => search.regex,
679 }
680 } else {
681 false
682 }
683 }
684}
685
686impl Entity for ProjectSearchBar {
687 type Event = ();
688}
689
690impl View for ProjectSearchBar {
691 fn ui_name() -> &'static str {
692 "ProjectSearchBar"
693 }
694
695 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
696 if let Some(search) = self.active_project_search.as_ref() {
697 let search = search.read(cx);
698 let theme = cx.global::<Settings>().theme.clone();
699 let editor_container = if search.query_contains_error {
700 theme.search.invalid_editor
701 } else {
702 theme.search.editor.input.container
703 };
704 Flex::row()
705 .with_child(
706 Flex::row()
707 .with_child(
708 ChildView::new(&search.query_editor)
709 .aligned()
710 .left()
711 .flex(1., true)
712 .boxed(),
713 )
714 .with_children(search.active_match_index.map(|match_ix| {
715 Label::new(
716 format!(
717 "{}/{}",
718 match_ix + 1,
719 search.model.read(cx).match_ranges.len()
720 ),
721 theme.search.match_index.text.clone(),
722 )
723 .contained()
724 .with_style(theme.search.match_index.container)
725 .aligned()
726 .boxed()
727 }))
728 .contained()
729 .with_style(editor_container)
730 .aligned()
731 .constrained()
732 .with_min_width(theme.search.editor.min_width)
733 .with_max_width(theme.search.editor.max_width)
734 .flex(1., false)
735 .boxed(),
736 )
737 .with_child(
738 Flex::row()
739 .with_child(self.render_nav_button("<", Direction::Prev, cx))
740 .with_child(self.render_nav_button(">", Direction::Next, cx))
741 .aligned()
742 .boxed(),
743 )
744 .with_child(
745 Flex::row()
746 .with_child(self.render_option_button(
747 "Case",
748 SearchOption::CaseSensitive,
749 cx,
750 ))
751 .with_child(self.render_option_button("Word", SearchOption::WholeWord, cx))
752 .with_child(self.render_option_button("Regex", SearchOption::Regex, cx))
753 .contained()
754 .with_style(theme.search.option_button_group)
755 .aligned()
756 .boxed(),
757 )
758 .contained()
759 .with_style(theme.search.container)
760 .aligned()
761 .left()
762 .named("project search")
763 } else {
764 Empty::new().boxed()
765 }
766 }
767}
768
769impl ToolbarItemView for ProjectSearchBar {
770 fn set_active_pane_item(
771 &mut self,
772 active_pane_item: Option<&dyn workspace::ItemHandle>,
773 cx: &mut ViewContext<Self>,
774 ) -> ToolbarItemLocation {
775 cx.notify();
776 self.subscription = None;
777 self.active_project_search = None;
778 if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
779 self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
780 self.active_project_search = Some(search);
781 ToolbarItemLocation::PrimaryLeft {
782 flex: Some((1., false)),
783 }
784 } else {
785 ToolbarItemLocation::Hidden
786 }
787 }
788}
789
790#[cfg(test)]
791mod tests {
792 use super::*;
793 use editor::DisplayPoint;
794 use gpui::{color::Color, TestAppContext};
795 use project::FakeFs;
796 use serde_json::json;
797 use std::sync::Arc;
798
799 #[gpui::test]
800 async fn test_project_search(cx: &mut TestAppContext) {
801 let fonts = cx.font_cache();
802 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
803 theme.search.match_background = Color::red();
804 let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
805 cx.update(|cx| cx.set_global(settings));
806
807 let fs = FakeFs::new(cx.background());
808 fs.insert_tree(
809 "/dir",
810 json!({
811 "one.rs": "const ONE: usize = 1;",
812 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
813 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
814 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
815 }),
816 )
817 .await;
818 let project = Project::test(fs.clone(), cx);
819 let (tree, _) = project
820 .update(cx, |project, cx| {
821 project.find_or_create_local_worktree("/dir", true, cx)
822 })
823 .await
824 .unwrap();
825 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
826 .await;
827
828 let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
829 let search_view = cx.add_view(Default::default(), |cx| {
830 ProjectSearchView::new(search.clone(), cx)
831 });
832
833 search_view.update(cx, |search_view, cx| {
834 search_view
835 .query_editor
836 .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
837 search_view.search(cx);
838 });
839 search_view.next_notification(&cx).await;
840 search_view.update(cx, |search_view, cx| {
841 assert_eq!(
842 search_view
843 .results_editor
844 .update(cx, |editor, cx| editor.display_text(cx)),
845 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
846 );
847 assert_eq!(
848 search_view
849 .results_editor
850 .update(cx, |editor, cx| editor.all_background_highlights(cx)),
851 &[
852 (
853 DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
854 Color::red()
855 ),
856 (
857 DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
858 Color::red()
859 ),
860 (
861 DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
862 Color::red()
863 )
864 ]
865 );
866 assert_eq!(search_view.active_match_index, Some(0));
867 assert_eq!(
868 search_view
869 .results_editor
870 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
871 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
872 );
873
874 search_view.select_match(Direction::Next, cx);
875 });
876
877 search_view.update(cx, |search_view, cx| {
878 assert_eq!(search_view.active_match_index, Some(1));
879 assert_eq!(
880 search_view
881 .results_editor
882 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
883 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
884 );
885 search_view.select_match(Direction::Next, cx);
886 });
887
888 search_view.update(cx, |search_view, cx| {
889 assert_eq!(search_view.active_match_index, Some(2));
890 assert_eq!(
891 search_view
892 .results_editor
893 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
894 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
895 );
896 search_view.select_match(Direction::Next, cx);
897 });
898
899 search_view.update(cx, |search_view, cx| {
900 assert_eq!(search_view.active_match_index, Some(0));
901 assert_eq!(
902 search_view
903 .results_editor
904 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
905 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
906 );
907 search_view.select_match(Direction::Prev, cx);
908 });
909
910 search_view.update(cx, |search_view, cx| {
911 assert_eq!(search_view.active_match_index, Some(2));
912 assert_eq!(
913 search_view
914 .results_editor
915 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
916 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
917 );
918 search_view.select_match(Direction::Prev, cx);
919 });
920
921 search_view.update(cx, |search_view, cx| {
922 assert_eq!(search_view.active_match_index, Some(1));
923 assert_eq!(
924 search_view
925 .results_editor
926 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
927 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
928 );
929 });
930 }
931}