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