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