Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

helix: Add `vim::HelixGotoLine` action and bind it to `G` (#61581)

Oleksii Orlenko03e5ad8a630c
diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json
index cf755258ac9..eb3ec16d018 100644
--- a/assets/keymaps/vim.json
+++ b/assets/keymaps/vim.json
@@ -471,6 +471,7 @@
"alt-.": "vim::RepeatFind",
"alt-b": "editor::MoveToStartOfLargerSyntaxNode",
"alt-e": "editor::MoveToEndOfLargerSyntaxNode",
+ "shift-g": "vim::HelixGotoLine",
// Changes
"shift-r": "editor::Paste",
diff --git a/crates/vim/src/helix.rs b/crates/vim/src/helix.rs
index 99f8ed2fe9e..9b2ecf8f5a8 100644
--- a/crates/vim/src/helix.rs
+++ b/crates/vim/src/helix.rs
@@ -42,6 +42,8 @@ actions!(
HelixInsertEndOfLine,
/// Goes to the location of the last modification.
HelixGotoLastModification,
+ /// Goes to the line specified by the count.
+ HelixGotoLine,
/// Select entire line or multiple lines, extending downwards.
HelixSelectLine,
/// Select all matches of a given pattern within the current selection.
@@ -76,6 +78,7 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, Vim::helix_insert_end_of_line);
Vim::action(editor, cx, Vim::helix_yank);
Vim::action(editor, cx, Vim::helix_goto_last_modification);
+ Vim::action(editor, cx, Vim::helix_goto_line);
Vim::action(editor, cx, Vim::helix_paste);
Vim::action(editor, cx, Vim::helix_select_regex);
Vim::action(editor, cx, Vim::helix_keep_newest_selection);
@@ -858,6 +861,17 @@ impl Vim {
self.jump(".".into(), false, false, window, cx);
}
+ pub fn helix_goto_line(
+ &mut self,
+ _: &HelixGotoLine,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ if let Some(line_number) = Vim::take_count(cx) {
+ self.helix_move_cursor(Motion::StartOfDocument, Some(line_number), window, cx);
+ }
+ }
+
pub fn helix_select_lines(
&mut self,
_: &HelixSelectLine,
@@ -4431,6 +4445,97 @@ mod test {
);
}
+ #[gpui::test]
+ async fn test_helix_goto_line(cx: &mut gpui::TestAppContext) {
+ let mut cx = VimTestContext::new(cx, true).await;
+ cx.enable_helix();
+
+ // <count G> lands at column 0 of the target line
+ cx.set_state(
+ indoc! {"
+ line one
+ line two
+ line ˇthree"},
+ Mode::HelixNormal,
+ );
+ cx.simulate_keystrokes("2 shift-g");
+ cx.assert_state(
+ indoc! {"
+ line one
+ ˇ line two
+ line three"},
+ Mode::HelixNormal,
+ );
+
+ // including when targeting the current line
+ cx.set_state(
+ indoc! {"
+ line one
+ line two
+ line ˇthree"},
+ Mode::HelixNormal,
+ );
+ cx.simulate_keystrokes("3 shift-g");
+ cx.assert_state(
+ indoc! {"
+ line one
+ line two
+ ˇline three"},
+ Mode::HelixNormal,
+ );
+
+ // but not when count is missing entirely
+ cx.set_state(
+ indoc! {"
+ line one
+ line two
+ line ˇthree"},
+ Mode::HelixNormal,
+ );
+ cx.simulate_keystrokes("shift-g");
+ cx.assert_state(
+ indoc! {"
+ line one
+ line two
+ line ˇthree"},
+ Mode::HelixNormal,
+ );
+
+ // bare shift-g does not exit select mode
+ cx.set_state(
+ indoc! {"
+ line one
+ line two
+ line ˇthree"},
+ Mode::HelixSelect,
+ );
+ cx.simulate_keystrokes("shift-g");
+ cx.assert_state(
+ indoc! {"
+ line one
+ line two
+ line ˇthree"},
+ Mode::HelixSelect,
+ );
+
+ // shift-g with count jumps without extending the selection, unlike <count>gg
+ cx.set_state(
+ indoc! {"
+ line one
+ line two
+ line «ˇthree»"},
+ Mode::HelixSelect,
+ );
+ cx.simulate_keystrokes("1 shift-g");
+ cx.assert_state(
+ indoc! {"
+ ˇline one
+ line two
+ line three"},
+ Mode::HelixSelect,
+ );
+ }
+
#[gpui::test]
async fn test_helix_end_of_document(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;