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