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