History

vim: add */#/g*/g# for jumping to next word

As in vim, this toggles the normal search experience.

Conrad Irwin committed 3 years ago

Commit dbec2ed1 parent 96ce0bb4 files · +152 −4Browse files

Unified · Split

4 files
  1. assets/keymaps/vim.json +15 −1
  2. crates/search/src/buffer_search.rs +25 −3
  3. crates/vim/src/normal.rs +4 −0
  4. crates/vim/src/normal/search.rs +108 −0
assets/keymaps/vim.json+15−1
@@ -101,6 +101,8 @@ · 100 unmodified lines
101101 "vim::SwitchMode",
102102 "Normal"
103103 ],
104+ "*": "vim::MoveToNext",
105+ "#": "vim::MoveToPrev",
104106 "0": "vim::StartOfLine", // When no number operator present, use start of line motion
105107 "1": [
106108 "vim::Number",
@@ -240,7 +242,19 @@ · 133 unmodified lines
240242 "vim::SwitchMode",
241243 "Normal"
242244 ],
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+ ]
244258 }
245259 },
246260 {
crates/search/src/buffer_search.rs+25−3
@@ -65,6 +65,7 @@ pub struct BufferSearchBar { · 64 unmodified lines
6565 pub query_editor: ViewHandle<Editor>,
6666 active_searchable_item: Option<Box<dyn SearchableItemHandle>>,
6767 active_match_index: Option<usize>,
68+ pending_match_direction: Option<Direction>,
6869 active_searchable_item_subscription: Option<Subscription>,
6970 seachable_items_with_matches:
7071 HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>,
@@ -252,6 +253,7 @@ impl BufferSearchBar { · 181 unmodified lines
252253 default_options: SearchOptions::NONE,
253254 search_options: SearchOptions::NONE,
254255 pending_search: None,
256+ pending_match_direction: None,
255257 query_contains_error: false,
256258 dismissed: true,
257259 }
@@ -285,10 +287,10 @@ impl BufferSearchBar { · 27 unmodified lines
285287 &mut self,
286288 focus: bool,
287289 suggest_query: bool,
288− search_option: SearchOptions,
290+ search_options: SearchOptions,
289291 cx: &mut ViewContext<Self>,
290292 ) -> bool {
291− self.search_options = search_option;
293+ self.search_options = search_options;
292294 let searchable_item = if let Some(searchable_item) = &self.active_searchable_item {
293295 SearchableItemHandle::boxed_clone(searchable_item.as_ref())
294296 } else {
@@ -486,6 +488,17 @@ impl BufferSearchBar { · 191 unmodified lines
486488 self.select_match(Direction::Prev, cx);
487489 }
488490
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+
489502 pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
490503 if let Some(index) = self.active_match_index {
491504 if let Some(searchable_item) = self.active_searchable_item.as_ref() {
@@ -567,6 +580,7 @@ impl BufferSearchBar { · 75 unmodified lines
567580 if let Some(active_searchable_item) = self.active_searchable_item.as_ref() {
568581 if query.is_empty() {
569582 self.active_match_index.take();
583+ self.pending_match_direction.take();
570584 active_searchable_item.clear_matches(cx);
571585 } else {
572586 let query = if self.search_options.contains(SearchOptions::REGEX) {
@@ -614,7 +628,15 @@ impl BufferSearchBar { · 41 unmodified lines
614628 .unwrap();
615629 active_searchable_item.update_matches(matches, cx);
616630 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+ }
618640 active_searchable_item
619641 .activate_match(match_ix, matches, cx);
620642 }
crates/vim/src/normal.rs+4−0
@@ -2,6 +2,7 @@ mod case; · 1 unmodified line
22 mod change;
33 mod delete;
44 mod scroll;
5+mod search;
56 mod substitute;
67 mod yank;
78
@@ -27,6 +28,7 @@ use self::{ · 19 unmodified lines
2728 case::change_case,
2829 change::{change_motion, change_object},
2930 delete::{delete_motion, delete_object},
31+ search::{move_to_next, move_to_prev},
3032 substitute::substitute,
3133 yank::{yank_motion, yank_object},
3234 };
@@ -57,6 +59,8 @@ pub fn init(cx: &mut AppContext) { · 24 unmodified lines
5759 cx.add_action(insert_line_above);
5860 cx.add_action(insert_line_below);
5961 cx.add_action(change_case);
62+ cx.add_action(move_to_next);
63+ cx.add_action(move_to_prev);
6064 cx.add_action(|_: &mut Workspace, _: &Substitute, cx| {
6165 Vim::update(cx, |vim, cx| {
6266 let times = vim.pop_number_operator(cx);
crates/vim/src/normal/search.rs+108−0
@@ -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+}

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