As in vim, this toggles the normal search experience.
Unified · Split
| @@ -101,6 +101,8 @@ · 100 unmodified lines | ||
| 101 | 101 | "vim::SwitchMode", |
| 102 | 102 | "Normal" |
| 103 | 103 | ], |
| 104 | + "*": "vim::MoveToNext", | |
| 105 | + "#": "vim::MoveToPrev", | |
| 104 | 106 | "0": "vim::StartOfLine", // When no number operator present, use start of line motion |
| 105 | 107 | "1": [ |
| 106 | 108 | "vim::Number", |
| @@ -240,7 +242,19 @@ · 133 unmodified lines | ||
| 240 | 242 | "vim::SwitchMode", |
| 241 | 243 | "Normal" |
| 242 | 244 | ], |
| 243 | − "d": "editor::GoToDefinition" | |
| 245 | + "d": "editor::GoToDefinition", | |
| 246 | + "*": [ | |
| 247 | + "vim::MoveToNext", | |
| 248 | + { | |
| 249 | + "partialWord": true | |
| 250 | + } | |
| 251 | + ], | |
| 252 | + "#": [ | |
| 253 | + "vim::MoveToPrev", | |
| 254 | + { | |
| 255 | + "partialWord": true | |
| 256 | + } | |
| 257 | + ] | |
| 244 | 258 | } |
| 245 | 259 | }, |
| 246 | 260 | { |
| @@ -65,6 +65,7 @@ pub struct BufferSearchBar { · 64 unmodified lines | ||
| 65 | 65 | pub query_editor: ViewHandle<Editor>, |
| 66 | 66 | active_searchable_item: Option<Box<dyn SearchableItemHandle>>, |
| 67 | 67 | active_match_index: Option<usize>, |
| 68 | + pending_match_direction: Option<Direction>, | |
| 68 | 69 | active_searchable_item_subscription: Option<Subscription>, |
| 69 | 70 | seachable_items_with_matches: |
| 70 | 71 | HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>, |
| @@ -252,6 +253,7 @@ impl BufferSearchBar { · 181 unmodified lines | ||
| 252 | 253 | default_options: SearchOptions::NONE, |
| 253 | 254 | search_options: SearchOptions::NONE, |
| 254 | 255 | pending_search: None, |
| 256 | + pending_match_direction: None, | |
| 255 | 257 | query_contains_error: false, |
| 256 | 258 | dismissed: true, |
| 257 | 259 | } |
| @@ -285,10 +287,10 @@ impl BufferSearchBar { · 27 unmodified lines | ||
| 285 | 287 | &mut self, |
| 286 | 288 | focus: bool, |
| 287 | 289 | suggest_query: bool, |
| 288 | − search_option: SearchOptions, | |
| 290 | + search_options: SearchOptions, | |
| 289 | 291 | cx: &mut ViewContext<Self>, |
| 290 | 292 | ) -> bool { |
| 291 | − self.search_options = search_option; | |
| 293 | + self.search_options = search_options; | |
| 292 | 294 | let searchable_item = if let Some(searchable_item) = &self.active_searchable_item { |
| 293 | 295 | SearchableItemHandle::boxed_clone(searchable_item.as_ref()) |
| 294 | 296 | } else { |
| @@ -486,6 +488,17 @@ impl BufferSearchBar { · 191 unmodified lines | ||
| 486 | 488 | self.select_match(Direction::Prev, cx); |
| 487 | 489 | } |
| 488 | 490 | |
| 491 | + pub fn select_word_under_cursor( | |
| 492 | + &mut self, | |
| 493 | + direction: Direction, | |
| 494 | + options: SearchOptions, | |
| 495 | + cx: &mut ViewContext<Self>, | |
| 496 | + ) { | |
| 497 | + self.active_match_index = None; | |
| 498 | + self.pending_match_direction = Some(direction); | |
| 499 | + self.show_with_options(false, true, options, cx); | |
| 500 | + } | |
| 501 | + | |
| 489 | 502 | pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) { |
| 490 | 503 | if let Some(index) = self.active_match_index { |
| 491 | 504 | if let Some(searchable_item) = self.active_searchable_item.as_ref() { |
| @@ -567,6 +580,7 @@ impl BufferSearchBar { · 75 unmodified lines | ||
| 567 | 580 | if let Some(active_searchable_item) = self.active_searchable_item.as_ref() { |
| 568 | 581 | if query.is_empty() { |
| 569 | 582 | self.active_match_index.take(); |
| 583 | + self.pending_match_direction.take(); | |
| 570 | 584 | active_searchable_item.clear_matches(cx); |
| 571 | 585 | } else { |
| 572 | 586 | let query = if self.search_options.contains(SearchOptions::REGEX) { |
| @@ -614,7 +628,15 @@ impl BufferSearchBar { · 41 unmodified lines | ||
| 614 | 628 | .unwrap(); |
| 615 | 629 | active_searchable_item.update_matches(matches, cx); |
| 616 | 630 | if select_closest_match { |
| 617 | − if let Some(match_ix) = this.active_match_index { | |
| 631 | + if let Some(mut match_ix) = this.active_match_index { | |
| 632 | + if let Some(direction) = this.pending_match_direction.take() | |
| 633 | + { | |
| 634 | + match_ix += match direction { | |
| 635 | + Direction::Next => 1, | |
| 636 | + Direction::Prev => matches.len() - 1, | |
| 637 | + }; | |
| 638 | + match_ix = match_ix % matches.len(); | |
| 639 | + } | |
| 618 | 640 | active_searchable_item |
| 619 | 641 | .activate_match(match_ix, matches, cx); |
| 620 | 642 | } |
| @@ -2,6 +2,7 @@ mod case; · 1 unmodified line | ||
| 2 | 2 | mod change; |
| 3 | 3 | mod delete; |
| 4 | 4 | mod scroll; |
| 5 | +mod search; | |
| 5 | 6 | mod substitute; |
| 6 | 7 | mod yank; |
| 7 | 8 | |
| @@ -27,6 +28,7 @@ use self::{ · 19 unmodified lines | ||
| 27 | 28 | case::change_case, |
| 28 | 29 | change::{change_motion, change_object}, |
| 29 | 30 | delete::{delete_motion, delete_object}, |
| 31 | + search::{move_to_next, move_to_prev}, | |
| 30 | 32 | substitute::substitute, |
| 31 | 33 | yank::{yank_motion, yank_object}, |
| 32 | 34 | }; |
| @@ -57,6 +59,8 @@ pub fn init(cx: &mut AppContext) { · 24 unmodified lines | ||
| 57 | 59 | cx.add_action(insert_line_above); |
| 58 | 60 | cx.add_action(insert_line_below); |
| 59 | 61 | cx.add_action(change_case); |
| 62 | + cx.add_action(move_to_next); | |
| 63 | + cx.add_action(move_to_prev); | |
| 60 | 64 | cx.add_action(|_: &mut Workspace, _: &Substitute, cx| { |
| 61 | 65 | Vim::update(cx, |vim, cx| { |
| 62 | 66 | let times = vim.pop_number_operator(cx); |
| @@ -0,0 +1,108 @@ | ||
| 1 | +use gpui::{impl_actions, ViewContext}; | |
| 2 | +use search::{BufferSearchBar, SearchOptions}; | |
| 3 | +use serde_derive::Deserialize; | |
| 4 | +use workspace::{searchable::Direction, Workspace}; | |
| 5 | + | |
| 6 | +use crate::Vim; | |
| 7 | + | |
| 8 | +#[derive(Clone, Deserialize, PartialEq)] | |
| 9 | +#[serde(rename_all = "camelCase")] | |
| 10 | +pub(crate) struct MoveToNext { | |
| 11 | + #[serde(default)] | |
| 12 | + partial_word: bool, | |
| 13 | +} | |
| 14 | + | |
| 15 | +#[derive(Clone, Deserialize, PartialEq)] | |
| 16 | +#[serde(rename_all = "camelCase")] | |
| 17 | +pub(crate) struct MoveToPrev { | |
| 18 | + #[serde(default)] | |
| 19 | + partial_word: bool, | |
| 20 | +} | |
| 21 | + | |
| 22 | +impl_actions!(vim, [MoveToNext, MoveToPrev]); | |
| 23 | + | |
| 24 | +pub(crate) fn move_to_next( | |
| 25 | + workspace: &mut Workspace, | |
| 26 | + action: &MoveToNext, | |
| 27 | + cx: &mut ViewContext<Workspace>, | |
| 28 | +) { | |
| 29 | + move_to_internal(workspace, Direction::Next, !action.partial_word, cx) | |
| 30 | +} | |
| 31 | + | |
| 32 | +pub(crate) fn move_to_prev( | |
| 33 | + workspace: &mut Workspace, | |
| 34 | + action: &MoveToPrev, | |
| 35 | + cx: &mut ViewContext<Workspace>, | |
| 36 | +) { | |
| 37 | + move_to_internal(workspace, Direction::Prev, !action.partial_word, cx) | |
| 38 | +} | |
| 39 | + | |
| 40 | +fn move_to_internal( | |
| 41 | + workspace: &mut Workspace, | |
| 42 | + direction: Direction, | |
| 43 | + whole_word: bool, | |
| 44 | + cx: &mut ViewContext<Workspace>, | |
| 45 | +) { | |
| 46 | + Vim::update(cx, |vim, cx| { | |
| 47 | + let pane = workspace.active_pane().clone(); | |
| 48 | + pane.update(cx, |pane, cx| { | |
| 49 | + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() { | |
| 50 | + search_bar.update(cx, |search_bar, cx| { | |
| 51 | + let mut options = SearchOptions::CASE_SENSITIVE; | |
| 52 | + options.set(SearchOptions::WHOLE_WORD, whole_word); | |
| 53 | + search_bar.select_word_under_cursor(direction, options, cx); | |
| 54 | + }); | |
| 55 | + } | |
| 56 | + }); | |
| 57 | + vim.clear_operator(cx); | |
| 58 | + }); | |
| 59 | +} | |
| 60 | + | |
| 61 | +#[cfg(test)] | |
| 62 | +mod test { | |
| 63 | + use search::BufferSearchBar; | |
| 64 | + | |
| 65 | + use crate::{state::Mode, test::VimTestContext}; | |
| 66 | + | |
| 67 | + #[gpui::test] | |
| 68 | + async fn test_move_to_next(cx: &mut gpui::TestAppContext) { | |
| 69 | + let mut cx = VimTestContext::new(cx, true).await; | |
| 70 | + let search_bar = cx.workspace(|workspace, cx| { | |
| 71 | + workspace | |
| 72 | + .active_pane() | |
| 73 | + .read(cx) | |
| 74 | + .toolbar() | |
| 75 | + .read(cx) | |
| 76 | + .item_of_type::<BufferSearchBar>() | |
| 77 | + .expect("Buffer search bar should be deployed") | |
| 78 | + }); | |
| 79 | + cx.set_state("ˇhi\nhigh\nhi\n", Mode::Normal); | |
| 80 | + | |
| 81 | + cx.simulate_keystrokes(["*"]); | |
| 82 | + search_bar.next_notification(&cx).await; | |
| 83 | + cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal); | |
| 84 | + | |
| 85 | + cx.simulate_keystrokes(["*"]); | |
| 86 | + search_bar.next_notification(&cx).await; | |
| 87 | + cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal); | |
| 88 | + | |
| 89 | + cx.simulate_keystrokes(["#"]); | |
| 90 | + search_bar.next_notification(&cx).await; | |
| 91 | + cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal); | |
| 92 | + | |
| 93 | + cx.simulate_keystrokes(["#"]); | |
| 94 | + search_bar.next_notification(&cx).await; | |
| 95 | + cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal); | |
| 96 | + | |
| 97 | + cx.simulate_keystrokes(["g", "*"]); | |
| 98 | + search_bar.next_notification(&cx).await; | |
| 99 | + cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal); | |
| 100 | + | |
| 101 | + cx.simulate_keystrokes(["n"]); | |
| 102 | + cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal); | |
| 103 | + | |
| 104 | + cx.simulate_keystrokes(["g", "#"]); | |
| 105 | + search_bar.next_notification(&cx).await; | |
| 106 | + cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal); | |
| 107 | + } | |
| 108 | +} | |