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
101 "vim::SwitchMode",101 "vim::SwitchMode",
102 "Normal"102 "Normal"
103 ],103 ],
104 "*": "vim::MoveToNext",
105 "#": "vim::MoveToPrev",
104 "0": "vim::StartOfLine", // When no number operator present, use start of line motion106 "0": "vim::StartOfLine", // When no number operator present, use start of line motion
105 "1": [107 "1": [
106 "vim::Number",108 "vim::Number",
@@ -240,7 +242,19 @@ · 133 unmodified lines
240 "vim::SwitchMode",242 "vim::SwitchMode",
241 "Normal"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 {
crates/search/src/buffer_search.rs+25−3
@@ -65,6 +65,7 @@ pub struct BufferSearchBar { · 64 unmodified lines
65 pub query_editor: ViewHandle<Editor>,65 pub query_editor: ViewHandle<Editor>,
66 active_searchable_item: Option<Box<dyn SearchableItemHandle>>,66 active_searchable_item: Option<Box<dyn SearchableItemHandle>>,
67 active_match_index: Option<usize>,67 active_match_index: Option<usize>,
68 pending_match_direction: Option<Direction>,
68 active_searchable_item_subscription: Option<Subscription>,69 active_searchable_item_subscription: Option<Subscription>,
69 seachable_items_with_matches:70 seachable_items_with_matches:
70 HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>,71 HashMap<Box<dyn WeakSearchableItemHandle>, Vec<Box<dyn Any + Send>>>,
@@ -252,6 +253,7 @@ impl BufferSearchBar { · 181 unmodified lines
252 default_options: SearchOptions::NONE,253 default_options: SearchOptions::NONE,
253 search_options: SearchOptions::NONE,254 search_options: SearchOptions::NONE,
254 pending_search: None,255 pending_search: None,
256 pending_match_direction: None,
255 query_contains_error: false,257 query_contains_error: false,
256 dismissed: true,258 dismissed: true,
257 }259 }
@@ -285,10 +287,10 @@ impl BufferSearchBar { · 27 unmodified lines
285 &mut self,287 &mut self,
286 focus: bool,288 focus: bool,
287 suggest_query: bool,289 suggest_query: bool,
288 search_option: SearchOptions,290 search_options: SearchOptions,
289 cx: &mut ViewContext<Self>,291 cx: &mut ViewContext<Self>,
290 ) -> bool {292 ) -> bool {
291 self.search_options = search_option;293 self.search_options = search_options;
292 let searchable_item = if let Some(searchable_item) = &self.active_searchable_item {294 let searchable_item = if let Some(searchable_item) = &self.active_searchable_item {
293 SearchableItemHandle::boxed_clone(searchable_item.as_ref())295 SearchableItemHandle::boxed_clone(searchable_item.as_ref())
294 } else {296 } else {
@@ -486,6 +488,17 @@ impl BufferSearchBar { · 191 unmodified lines
486 self.select_match(Direction::Prev, cx);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 pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {502 pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
490 if let Some(index) = self.active_match_index {503 if let Some(index) = self.active_match_index {
491 if let Some(searchable_item) = self.active_searchable_item.as_ref() {504 if let Some(searchable_item) = self.active_searchable_item.as_ref() {
@@ -567,6 +580,7 @@ impl BufferSearchBar { · 75 unmodified lines
567 if let Some(active_searchable_item) = self.active_searchable_item.as_ref() {580 if let Some(active_searchable_item) = self.active_searchable_item.as_ref() {
568 if query.is_empty() {581 if query.is_empty() {
569 self.active_match_index.take();582 self.active_match_index.take();
583 self.pending_match_direction.take();
570 active_searchable_item.clear_matches(cx);584 active_searchable_item.clear_matches(cx);
571 } else {585 } else {
572 let query = if self.search_options.contains(SearchOptions::REGEX) {586 let query = if self.search_options.contains(SearchOptions::REGEX) {
@@ -614,7 +628,15 @@ impl BufferSearchBar { · 41 unmodified lines
614 .unwrap();628 .unwrap();
615 active_searchable_item.update_matches(matches, cx);629 active_searchable_item.update_matches(matches, cx);
616 if select_closest_match {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 active_searchable_item640 active_searchable_item
619 .activate_match(match_ix, matches, cx);641 .activate_match(match_ix, matches, cx);
620 }642 }
crates/vim/src/normal.rs+4−0
@@ -2,6 +2,7 @@ mod case; · 1 unmodified line
2mod change;2mod change;
3mod delete;3mod delete;
4mod scroll;4mod scroll;
5mod search;
5mod substitute;6mod substitute;
6mod yank;7mod yank;
7 8
@@ -27,6 +28,7 @@ use self::{ · 19 unmodified lines
27 case::change_case,28 case::change_case,
28 change::{change_motion, change_object},29 change::{change_motion, change_object},
29 delete::{delete_motion, delete_object},30 delete::{delete_motion, delete_object},
31 search::{move_to_next, move_to_prev},
30 substitute::substitute,32 substitute::substitute,
31 yank::{yank_motion, yank_object},33 yank::{yank_motion, yank_object},
32};34};
@@ -57,6 +59,8 @@ pub fn init(cx: &mut AppContext) { · 24 unmodified lines
57 cx.add_action(insert_line_above);59 cx.add_action(insert_line_above);
58 cx.add_action(insert_line_below);60 cx.add_action(insert_line_below);
59 cx.add_action(change_case);61 cx.add_action(change_case);
62 cx.add_action(move_to_next);
63 cx.add_action(move_to_prev);
60 cx.add_action(|_: &mut Workspace, _: &Substitute, cx| {64 cx.add_action(|_: &mut Workspace, _: &Substitute, cx| {
61 Vim::update(cx, |vim, cx| {65 Vim::update(cx, |vim, cx| {
62 let times = vim.pop_number_operator(cx);66 let times = vim.pop_number_operator(cx);
crates/vim/src/normal/search.rs+108−0
@@ -0,0 +1,108 @@
1use gpui::{impl_actions, ViewContext};
2use search::{BufferSearchBar, SearchOptions};
3use serde_derive::Deserialize;
4use workspace::{searchable::Direction, Workspace};
5
6use crate::Vim;
7
8#[derive(Clone, Deserialize, PartialEq)]
9#[serde(rename_all = "camelCase")]
10pub(crate) struct MoveToNext {
11 #[serde(default)]
12 partial_word: bool,
13}
14
15#[derive(Clone, Deserialize, PartialEq)]
16#[serde(rename_all = "camelCase")]
17pub(crate) struct MoveToPrev {
18 #[serde(default)]
19 partial_word: bool,
20}
21
22impl_actions!(vim, [MoveToNext, MoveToPrev]);
23
24pub(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
32pub(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
40fn 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)]
62mod 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