Skip to content

Commit ff0bb1f

Browse files
authored
vim: Fix insert before in visual modes (zed-industries#25603)
Closes zed-industries#22536 Changes: - Visual and visual block: Cursor at start of selection. - Visual line: Cursor at start on line. - Uses different handling since the selection does not actually change in vline. Release Notes: - vim: Fixed insert before (`shift-i`) in visual modes.
1 parent 9c7eee2 commit ff0bb1f

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

crates/vim/src/normal.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ impl Vim {
291291

292292
fn insert_before(&mut self, _: &InsertBefore, window: &mut Window, cx: &mut Context<Self>) {
293293
self.start_recording(cx);
294+
if self.mode.is_visual() {
295+
let current_mode = self.mode;
296+
self.update_editor(window, cx, |_, editor, window, cx| {
297+
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
298+
s.move_with(|map, selection| {
299+
if current_mode == Mode::VisualLine {
300+
let start_of_line = motion::start_of_line(map, false, selection.start);
301+
selection.collapse_to(start_of_line, SelectionGoal::None)
302+
} else {
303+
selection.collapse_to(selection.start, SelectionGoal::None)
304+
}
305+
});
306+
});
307+
});
308+
}
294309
self.switch_mode(Mode::Insert, false, window, cx);
295310
}
296311

@@ -1589,4 +1604,35 @@ mod test {
15891604
cx.simulate_shared_keystrokes("p p").await;
15901605
cx.shared_state().await.assert_eq("\nhello\nˇhello");
15911606
}
1607+
1608+
#[gpui::test]
1609+
async fn test_visual_mode_insert_before_after(cx: &mut gpui::TestAppContext) {
1610+
let mut cx = NeovimBackedTestContext::new(cx).await;
1611+
1612+
cx.set_shared_state("heˇllo").await;
1613+
cx.simulate_shared_keystrokes("v i w shift-i").await;
1614+
cx.shared_state().await.assert_eq("ˇhello");
1615+
1616+
cx.set_shared_state(indoc! {"
1617+
The quick brown
1618+
fox ˇjumps over
1619+
the lazy dog"})
1620+
.await;
1621+
cx.simulate_shared_keystrokes("shift-v shift-i").await;
1622+
cx.shared_state().await.assert_eq(indoc! {"
1623+
The quick brown
1624+
ˇfox jumps over
1625+
the lazy dog"});
1626+
1627+
cx.set_shared_state(indoc! {"
1628+
The quick brown
1629+
fox ˇjumps over
1630+
the lazy dog"})
1631+
.await;
1632+
cx.simulate_shared_keystrokes("shift-v shift-a").await;
1633+
cx.shared_state().await.assert_eq(indoc! {"
1634+
The quick brown
1635+
fox jˇumps over
1636+
the lazy dog"});
1637+
}
15921638
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{"Put":{"state":"heˇllo"}}
2+
{"Key":"v"}
3+
{"Key":"i"}
4+
{"Key":"w"}
5+
{"Key":"shift-i"}
6+
{"Get":{"state":"ˇhello","mode":"Insert"}}
7+
{"Put":{"state":"The quick brown\nfox ˇjumps over\nthe lazy dog"}}
8+
{"Key":"shift-v"}
9+
{"Key":"shift-i"}
10+
{"Get":{"state":"The quick brown\nˇfox jumps over\nthe lazy dog","mode":"Insert"}}
11+
{"Put":{"state":"The quick brown\nfox ˇjumps over\nthe lazy dog"}}
12+
{"Key":"shift-v"}
13+
{"Key":"shift-a"}
14+
{"Get":{"state":"The quick brown\nfox jˇumps over\nthe lazy dog","mode":"Insert"}}

0 commit comments

Comments
 (0)