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)).
Unified · Split
| @@ -194,8 +194,8 @@ · 193 unmodified lines | ||
| 194 | 194 | { |
| 195 | 195 | "context": "Editor && mode == auto_height", |
| 196 | 196 | "bindings": { |
| 197 | − "alt-enter": "editor::Newline", | |
| 198 | − "cmd-alt-enter": "editor::NewlineBelow" | |
| 197 | + "shift-enter": "editor::Newline", | |
| 198 | + "cmd-shift-enter": "editor::NewlineBelow" | |
| 199 | 199 | } |
| 200 | 200 | }, |
| 201 | 201 | { |
| @@ -221,7 +221,8 @@ · 19 unmodified lines | ||
| 221 | 221 | "escape": "buffer_search::Dismiss", |
| 222 | 222 | "tab": "buffer_search::FocusEditor", |
| 223 | 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 | 243 | "cmd-f": "project_search::ToggleFocus", |
| 243 | 244 | "cmd-g": "search::SelectNextMatch", |
| 244 | 245 | "cmd-shift-g": "search::SelectPrevMatch", |
| 246 | + "alt-enter": "search::SelectAllMatches", | |
| 245 | 247 | "alt-cmd-c": "search::ToggleCaseSensitive", |
| 246 | 248 | "alt-cmd-w": "search::ToggleWholeWord", |
| 247 | 249 | "alt-cmd-r": "search::ToggleRegex" |
| @@ -887,10 +887,20 @@ pub(crate) enum BufferSearchHighlights {} · 886 unmodified lines | ||
| 887 | 887 | impl SearchableItem for Editor { |
| 888 | 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 | 895 | match event { |
| 892 | 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 | 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 | 959 | fn match_index_for_direction( |
| 945 | 960 | &mut self, |
| 946 | 961 | matches: &Vec<Range<Anchor>>, |
| @@ -949,8 +964,16 @@ impl SearchableItem for Editor { · 2 unmodified lines | ||
| 949 | 964 | cx: &mut ViewContext<Self>, |
| 950 | 965 | ) -> usize { |
| 951 | 966 | let buffer = self.buffer().read(cx).snapshot(cx); |
| 952 | − let cursor = self.selections.newest_anchor().head(); | |
| 953 | − if matches[current_index].start.cmp(&cursor, &buffer).is_gt() { | |
| 967 | + let current_index_position = if self.selections.disjoint_anchors().len() == 1 { | |
| 968 | + self.selections.newest_anchor().head() | |
| 969 | + } else { | |
| 970 | + matches[current_index].start | |
| 971 | + }; | |
| 972 | + if matches[current_index] | |
| 973 | + .start | |
| 974 | + .cmp(¤t_index_position, &buffer) | |
| 975 | + .is_gt() | |
| 976 | + { | |
| 954 | 977 | if direction == Direction::Prev { |
| 955 | 978 | if current_index == 0 { |
| 956 | 979 | current_index = matches.len() - 1; |
| @@ -958,7 +981,11 @@ impl SearchableItem for Editor { · 1 unmodified line | ||
| 958 | 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(¤t_index_position, &buffer) | |
| 987 | + .is_lt() | |
| 988 | + { | |
| 962 | 989 | if direction == Direction::Next { |
| 963 | 990 | current_index = 0; |
| 964 | 991 | } |
| @@ -16,13 +16,13 @@ use crate::{ · 15 unmodified lines | ||
| 16 | 16 | Anchor, DisplayPoint, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode, ToOffset, |
| 17 | 17 | }; |
| 18 | 18 | |
| 19 | −#[derive(Clone)] | |
| 19 | +#[derive(Debug, Clone)] | |
| 20 | 20 | pub struct PendingSelection { |
| 21 | 21 | pub selection: Selection<Anchor>, |
| 22 | 22 | pub mode: SelectMode, |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | −#[derive(Clone)] | |
| 25 | +#[derive(Debug, Clone)] | |
| 26 | 26 | pub struct SelectionsCollection { |
| 27 | 27 | display_map: ModelHandle<DisplayMap>, |
| 28 | 28 | buffer: ModelHandle<MultiBuffer>, |
| @@ -362,8 +362,13 @@ impl Item for FeedbackEditor { · 361 unmodified lines | ||
| 362 | 362 | impl SearchableItem for FeedbackEditor { |
| 363 | 363 | type Match = Range<Anchor>; |
| 364 | 364 | |
| 365 | − fn to_search_event(event: &Self::Event) -> Option<workspace::searchable::SearchEvent> { | |
| 366 | − Editor::to_search_event(event) | |
| 365 | + fn to_search_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 | 374 | fn clear_matches(&mut self, cx: &mut ViewContext<Self>) { |
| @@ -391,6 +396,11 @@ impl SearchableItem for FeedbackEditor { · 21 unmodified lines | ||
| 391 | 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 | 404 | fn find_matches( |
| 395 | 405 | &mut self, |
| 396 | 406 | query: project::search::SearchQuery, |
| @@ -467,8 +467,13 @@ impl Item for LspLogView { · 466 unmodified lines | ||
| 467 | 467 | impl SearchableItem for LspLogView { |
| 468 | 468 | type Match = <Editor as SearchableItem>::Match; |
| 469 | 469 | |
| 470 | − fn to_search_event(event: &Self::Event) -> Option<workspace::searchable::SearchEvent> { | |
| 471 | − Editor::to_search_event(event) | |
| 470 | + fn to_search_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 | 479 | fn clear_matches(&mut self, cx: &mut ViewContext<Self>) { |
| @@ -494,6 +499,11 @@ impl SearchableItem for LspLogView { · 19 unmodified lines | ||
| 494 | 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 | 507 | fn find_matches( |
| 498 | 508 | &mut self, |
| 499 | 509 | query: project::search::SearchQuery, |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | use crate::{ |
| 2 | − SearchOption, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex, | |
| 3 | − ToggleWholeWord, | |
| 2 | + SearchOption, SelectAllMatches, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, | |
| 3 | + ToggleRegex, ToggleWholeWord, | |
| 4 | 4 | }; |
| 5 | 5 | use collections::HashMap; |
| 6 | 6 | use editor::Editor; |
| @@ -39,8 +39,10 @@ pub fn init(cx: &mut AppContext) { · 32 unmodified lines | ||
| 39 | 39 | cx.add_action(BufferSearchBar::focus_editor); |
| 40 | 40 | cx.add_action(BufferSearchBar::select_next_match); |
| 41 | 41 | cx.add_action(BufferSearchBar::select_prev_match); |
| 42 | + cx.add_action(BufferSearchBar::select_all_matches); | |
| 42 | 43 | cx.add_action(BufferSearchBar::select_next_match_on_pane); |
| 43 | 44 | cx.add_action(BufferSearchBar::select_prev_match_on_pane); |
| 45 | + cx.add_action(BufferSearchBar::select_all_matches_on_pane); | |
| 44 | 46 | cx.add_action(BufferSearchBar::handle_editor_cancel); |
| 45 | 47 | add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx); |
| 46 | 48 | add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx); |
| @@ -66,7 +68,7 @@ pub struct BufferSearchBar { · 19 unmodified lines | ||
| 66 | 68 | active_searchable_item: Option<Box<dyn SearchableItemHandle>>, |
| 67 | 69 | active_match_index: Option<usize>, |
| 68 | 70 | active_searchable_item_subscription: Option<Subscription>, |
| 69 | − seachable_items_with_matches: | |
| 71 | + searchable_items_with_matches: | |
| 70 | 72 | HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>, |
| 71 | 73 | pending_search: Option<Task<()>>, |
| 72 | 74 | case_sensitive: bool, |
| @@ -118,7 +120,7 @@ impl View for BufferSearchBar { · 45 unmodified lines | ||
| 118 | 120 | .with_children(self.active_searchable_item.as_ref().and_then( |
| 119 | 121 | |searchable_item| { |
| 120 | 122 | let matches = self |
| 121 | − .seachable_items_with_matches | |
| 123 | + .searchable_items_with_matches | |
| 122 | 124 | .get(&searchable_item.downgrade())?; |
| 123 | 125 | let message = if let Some(match_ix) = self.active_match_index { |
| 124 | 126 | format!("{}/{}", match_ix + 1, matches.len()) |
| @@ -146,6 +148,7 @@ impl View for BufferSearchBar { · 21 unmodified lines | ||
| 146 | 148 | Flex::row() |
| 147 | 149 | .with_child(self.render_nav_button("<", Direction::Prev, cx)) |
| 148 | 150 | .with_child(self.render_nav_button(">", Direction::Next, cx)) |
| 151 | + .with_child(self.render_action_button("Select All", cx)) | |
| 149 | 152 | .aligned(), |
| 150 | 153 | ) |
| 151 | 154 | .with_child( |
| @@ -249,7 +252,7 @@ impl BufferSearchBar { · 97 unmodified lines | ||
| 249 | 252 | active_searchable_item: None, |
| 250 | 253 | active_searchable_item_subscription: None, |
| 251 | 254 | active_match_index: None, |
| 252 | − seachable_items_with_matches: Default::default(), | |
| 255 | + searchable_items_with_matches: Default::default(), | |
| 253 | 256 | case_sensitive: false, |
| 254 | 257 | whole_word: false, |
| 255 | 258 | regex: false, |
| @@ -265,7 +268,7 @@ impl BufferSearchBar { · 9 unmodified lines | ||
| 265 | 268 | |
| 266 | 269 | pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext<Self>) { |
| 267 | 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 | 272 | if let Some(searchable_item) = |
| 270 | 273 | WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx) |
| 271 | 274 | { |
| @@ -401,6 +404,37 @@ impl BufferSearchBar { · 129 unmodified lines | ||
| 401 | 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 | 438 | fn render_close_button( |
| 405 | 439 | &self, |
| 406 | 440 | theme: &theme::Search, |
| @@ -488,11 +522,25 @@ impl BufferSearchBar { · 81 unmodified lines | ||
| 488 | 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 | 539 | pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) { |
| 492 | 540 | if let Some(index) = self.active_match_index { |
| 493 | 541 | if let Some(searchable_item) = self.active_searchable_item.as_ref() { |
| 494 | 542 | if let Some(matches) = self |
| 495 | − .seachable_items_with_matches | |
| 543 | + .searchable_items_with_matches | |
| 496 | 544 | .get(&searchable_item.downgrade()) |
| 497 | 545 | { |
| 498 | 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 | 585 | fn on_query_editor_event( |
| 528 | 586 | &mut self, |
| 529 | 587 | _: ViewHandle<Editor>, |
| @@ -547,7 +605,7 @@ impl BufferSearchBar { · 17 unmodified lines | ||
| 547 | 605 | |
| 548 | 606 | fn clear_matches(&mut self, cx: &mut ViewContext<Self>) { |
| 549 | 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 | 609 | if let Some(searchable_item) = |
| 552 | 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_matches | |
| 620 | + self.searchable_items_with_matches | |
| 563 | 621 | .extend(active_item_matches); |
| 564 | 622 | } |
| 565 | 623 | |
| @@ -605,13 +663,13 @@ impl BufferSearchBar { · 39 unmodified lines | ||
| 605 | 663 | if let Some(active_searchable_item) = |
| 606 | 664 | WeakSearchableItemHandle::upgrade(active_searchable_item.as_ref(), cx) |
| 607 | 665 | { |
| 608 | − this.seachable_items_with_matches | |
| 666 | + this.searchable_items_with_matches | |
| 609 | 667 | .insert(active_searchable_item.downgrade(), matches); |
| 610 | 668 | |
| 611 | 669 | this.update_match_index(cx); |
| 612 | 670 | if !this.dismissed { |
| 613 | 671 | let matches = this |
| 614 | − .seachable_items_with_matches | |
| 672 | + .searchable_items_with_matches | |
| 615 | 673 | .get(&active_searchable_item.downgrade()) |
| 616 | 674 | .unwrap(); |
| 617 | 675 | active_searchable_item.update_matches(matches, cx); |
| @@ -637,7 +695,7 @@ impl BufferSearchBar { · 19 unmodified lines | ||
| 637 | 695 | .as_ref() |
| 638 | 696 | .and_then(|searchable_item| { |
| 639 | 697 | let matches = self |
| 640 | − .seachable_items_with_matches | |
| 698 | + .searchable_items_with_matches | |
| 641 | 699 | .get(&searchable_item.downgrade())?; |
| 642 | 700 | searchable_item.active_match_index(matches, cx) |
| 643 | 701 | }); |
| @@ -966,4 +1024,133 @@ mod tests { · 322 unmodified lines | ||
| 966 | 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 | } |
| @@ -17,7 +17,8 @@ actions!( · 16 unmodified lines | ||
| 17 | 17 | ToggleCaseSensitive, |
| 18 | 18 | ToggleRegex, |
| 19 | 19 | SelectNextMatch, |
| 20 | − SelectPrevMatch | |
| 20 | + SelectPrevMatch, | |
| 21 | + SelectAllMatches, | |
| 21 | 22 | ] |
| 22 | 23 | ); |
| 23 | 24 | |
| @@ -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 | 926 | fn set_selection(&mut self, selection: Option<(Selection, Point)>) { |
| 912 | 927 | self.events |
| 913 | 928 | .push_back(InternalEvent::SetSelection(selection)); |
| @@ -647,7 +647,11 @@ impl SearchableItem for TerminalView { · 646 unmodified lines | ||
| 647 | 647 | } |
| 648 | 648 | |
| 649 | 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 | 655 | match event { |
| 652 | 656 | Event::Wakeup => Some(SearchEvent::MatchesInvalidated), |
| 653 | 657 | Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged), |
| @@ -682,6 +686,13 @@ impl SearchableItem for TerminalView { · 28 unmodified lines | ||
| 682 | 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 | 696 | /// Get all of the matches for this query, should be done on the background |
| 686 | 697 | fn find_matches( |
| 687 | 698 | &mut self, |
| @@ -379,6 +379,7 @@ pub struct Search { · 378 unmodified lines | ||
| 379 | 379 | pub invalid_include_exclude_editor: ContainerStyle, |
| 380 | 380 | pub include_exclude_inputs: ContainedText, |
| 381 | 381 | pub option_button: Toggleable<Interactive<ContainedText>>, |
| 382 | + pub action_button: Interactive<ContainedText>, | |
| 382 | 383 | pub match_background: Color, |
| 383 | 384 | pub match_index: ContainedText, |
| 384 | 385 | pub results_status: TextStyle, |
| @@ -37,7 +37,11 @@ pub trait SearchableItem: Item { · 36 unmodified lines | ||
| 37 | 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 | 45 | fn clear_matches(&mut self, cx: &mut ViewContext<Self>); |
| 42 | 46 | fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>); |
| 43 | 47 | fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String; |
| @@ -47,6 +51,7 @@ pub trait SearchableItem: Item { · 3 unmodified lines | ||
| 47 | 51 | matches: Vec<Self::Match>, |
| 48 | 52 | cx: &mut ViewContext<Self>, |
| 49 | 53 | ); |
| 54 | + fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>); | |
| 50 | 55 | fn match_index_for_direction( |
| 51 | 56 | &mut self, |
| 52 | 57 | matches: &Vec<Self::Match>, |
| @@ -102,6 +107,7 @@ pub trait SearchableItemHandle: ItemHandle { · 49 unmodified lines | ||
| 102 | 107 | matches: &Vec<Box<dyn Any + Send>>, |
| 103 | 108 | cx: &mut WindowContext, |
| 104 | 109 | ); |
| 110 | + fn select_matches(&self, matches: &Vec<Box<dyn Any + Send>>, cx: &mut WindowContext); | |
| 105 | 111 | fn match_index_for_direction( |
| 106 | 112 | &self, |
| 107 | 113 | matches: &Vec<Box<dyn Any + Send>>, |
| @@ -139,8 +145,9 @@ impl<T: SearchableItem> SearchableItemHandle for ViewHandle<T> { · 31 unmodified lines | ||
| 139 | 145 | cx: &mut WindowContext, |
| 140 | 146 | handler: Box<dyn Fn(SearchEvent, &mut WindowContext)>, |
| 141 | 147 | ) -> Subscription { |
| 142 | − cx.subscribe(self, move |_, event, cx| { | |
| 143 | − if let Some(search_event) = T::to_search_event(event) { | |
| 148 | + cx.subscribe(self, move |handle, event, cx| { | |
| 149 | + let search_event = handle.update(cx, |handle, cx| handle.to_search_event(event, cx)); | |
| 150 | + if let Some(search_event) = search_event { | |
| 144 | 151 | handler(search_event, cx) |
| 145 | 152 | } |
| 146 | 153 | }) |
| @@ -165,6 +172,12 @@ impl<T: SearchableItem> SearchableItemHandle for ViewHandle<T> { · 18 unmodified lines | ||
| 165 | 172 | let matches = downcast_matches(matches); |
| 166 | 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 | 181 | fn match_index_for_direction( |
| 169 | 182 | &self, |
| 170 | 183 | matches: &Vec<Box<dyn Any + Send>>, |
| @@ -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 | 115 | editor, |
| 87 | 116 | invalid_editor: { |
| 88 | 117 | ...editor, |