vim: Add support for vim::PreviousLineStart motion (#14193)

Release Notes:

- vim: Added `-`/`+` to go to beginning of line above/below
([#14183](https://github.com/zed-industries/zed/issues/14183)).
- vim: (Breaking) Removed non-standard builtin binding from `-` to open
the project panel. You can re-add it to your keymap file with:
`{"context":"VimControl", "bindings":{ "-":
"pane::RevealInProjectPanel"}}`


Optionally, include screenshots / media showcasing your addition that
can be included in the release notes.


https://github.com/zed-industries/zed/assets/32429059/0e9e9348-265e-4a81-a45a-4739034dc5d9

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
sherwyn 2024-07-12 07:36:07 +09:00 committed by GitHub
parent 12dfd4a2c2
commit e402d7e96a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 43 additions and 2 deletions

View file

@ -12,6 +12,8 @@
"down": "vim::Down", "down": "vim::Down",
"enter": "vim::NextLineStart", "enter": "vim::NextLineStart",
"ctrl-m": "vim::NextLineStart", "ctrl-m": "vim::NextLineStart",
"+": "vim::NextLineStart",
"-": "vim::PreviousLineStart",
"tab": "vim::Tab", "tab": "vim::Tab",
"shift-tab": "vim::Tab", "shift-tab": "vim::Tab",
"k": "vim::Up", "k": "vim::Up",
@ -192,7 +194,6 @@
"ctrl-w g shift-d": "editor::GoToTypeDefinitionSplit", "ctrl-w g shift-d": "editor::GoToTypeDefinitionSplit",
"ctrl-w space": "editor::OpenExcerptsSplit", "ctrl-w space": "editor::OpenExcerptsSplit",
"ctrl-w g space": "editor::OpenExcerptsSplit", "ctrl-w g space": "editor::OpenExcerptsSplit",
"-": "pane::RevealInProjectPanel",
"ctrl-6": "pane::AlternateFile" "ctrl-6": "pane::AlternateFile"
} }
}, },

View file

@ -91,6 +91,7 @@ pub enum Motion {
last_find: Box<Motion>, last_find: Box<Motion>,
}, },
NextLineStart, NextLineStart,
PreviousLineStart,
StartOfLineDownward, StartOfLineDownward,
EndOfLineDownward, EndOfLineDownward,
GoToColumn, GoToColumn,
@ -235,6 +236,7 @@ actions!(
EndOfDocument, EndOfDocument,
Matching, Matching,
NextLineStart, NextLineStart,
PreviousLineStart,
StartOfLineDownward, StartOfLineDownward,
EndOfLineDownward, EndOfLineDownward,
GoToColumn, GoToColumn,
@ -353,6 +355,9 @@ pub fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(|_: &mut Workspace, &NextLineStart, cx: _| { workspace.register_action(|_: &mut Workspace, &NextLineStart, cx: _| {
motion(Motion::NextLineStart, cx) motion(Motion::NextLineStart, cx)
}); });
workspace.register_action(|_: &mut Workspace, &PreviousLineStart, cx: _| {
motion(Motion::PreviousLineStart, cx)
});
workspace.register_action(|_: &mut Workspace, &StartOfLineDownward, cx: _| { workspace.register_action(|_: &mut Workspace, &StartOfLineDownward, cx: _| {
motion(Motion::StartOfLineDownward, cx) motion(Motion::StartOfLineDownward, cx)
}); });
@ -468,6 +473,7 @@ impl Motion {
| EndOfDocument | EndOfDocument
| CurrentLine | CurrentLine
| NextLineStart | NextLineStart
| PreviousLineStart
| StartOfLineDownward | StartOfLineDownward
| StartOfParagraph | StartOfParagraph
| WindowTop | WindowTop
@ -537,6 +543,7 @@ impl Motion {
| WindowMiddle | WindowMiddle
| WindowBottom | WindowBottom
| NextLineStart | NextLineStart
| PreviousLineStart
| ZedSearchResult { .. } | ZedSearchResult { .. }
| Jump { .. } => false, | Jump { .. } => false,
} }
@ -561,7 +568,8 @@ impl Motion {
| PreviousWordEnd { .. } | PreviousWordEnd { .. }
| NextSubwordEnd { .. } | NextSubwordEnd { .. }
| PreviousSubwordEnd { .. } | PreviousSubwordEnd { .. }
| NextLineStart => true, | NextLineStart
| PreviousLineStart => true,
Left Left
| Backspace | Backspace
| Right | Right
@ -763,6 +771,7 @@ impl Motion {
_ => return None, _ => return None,
}, },
NextLineStart => (next_line_start(map, point, times), SelectionGoal::None), NextLineStart => (next_line_start(map, point, times), SelectionGoal::None),
PreviousLineStart => (previous_line_start(map, point, times), SelectionGoal::None),
StartOfLineDownward => (next_line_start(map, point, times - 1), SelectionGoal::None), StartOfLineDownward => (next_line_start(map, point, times - 1), SelectionGoal::None),
EndOfLineDownward => (last_non_whitespace(map, point, times), SelectionGoal::None), EndOfLineDownward => (last_non_whitespace(map, point, times), SelectionGoal::None),
GoToColumn => (go_to_column(map, point, times), SelectionGoal::None), GoToColumn => (go_to_column(map, point, times), SelectionGoal::None),
@ -1655,6 +1664,11 @@ fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) ->
first_non_whitespace(map, false, correct_line) first_non_whitespace(map, false, correct_line)
} }
fn previous_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
let correct_line = start_of_relative_buffer_row(map, point, (times as isize) * -1);
first_non_whitespace(map, false, correct_line)
}
fn go_to_column(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint { fn go_to_column(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
let correct_line = start_of_relative_buffer_row(map, point, 0); let correct_line = start_of_relative_buffer_row(map, point, 0);
right(map, correct_line, times.saturating_sub(1)) right(map, correct_line, times.saturating_sub(1))

View file

@ -1279,3 +1279,22 @@ async fn test_find_multibyte(cx: &mut gpui::TestAppContext) {
.await .await
.assert_eq(r#"<label for="guests">ˇo</label>"#); .assert_eq(r#"<label for="guests">ˇo</label>"#);
} }
#[gpui::test]
async fn test_plus_minus(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.set_shared_state(indoc! {
"one
two
thrˇee
"})
.await;
cx.simulate_shared_keystrokes("-").await;
cx.shared_state().await.assert_matches();
cx.simulate_shared_keystrokes("-").await;
cx.shared_state().await.assert_matches();
cx.simulate_shared_keystrokes("+").await;
cx.shared_state().await.assert_matches();
}

View file

@ -0,0 +1,7 @@
{"Put":{"state":"one\n two\nthrˇee\n"}}
{"Key":"-"}
{"Get":{"state":"one\n ˇtwo\nthree\n","mode":"Normal"}}
{"Key":"-"}
{"Get":{"state":"ˇone\n two\nthree\n","mode":"Normal"}}
{"Key":"+"}
{"Get":{"state":"one\n ˇtwo\nthree\n","mode":"Normal"}}