History

Select all matches (#2717)

Closes https://github.com/zed-industries/community/issues/75 Closes https://github.com/zed-industries/community/issues/1749 The PR * changes keybindings for `Editor && mode == auto_height` context: before, `alt-enter` and `alt-shift-enter` added new lines in such editors, including the one from buffer search. New bindings are the same as in `Editor && mode == full` context. * adds `search::SelectAllMatches` action and binds it to `Alt + Enter` by default, to select all matches of a buffer search The behavior mimics VSCode: we do not move the screen even if all selections are out of the visible range (Cmd+G will navigate there) and allow reselecting the results from both pane and search field, as long as the search is not dismissed. Release Notes: - Added `search::SelectAllMatches` (`Alt + Enter` default) action to place carets and select all buffer search results ([#75](https://github.com/zed-industries/community/issues/75), [#1749](https://github.com/zed-industries/community/issues/1749)).

Kirill Bulatov committed 3 years ago

Commit cde5b392 parents 0f54893 b14cd5f12 files · +337 −31Browse files

Unified · Split

12 files
  1. assets/keymaps/default.json +5 −3
  2. crates/editor/src/items.rs +32 −5
  3. crates/editor/src/selections_collection.rs +2 −2
  4. crates/feedback/src/feedback_editor.rs +12 −2
  5. crates/language_tools/src/lsp_log.rs +12 −2
  6. crates/search/src/buffer_search.rs +199 −12
  7. crates/search/src/search.rs +2 −1
  8. crates/terminal/src/terminal.rs +15 −0
  9. crates/terminal_view/src/terminal_view.rs +12 −1
  10. crates/theme/src/theme.rs +1 −0
  11. crates/workspace/src/searchable.rs +16 −3
  12. styles/src/style_tree/search.ts +29 −0
assets/keymaps/default.json+5−3
@@ -194,8 +194,8 @@ · 193 unmodified lines
194 {194 {
195 "context": "Editor && mode == auto_height",195 "context": "Editor && mode == auto_height",
196 "bindings": {196 "bindings": {
197 "alt-enter": "editor::Newline",197 "shift-enter": "editor::Newline",
198 "cmd-alt-enter": "editor::NewlineBelow"198 "cmd-shift-enter": "editor::NewlineBelow"
199 }199 }
200 },200 },
201 {201 {
@@ -221,7 +221,8 @@ · 19 unmodified lines
221 "escape": "buffer_search::Dismiss",221 "escape": "buffer_search::Dismiss",
222 "tab": "buffer_search::FocusEditor",222 "tab": "buffer_search::FocusEditor",
223 "enter": "search::SelectNextMatch",223 "enter": "search::SelectNextMatch",
224 "shift-enter": "search::SelectPrevMatch"224 "shift-enter": "search::SelectPrevMatch",
225 "alt-enter": "search::SelectAllMatches"
225 }226 }
226 },227 },
227 {228 {
@@ -242,6 +243,7 @@ · 14 unmodified lines
242 "cmd-f": "project_search::ToggleFocus",243 "cmd-f": "project_search::ToggleFocus",
243 "cmd-g": "search::SelectNextMatch",244 "cmd-g": "search::SelectNextMatch",
244 "cmd-shift-g": "search::SelectPrevMatch",245 "cmd-shift-g": "search::SelectPrevMatch",
246 "alt-enter": "search::SelectAllMatches",
245 "alt-cmd-c": "search::ToggleCaseSensitive",247 "alt-cmd-c": "search::ToggleCaseSensitive",
246 "alt-cmd-w": "search::ToggleWholeWord",248 "alt-cmd-w": "search::ToggleWholeWord",
247 "alt-cmd-r": "search::ToggleRegex"249 "alt-cmd-r": "search::ToggleRegex"
crates/editor/src/items.rs+32−5
@@ -887,10 +887,20 @@ pub(crate) enum BufferSearchHighlights {} · 886 unmodified lines
887impl SearchableItem for Editor {887impl SearchableItem for Editor {
888 type Match = Range<Anchor>;888 type Match = Range<Anchor>;
889 889
890 fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {890 fn to_search_event(
891 &mut self,
892 event: &Self::Event,
893 _: &mut ViewContext<Self>,
894 ) -> Option<SearchEvent> {
891 match event {895 match event {
892 Event::BufferEdited => Some(SearchEvent::MatchesInvalidated),896 Event::BufferEdited => Some(SearchEvent::MatchesInvalidated),
893 Event::SelectionsChanged { .. } => Some(SearchEvent::ActiveMatchChanged),897 Event::SelectionsChanged { .. } => {
898 if self.selections.disjoint_anchors().len() == 1 {
899 Some(SearchEvent::ActiveMatchChanged)
900 } else {
901 None
902 }
903 }
894 _ => None,904 _ => None,
895 }905 }
896 }906 }
@@ -941,6 +951,11 @@ impl SearchableItem for Editor { · 44 unmodified lines
941 });951 });
942 }952 }
943 953
954 fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
955 self.unfold_ranges(matches.clone(), false, false, cx);
956 self.change_selections(None, cx, |s| s.select_ranges(matches));
957 }
958
944 fn match_index_for_direction(959 fn match_index_for_direction(
945 &mut self,960 &mut self,
946 matches: &Vec<Range<Anchor>>,961 matches: &Vec<Range<Anchor>>,
@@ -949,8 +964,16 @@ impl SearchableItem for Editor { · 2 unmodified lines
949 cx: &mut ViewContext<Self>,964 cx: &mut ViewContext<Self>,
950 ) -> usize {965 ) -> usize {
951 let buffer = self.buffer().read(cx).snapshot(cx);966 let buffer = self.buffer().read(cx).snapshot(cx);
952 let cursor = self.selections.newest_anchor().head();967 let current_index_position = if self.selections.disjoint_anchors().len() == 1 {
953 if matches[current_index].start.cmp(&cursor, &buffer).is_gt() {968 self.selections.newest_anchor().head()
969 } else {
970 matches[current_index].start
971 };
972 if matches[current_index]
973 .start
974 .cmp(&current_index_position, &buffer)
975 .is_gt()
976 {
954 if direction == Direction::Prev {977 if direction == Direction::Prev {
955 if current_index == 0 {978 if current_index == 0 {
956 current_index = matches.len() - 1;979 current_index = matches.len() - 1;
@@ -958,7 +981,11 @@ impl SearchableItem for Editor { · 1 unmodified line
958 current_index -= 1;981 current_index -= 1;
959 }982 }
960 }983 }
961 } else if matches[current_index].end.cmp(&cursor, &buffer).is_lt() {984 } else if matches[current_index]
985 .end
986 .cmp(&current_index_position, &buffer)
987 .is_lt()
988 {
962 if direction == Direction::Next {989 if direction == Direction::Next {
963 current_index = 0;990 current_index = 0;
964 }991 }
crates/editor/src/selections_collection.rs+2−2
@@ -16,13 +16,13 @@ use crate::{ · 15 unmodified lines
16 Anchor, DisplayPoint, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode, ToOffset,16 Anchor, DisplayPoint, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode, ToOffset,
17};17};
18 18
19#[derive(Clone)]19#[derive(Debug, Clone)]
20pub struct PendingSelection {20pub struct PendingSelection {
21 pub selection: Selection<Anchor>,21 pub selection: Selection<Anchor>,
22 pub mode: SelectMode,22 pub mode: SelectMode,
23}23}
24 24
25#[derive(Clone)]25#[derive(Debug, Clone)]
26pub struct SelectionsCollection {26pub struct SelectionsCollection {
27 display_map: ModelHandle<DisplayMap>,27 display_map: ModelHandle<DisplayMap>,
28 buffer: ModelHandle<MultiBuffer>,28 buffer: ModelHandle<MultiBuffer>,
crates/feedback/src/feedback_editor.rs+12−2
@@ -362,8 +362,13 @@ impl Item for FeedbackEditor { · 361 unmodified lines
362impl SearchableItem for FeedbackEditor {362impl SearchableItem for FeedbackEditor {
363 type Match = Range<Anchor>;363 type Match = Range<Anchor>;
364 364
365 fn to_search_event(event: &Self::Event) -> Option<workspace::searchable::SearchEvent> {365 fn to_search_event(
366 Editor::to_search_event(event)366 &mut self,
367 event: &Self::Event,
368 cx: &mut ViewContext<Self>,
369 ) -> Option<workspace::searchable::SearchEvent> {
370 self.editor
371 .update(cx, |editor, cx| editor.to_search_event(event, cx))
367 }372 }
368 373
369 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {374 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
@@ -391,6 +396,11 @@ impl SearchableItem for FeedbackEditor { · 21 unmodified lines
391 .update(cx, |editor, cx| editor.activate_match(index, matches, cx))396 .update(cx, |editor, cx| editor.activate_match(index, matches, cx))
392 }397 }
393 398
399 fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
400 self.editor
401 .update(cx, |e, cx| e.select_matches(matches, cx))
402 }
403
394 fn find_matches(404 fn find_matches(
395 &mut self,405 &mut self,
396 query: project::search::SearchQuery,406 query: project::search::SearchQuery,
crates/language_tools/src/lsp_log.rs+12−2
@@ -467,8 +467,13 @@ impl Item for LspLogView { · 466 unmodified lines
467impl SearchableItem for LspLogView {467impl SearchableItem for LspLogView {
468 type Match = <Editor as SearchableItem>::Match;468 type Match = <Editor as SearchableItem>::Match;
469 469
470 fn to_search_event(event: &Self::Event) -> Option<workspace::searchable::SearchEvent> {470 fn to_search_event(
471 Editor::to_search_event(event)471 &mut self,
472 event: &Self::Event,
473 cx: &mut ViewContext<Self>,
474 ) -> Option<workspace::searchable::SearchEvent> {
475 self.editor
476 .update(cx, |editor, cx| editor.to_search_event(event, cx))
472 }477 }
473 478
474 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {479 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
@@ -494,6 +499,11 @@ impl SearchableItem for LspLogView { · 19 unmodified lines
494 .update(cx, |e, cx| e.activate_match(index, matches, cx))499 .update(cx, |e, cx| e.activate_match(index, matches, cx))
495 }500 }
496 501
502 fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
503 self.editor
504 .update(cx, |e, cx| e.select_matches(matches, cx))
505 }
506
497 fn find_matches(507 fn find_matches(
498 &mut self,508 &mut self,
499 query: project::search::SearchQuery,509 query: project::search::SearchQuery,
crates/search/src/buffer_search.rs+199−12
@@ -1,6 +1,6 @@
1use crate::{1use crate::{
2 SearchOption, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex,2 SearchOption, SelectAllMatches, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive,
3 ToggleWholeWord,3 ToggleRegex, ToggleWholeWord,
4};4};
5use collections::HashMap;5use collections::HashMap;
6use editor::Editor;6use editor::Editor;
@@ -39,8 +39,10 @@ pub fn init(cx: &mut AppContext) { · 32 unmodified lines
39 cx.add_action(BufferSearchBar::focus_editor);39 cx.add_action(BufferSearchBar::focus_editor);
40 cx.add_action(BufferSearchBar::select_next_match);40 cx.add_action(BufferSearchBar::select_next_match);
41 cx.add_action(BufferSearchBar::select_prev_match);41 cx.add_action(BufferSearchBar::select_prev_match);
42 cx.add_action(BufferSearchBar::select_all_matches);
42 cx.add_action(BufferSearchBar::select_next_match_on_pane);43 cx.add_action(BufferSearchBar::select_next_match_on_pane);
43 cx.add_action(BufferSearchBar::select_prev_match_on_pane);44 cx.add_action(BufferSearchBar::select_prev_match_on_pane);
45 cx.add_action(BufferSearchBar::select_all_matches_on_pane);
44 cx.add_action(BufferSearchBar::handle_editor_cancel);46 cx.add_action(BufferSearchBar::handle_editor_cancel);
45 add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);47 add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);
46 add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);48 add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);
@@ -66,7 +68,7 @@ pub struct BufferSearchBar { · 19 unmodified lines
66 active_searchable_item: Option<Box<dyn SearchableItemHandle>>,68 active_searchable_item: Option<Box<dyn SearchableItemHandle>>,
67 active_match_index: Option<usize>,69 active_match_index: Option<usize>,
68 active_searchable_item_subscription: Option<Subscription>,70 active_searchable_item_subscription: Option<Subscription>,
69 seachable_items_with_matches:71 searchable_items_with_matches:
70 HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>,72 HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>,
71 pending_search: Option<Task<()>>,73 pending_search: Option<Task<()>>,
72 case_sensitive: bool,74 case_sensitive: bool,
@@ -118,7 +120,7 @@ impl View for BufferSearchBar { · 45 unmodified lines
118 .with_children(self.active_searchable_item.as_ref().and_then(120 .with_children(self.active_searchable_item.as_ref().and_then(
119 |searchable_item| {121 |searchable_item| {
120 let matches = self122 let matches = self
121 .seachable_items_with_matches123 .searchable_items_with_matches
122 .get(&searchable_item.downgrade())?;124 .get(&searchable_item.downgrade())?;
123 let message = if let Some(match_ix) = self.active_match_index {125 let message = if let Some(match_ix) = self.active_match_index {
124 format!("{}/{}", match_ix + 1, matches.len())126 format!("{}/{}", match_ix + 1, matches.len())
@@ -146,6 +148,7 @@ impl View for BufferSearchBar { · 21 unmodified lines
146 Flex::row()148 Flex::row()
147 .with_child(self.render_nav_button("<", Direction::Prev, cx))149 .with_child(self.render_nav_button("<", Direction::Prev, cx))
148 .with_child(self.render_nav_button(">", Direction::Next, cx))150 .with_child(self.render_nav_button(">", Direction::Next, cx))
151 .with_child(self.render_action_button("Select All", cx))
149 .aligned(),152 .aligned(),
150 )153 )
151 .with_child(154 .with_child(
@@ -249,7 +252,7 @@ impl BufferSearchBar { · 97 unmodified lines
249 active_searchable_item: None,252 active_searchable_item: None,
250 active_searchable_item_subscription: None,253 active_searchable_item_subscription: None,
251 active_match_index: None,254 active_match_index: None,
252 seachable_items_with_matches: Default::default(),255 searchable_items_with_matches: Default::default(),
253 case_sensitive: false,256 case_sensitive: false,
254 whole_word: false,257 whole_word: false,
255 regex: false,258 regex: false,
@@ -265,7 +268,7 @@ impl BufferSearchBar { · 9 unmodified lines
265 268
266 pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext<Self>) {269 pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext<Self>) {
267 self.dismissed = true;270 self.dismissed = true;
268 for searchable_item in self.seachable_items_with_matches.keys() {271 for searchable_item in self.searchable_items_with_matches.keys() {
269 if let Some(searchable_item) =272 if let Some(searchable_item) =
270 WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx)273 WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx)
271 {274 {
@@ -401,6 +404,37 @@ impl BufferSearchBar { · 129 unmodified lines
401 .into_any()404 .into_any()
402 }405 }
403 406
407 fn render_action_button(
408 &self,
409 icon: &'static str,
410 cx: &mut ViewContext<Self>,
411 ) -> AnyElement<Self> {
412 let tooltip = "Select All Matches";
413 let tooltip_style = theme::current(cx).tooltip.clone();
414 let action_type_id = 0_usize;
415
416 enum ActionButton {}
417 MouseEventHandler::<ActionButton, _>::new(action_type_id, cx, |state, cx| {
418 let theme = theme::current(cx);
419 let style = theme.search.action_button.style_for(state);
420 Label::new(icon, style.text.clone())
421 .contained()
422 .with_style(style.container)
423 })
424 .on_click(MouseButton::Left, move |_, this, cx| {
425 this.select_all_matches(&SelectAllMatches, cx)
426 })
427 .with_cursor_style(CursorStyle::PointingHand)
428 .with_tooltip::<ActionButton>(
429 action_type_id,
430 tooltip.to_string(),
431 Some(Box::new(SelectAllMatches)),
432 tooltip_style,
433 cx,
434 )
435 .into_any()
436 }
437
404 fn render_close_button(438 fn render_close_button(
405 &self,439 &self,
406 theme: &theme::Search,440 theme: &theme::Search,
@@ -488,11 +522,25 @@ impl BufferSearchBar { · 81 unmodified lines
488 self.select_match(Direction::Prev, cx);522 self.select_match(Direction::Prev, cx);
489 }523 }
490 524
525 fn select_all_matches(&mut self, _: &SelectAllMatches, cx: &mut ViewContext<Self>) {
526 if !self.dismissed {
527 if let Some(searchable_item) = self.active_searchable_item.as_ref() {
528 if let Some(matches) = self
529 .searchable_items_with_matches
530 .get(&searchable_item.downgrade())
531 {
532 searchable_item.select_matches(matches, cx);
533 self.focus_editor(&FocusEditor, cx);
534 }
535 }
536 }
537 }
538
491 pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {539 pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
492 if let Some(index) = self.active_match_index {540 if let Some(index) = self.active_match_index {
493 if let Some(searchable_item) = self.active_searchable_item.as_ref() {541 if let Some(searchable_item) = self.active_searchable_item.as_ref() {
494 if let Some(matches) = self542 if let Some(matches) = self
495 .seachable_items_with_matches543 .searchable_items_with_matches
496 .get(&searchable_item.downgrade())544 .get(&searchable_item.downgrade())
497 {545 {
498 let new_match_index =546 let new_match_index =
@@ -524,6 +572,16 @@ impl BufferSearchBar { · 25 unmodified lines
524 }572 }
525 }573 }
526 574
575 fn select_all_matches_on_pane(
576 pane: &mut Pane,
577 action: &SelectAllMatches,
578 cx: &mut ViewContext<Pane>,
579 ) {
580 if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
581 search_bar.update(cx, |bar, cx| bar.select_all_matches(action, cx));
582 }
583 }
584
527 fn on_query_editor_event(585 fn on_query_editor_event(
528 &mut self,586 &mut self,
529 _: ViewHandle<Editor>,587 _: ViewHandle<Editor>,
@@ -547,7 +605,7 @@ impl BufferSearchBar { · 17 unmodified lines
547 605
548 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {606 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
549 let mut active_item_matches = None;607 let mut active_item_matches = None;
550 for (searchable_item, matches) in self.seachable_items_with_matches.drain() {608 for (searchable_item, matches) in self.searchable_items_with_matches.drain() {
551 if let Some(searchable_item) =609 if let Some(searchable_item) =
552 WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx)610 WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx)
553 {611 {
@@ -559,7 +617,7 @@ impl BufferSearchBar { · 5 unmodified lines
559 }617 }
560 }618 }
561 619
562 self.seachable_items_with_matches620 self.searchable_items_with_matches
563 .extend(active_item_matches);621 .extend(active_item_matches);
564 }622 }
565 623
@@ -605,13 +663,13 @@ impl BufferSearchBar { · 39 unmodified lines
605 if let Some(active_searchable_item) =663 if let Some(active_searchable_item) =
606 WeakSearchableItemHandle::upgrade(active_searchable_item.as_ref(), cx)664 WeakSearchableItemHandle::upgrade(active_searchable_item.as_ref(), cx)
607 {665 {
608 this.seachable_items_with_matches666 this.searchable_items_with_matches
609 .insert(active_searchable_item.downgrade(), matches);667 .insert(active_searchable_item.downgrade(), matches);
610 668
611 this.update_match_index(cx);669 this.update_match_index(cx);
612 if !this.dismissed {670 if !this.dismissed {
613 let matches = this671 let matches = this
614 .seachable_items_with_matches672 .searchable_items_with_matches
615 .get(&active_searchable_item.downgrade())673 .get(&active_searchable_item.downgrade())
616 .unwrap();674 .unwrap();
617 active_searchable_item.update_matches(matches, cx);675 active_searchable_item.update_matches(matches, cx);
@@ -637,7 +695,7 @@ impl BufferSearchBar { · 19 unmodified lines
637 .as_ref()695 .as_ref()
638 .and_then(|searchable_item| {696 .and_then(|searchable_item| {
639 let matches = self697 let matches = self
640 .seachable_items_with_matches698 .searchable_items_with_matches
641 .get(&searchable_item.downgrade())?;699 .get(&searchable_item.downgrade())?;
642 searchable_item.active_match_index(matches, cx)700 searchable_item.active_match_index(matches, cx)
643 });701 });
@@ -966,4 +1024,133 @@ mod tests { · 322 unmodified lines
966 assert_eq!(search_bar.active_match_index, Some(2));1024 assert_eq!(search_bar.active_match_index, Some(2));
967 });1025 });
968 }1026 }
1027
1028 #[gpui::test]
1029 async fn test_search_select_all_matches(cx: &mut TestAppContext) {
1030 crate::project_search::tests::init_test(cx);
1031
1032 let buffer_text = r#"
1033 A regular expression (shortened as regex or regexp;[1] also referred to as
1034 rational expression[2][3]) is a sequence of characters that specifies a search
1035 pattern in text. Usually such patterns are used by string-searching algorithms
1036 for "find" or "find and replace" operations on strings, or for input validation.
1037 "#
1038 .unindent();
1039 let expected_query_matches_count = buffer_text
1040 .chars()
1041 .filter(|c| c.to_ascii_lowercase() == 'a')
1042 .count();
1043 assert!(
1044 expected_query_matches_count > 1,
1045 "Should pick a query with multiple results"
1046 );
1047 let buffer = cx.add_model(|cx| Buffer::new(0, buffer_text, cx));
1048 let (window_id, _root_view) = cx.add_window(|_| EmptyView);
1049
1050 let editor = cx.add_view(window_id, |cx| Editor::for_buffer(buffer.clone(), None, cx));
1051
1052 let search_bar = cx.add_view(window_id, |cx| {
1053 let mut search_bar = BufferSearchBar::new(cx);
1054 search_bar.set_active_pane_item(Some(&editor), cx);
1055 search_bar.show(false, true, cx);
1056 search_bar
1057 });
1058
1059 search_bar.update(cx, |search_bar, cx| {
1060 search_bar.set_query("a", cx);
1061 });
1062
1063 editor.next_notification(cx).await;
1064 let initial_selections = editor.update(cx, |editor, cx| {
1065 let initial_selections = editor.selections.display_ranges(cx);
1066 assert_eq!(
1067 initial_selections.len(), 1,
1068 "Expected to have only one selection before adding carets to all matches, but got: {initial_selections:?}",
1069 );
1070 initial_selections
1071 });
1072 search_bar.update(cx, |search_bar, _| {
1073 assert_eq!(search_bar.active_match_index, Some(0));
1074 });
1075
1076 search_bar.update(cx, |search_bar, cx| {
1077 search_bar.select_all_matches(&SelectAllMatches, cx);
1078 let all_selections =
1079 editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
1080 assert_eq!(
1081 all_selections.len(),
1082 expected_query_matches_count,
1083 "Should select all `a` characters in the buffer, but got: {all_selections:?}"
1084 );
1085 });
1086 search_bar.update(cx, |search_bar, _| {
1087 assert_eq!(
1088 search_bar.active_match_index,
1089 Some(0),
1090 "Match index should not change after selecting all matches"
1091 );
1092 });
1093
1094 search_bar.update(cx, |search_bar, cx| {
1095 search_bar.select_next_match(&SelectNextMatch, cx);
1096 let all_selections =
1097 editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
1098 assert_eq!(
1099 all_selections.len(),
1100 1,
1101 "On next match, should deselect items and select the next match"
1102 );
1103 assert_ne!(
1104 all_selections, initial_selections,
1105 "Next match should be different from the first selection"
1106 );
1107 });
1108 search_bar.update(cx, |search_bar, _| {
1109 assert_eq!(
1110 search_bar.active_match_index,
1111 Some(1),
1112 "Match index should be updated to the next one"
1113 );
1114 });
1115
1116 search_bar.update(cx, |search_bar, cx| {
1117 search_bar.select_all_matches(&SelectAllMatches, cx);
1118 let all_selections =
1119 editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
1120 assert_eq!(
1121 all_selections.len(),
1122 expected_query_matches_count,
1123 "Should select all `a` characters in the buffer, but got: {all_selections:?}"
1124 );
1125 });
1126 search_bar.update(cx, |search_bar, _| {
1127 assert_eq!(
1128 search_bar.active_match_index,
1129 Some(1),
1130 "Match index should not change after selecting all matches"
1131 );
1132 });
1133
1134 search_bar.update(cx, |search_bar, cx| {
1135 search_bar.select_prev_match(&SelectPrevMatch, cx);
1136 let all_selections =
1137 editor.update(cx, |editor, cx| editor.selections.display_ranges(cx));
1138 assert_eq!(
1139 all_selections.len(),
1140 1,
1141 "On previous match, should deselect items and select the previous item"
1142 );
1143 assert_eq!(
1144 all_selections, initial_selections,
1145 "Previous match should be the same as the first selection"
1146 );
1147 });
1148 search_bar.update(cx, |search_bar, _| {
1149 assert_eq!(
1150 search_bar.active_match_index,
1151 Some(0),
1152 "Match index should be updated to the previous one"
1153 );
1154 });
1155 }
969}1156}
crates/search/src/search.rs+2−1
@@ -17,7 +17,8 @@ actions!( · 16 unmodified lines
17 ToggleCaseSensitive,17 ToggleCaseSensitive,
18 ToggleRegex,18 ToggleRegex,
19 SelectNextMatch,19 SelectNextMatch,
20 SelectPrevMatch20 SelectPrevMatch,
21 SelectAllMatches,
21 ]22 ]
22);23);
23 24
crates/terminal/src/terminal.rs+15−0
@@ -908,6 +908,21 @@ impl Terminal { · 907 unmodified lines
908 }908 }
909 }909 }
910 910
911 pub fn select_matches(&mut self, matches: Vec<RangeInclusive<Point>>) {
912 let matches_to_select = self
913 .matches
914 .iter()
915 .filter(|self_match| matches.contains(self_match))
916 .cloned()
917 .collect::<Vec<_>>();
918 for match_to_select in matches_to_select {
919 self.set_selection(Some((
920 make_selection(&match_to_select),
921 *match_to_select.end(),
922 )));
923 }
924 }
925
911 fn set_selection(&mut self, selection: Option<(Selection, Point)>) {926 fn set_selection(&mut self, selection: Option<(Selection, Point)>) {
912 self.events927 self.events
913 .push_back(InternalEvent::SetSelection(selection));928 .push_back(InternalEvent::SetSelection(selection));
crates/terminal_view/src/terminal_view.rs+12−1
@@ -647,7 +647,11 @@ impl SearchableItem for TerminalView { · 646 unmodified lines
647 }647 }
648 648
649 /// Convert events raised by this item into search-relevant events (if applicable)649 /// Convert events raised by this item into search-relevant events (if applicable)
650 fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {650 fn to_search_event(
651 &mut self,
652 event: &Self::Event,
653 _: &mut ViewContext<Self>,
654 ) -> Option<SearchEvent> {
651 match event {655 match event {
652 Event::Wakeup => Some(SearchEvent::MatchesInvalidated),656 Event::Wakeup => Some(SearchEvent::MatchesInvalidated),
653 Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged),657 Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged),
@@ -682,6 +686,13 @@ impl SearchableItem for TerminalView { · 28 unmodified lines
682 cx.notify();686 cx.notify();
683 }687 }
684 688
689 /// Add selections for all matches given.
690 fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
691 self.terminal()
692 .update(cx, |term, _| term.select_matches(matches));
693 cx.notify();
694 }
695
685 /// Get all of the matches for this query, should be done on the background696 /// Get all of the matches for this query, should be done on the background
686 fn find_matches(697 fn find_matches(
687 &mut self,698 &mut self,
crates/theme/src/theme.rs+1−0
@@ -379,6 +379,7 @@ pub struct Search { · 378 unmodified lines
379 pub invalid_include_exclude_editor: ContainerStyle,379 pub invalid_include_exclude_editor: ContainerStyle,
380 pub include_exclude_inputs: ContainedText,380 pub include_exclude_inputs: ContainedText,
381 pub option_button: Toggleable<Interactive<ContainedText>>,381 pub option_button: Toggleable<Interactive<ContainedText>>,
382 pub action_button: Interactive<ContainedText>,
382 pub match_background: Color,383 pub match_background: Color,
383 pub match_index: ContainedText,384 pub match_index: ContainedText,
384 pub results_status: TextStyle,385 pub results_status: TextStyle,
crates/workspace/src/searchable.rs+16−3
@@ -37,7 +37,11 @@ pub trait SearchableItem: Item { · 36 unmodified lines
37 regex: true,37 regex: true,
38 }38 }
39 }39 }
40 fn to_search_event(event: &Self::Event) -> Option<SearchEvent>;40 fn to_search_event(
41 &mut self,
42 event: &Self::Event,
43 cx: &mut ViewContext<Self>,
44 ) -> Option<SearchEvent>;
41 fn clear_matches(&mut self, cx: &mut ViewContext<Self>);45 fn clear_matches(&mut self, cx: &mut ViewContext<Self>);
42 fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>);46 fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>);
43 fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String;47 fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String;
@@ -47,6 +51,7 @@ pub trait SearchableItem: Item { · 3 unmodified lines
47 matches: Vec<Self::Match>,51 matches: Vec<Self::Match>,
48 cx: &mut ViewContext<Self>,52 cx: &mut ViewContext<Self>,
49 );53 );
54 fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>);
50 fn match_index_for_direction(55 fn match_index_for_direction(
51 &mut self,56 &mut self,
52 matches: &Vec<Self::Match>,57 matches: &Vec<Self::Match>,
@@ -102,6 +107,7 @@ pub trait SearchableItemHandle: ItemHandle { · 49 unmodified lines
102 matches: &Vec<Box<dyn Any + Send>>,107 matches: &Vec<Box<dyn Any + Send>>,
103 cx: &mut WindowContext,108 cx: &mut WindowContext,
104 );109 );
110 fn select_matches(&self, matches: &Vec<Box<dyn Any + Send>>, cx: &mut WindowContext);
105 fn match_index_for_direction(111 fn match_index_for_direction(
106 &self,112 &self,
107 matches: &Vec<Box<dyn Any + Send>>,113 matches: &Vec<Box<dyn Any + Send>>,
@@ -139,8 +145,9 @@ impl<T: SearchableItem> SearchableItemHandle for ViewHandle<T> { · 31 unmodified lines
139 cx: &mut WindowContext,145 cx: &mut WindowContext,
140 handler: Box<dyn Fn(SearchEvent, &mut WindowContext)>,146 handler: Box<dyn Fn(SearchEvent, &mut WindowContext)>,
141 ) -> Subscription {147 ) -> Subscription {
142 cx.subscribe(self, move |_, event, cx| {148 cx.subscribe(self, move |handle, event, cx| {
143 if let Some(search_event) = T::to_search_event(event) {149 let search_event = handle.update(cx, |handle, cx| handle.to_search_event(event, cx));
150 if let Some(search_event) = search_event {
144 handler(search_event, cx)151 handler(search_event, cx)
145 }152 }
146 })153 })
@@ -165,6 +172,12 @@ impl<T: SearchableItem> SearchableItemHandle for ViewHandle<T> { · 18 unmodified lines
165 let matches = downcast_matches(matches);172 let matches = downcast_matches(matches);
166 self.update(cx, |this, cx| this.activate_match(index, matches, cx));173 self.update(cx, |this, cx| this.activate_match(index, matches, cx));
167 }174 }
175
176 fn select_matches(&self, matches: &Vec<Box<dyn Any + Send>>, cx: &mut WindowContext) {
177 let matches = downcast_matches(matches);
178 self.update(cx, |this, cx| this.select_matches(matches, cx));
179 }
180
168 fn match_index_for_direction(181 fn match_index_for_direction(
169 &self,182 &self,
170 matches: &Vec<Box<dyn Any + Send>>,183 matches: &Vec<Box<dyn Any + Send>>,
styles/src/style_tree/search.ts+29−0
@@ -83,6 +83,35 @@ export default function search(): any { · 82 unmodified lines
83 },83 },
84 },84 },
85 }),85 }),
86 action_button: interactive({
87 base: {
88 ...text(theme.highest, "mono", "on"),
89 background: background(theme.highest, "on"),
90 corner_radius: 6,
91 border: border(theme.highest, "on"),
92 margin: {
93 right: 4,
94 },
95 padding: {
96 bottom: 2,
97 left: 10,
98 right: 10,
99 top: 2,
100 },
101 },
102 state: {
103 hovered: {
104 ...text(theme.highest, "mono", "on", "hovered"),
105 background: background(theme.highest, "on", "hovered"),
106 border: border(theme.highest, "on", "hovered"),
107 },
108 clicked: {
109 ...text(theme.highest, "mono", "on", "pressed"),
110 background: background(theme.highest, "on", "pressed"),
111 border: border(theme.highest, "on", "pressed"),
112 },
113 },
114 }),
86 editor,115 editor,
87 invalid_editor: {116 invalid_editor: {
88 ...editor,117 ...editor,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t