vim: Add sneak motion (#22793)

A (re)continuation of https://github.com/zed-industries/zed/pull/21067. 

This takes the original implementation in
https://github.com/zed-industries/zed/pull/15572 and adds the test in
https://github.com/zed-industries/zed/pull/21067. Then, as requested in
https://github.com/zed-industries/zed/pull/21067#issuecomment-2515469185,
it documents how to map a keybinding instead of having a setting.

Closes #13858

Release Notes:

- Added support for the popular
[vim_sneak](https://github.com/justinmk/vim-sneak) plugin. This is
disabled by default and can be enabled by binding a key to the `Sneak`
and `SneakBackward` operators.

Reference:
https://github.com/justinmk/vim-sneak

---------

Co-authored-by: Kajetan Puchalski <kajetan.puchalski@tuta.io>
Co-authored-by: Aidan Grant <mraidangrant@gmail.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Nico Lehmann 2025-01-10 04:07:32 -03:00 committed by GitHub
parent 0d6a549950
commit 0b105ba8b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 301 additions and 5 deletions

View file

@ -17,7 +17,12 @@ use indoc::indoc;
use search::BufferSearchBar;
use workspace::WorkspaceSettings;
use crate::{insert::NormalBefore, motion, state::Mode};
use crate::{
insert::NormalBefore,
motion,
state::{Mode, Operator},
PushOperator,
};
#[gpui::test]
async fn test_initially_disabled(cx: &mut gpui::TestAppContext) {
@ -1332,6 +1337,68 @@ async fn test_find_multibyte(cx: &mut gpui::TestAppContext) {
.assert_eq(r#"<label for="guests">ˇo</label>"#);
}
#[gpui::test]
async fn test_sneak(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.update(|cx| {
cx.bind_keys([
KeyBinding::new(
"s",
PushOperator(Operator::Sneak { first_char: None }),
Some("vim_mode == normal"),
),
KeyBinding::new(
"S",
PushOperator(Operator::SneakBackward { first_char: None }),
Some("vim_mode == normal"),
),
KeyBinding::new(
"S",
PushOperator(Operator::SneakBackward { first_char: None }),
Some("vim_mode == visual"),
),
])
});
// Sneak forwards multibyte & multiline
cx.set_state(
indoc! {
r#"<labelˇ for="guests">
Počet hostů
</label>"#
},
Mode::Normal,
);
cx.simulate_keystrokes("s t ů");
cx.assert_state(
indoc! {
r#"<label for="guests">
Počet hosˇtů
</label>"#
},
Mode::Normal,
);
// Visual sneak backwards multibyte & multiline
cx.simulate_keystrokes("v S < l");
cx.assert_state(
indoc! {
r#"«ˇ<label for="guests">
Počet host»ů
</label>"#
},
Mode::Visual,
);
// Sneak backwards repeated
cx.set_state(r#"11 12 13 ˇ14"#, Mode::Normal);
cx.simulate_keystrokes("S space 1");
cx.assert_state(r#"11 12ˇ 13 14"#, Mode::Normal);
cx.simulate_keystrokes(";");
cx.assert_state(r#"11ˇ 12 13 14"#, Mode::Normal);
}
#[gpui::test]
async fn test_plus_minus(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;