From 354427413abc8079b4f03cfc9b2f08ba22cb685d Mon Sep 17 00:00:00 2001 From: crwen <1543720935@qq.com> Date: Mon, 24 Jun 2024 08:23:40 +0000 Subject: [PATCH] vim: Switch to normal mode after toggling comments (#13412) Release Notes: - vim: Fixed switching to normal mode after `g c`(vim::ToggleComments) in visual mode ([#4439](https://github.com/zed-industries/zed/issues/4439)) --- assets/keymaps/vim.json | 4 +-- crates/vim/src/normal.rs | 18 +++++++++++++ crates/vim/src/test.rs | 55 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index 621046e2b4..3e674d83b9 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -612,13 +612,13 @@ { "context": "Editor && vim_mode == normal && !VimWaiting", "bindings": { - "g c c": "editor::ToggleComments" + "g c c": "vim::ToggleComments" } }, { "context": "Editor && vim_mode == visual", "bindings": { - "g c": "editor::ToggleComments" + "g c": "vim::ToggleComments" } }, { diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index 0bc03b1637..7b8b57e39d 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -64,6 +64,7 @@ actions!( JoinLines, Indent, Outdent, + ToggleComments, ] ); @@ -79,6 +80,7 @@ pub(crate) fn register(workspace: &mut Workspace, cx: &mut ViewContext) { }) } +fn toggle_comments(_: &mut Workspace, _: &ToggleComments, cx: &mut ViewContext) { + Vim::update(cx, |vim, cx| { + vim.record_current_action(cx); + vim.update_active_editor(cx, |_, editor, cx| { + editor.transact(cx, |editor, cx| { + let mut original_positions = save_selection_starts(editor, cx); + editor.toggle_comments(&Default::default(), cx); + restore_selection_cursors(editor, cx, &mut original_positions); + }); + }); + if vim.state().mode.is_visual() { + vim.switch_mode(Mode::Normal, false, cx) + } + }); +} + fn save_selection_starts(editor: &Editor, cx: &mut ViewContext) -> HashMap { let (map, selections) = editor.selections.all_display(cx); selections diff --git a/crates/vim/src/test.rs b/crates/vim/src/test.rs index 1786bde482..7c59d5541a 100644 --- a/crates/vim/src/test.rs +++ b/crates/vim/src/test.rs @@ -1211,3 +1211,58 @@ async fn test_dw_eol(cx: &mut gpui::TestAppContext) { .await .assert_eq("twelve ˇtwelve char\ntwelve char"); } + +#[gpui::test] +async fn test_toggle_comments(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + + let language = std::sync::Arc::new(language::Language::new( + language::LanguageConfig { + line_comments: vec!["// ".into(), "//! ".into(), "/// ".into()], + ..Default::default() + }, + Some(language::tree_sitter_rust::language()), + )); + cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx)); + + // works in normal model + cx.set_state( + indoc! {" + ˇone + two + three + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("g c c"); + cx.assert_state( + indoc! {" + // ˇone + two + three + "}, + Mode::Normal, + ); + + // works in visual mode + cx.simulate_keystrokes("v j g c"); + cx.assert_state( + indoc! {" + // // ˇone + // two + three + "}, + Mode::Normal, + ); + + // works in visual line mode + cx.simulate_keystrokes("shift-v j g c"); + cx.assert_state( + indoc! {" + // ˇone + two + three + "}, + Mode::Normal, + ); +}