vim: Copy comment to new lines with o/O (#19766)

Co-Authored-By: Kurt Wolf <kurtwolfbuilds@gmail.com>

Closes: #4691

Closes #ISSUE

Release Notes:

- vim: o/O now respect `extend_comment_on_newline`
This commit is contained in:
Conrad Irwin 2024-10-25 16:47:44 -06:00 committed by GitHub
parent 98d2e5fe73
commit 78ed0c9312
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 73 additions and 16 deletions

View file

@ -328,14 +328,18 @@ impl Vim {
.into_iter()
.map(|selection| selection.start.row)
.collect();
let edits = selection_start_rows.into_iter().map(|row| {
let indent = snapshot
.indent_size_for_line(MultiBufferRow(row))
.chars()
.collect::<String>();
let start_of_line = Point::new(row, 0);
(start_of_line..start_of_line, indent + "\n")
});
let edits = selection_start_rows
.into_iter()
.map(|row| {
let indent = snapshot
.indent_and_comment_for_line(MultiBufferRow(row), cx)
.chars()
.collect::<String>();
let start_of_line = Point::new(row, 0);
(start_of_line..start_of_line, indent + "\n")
})
.collect::<Vec<_>>();
editor.edit_with_autoindent(edits, cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_cursors_with(|map, cursor, _| {
@ -361,14 +365,18 @@ impl Vim {
.into_iter()
.map(|selection| selection.end.row)
.collect();
let edits = selection_end_rows.into_iter().map(|row| {
let indent = snapshot
.indent_size_for_line(MultiBufferRow(row))
.chars()
.collect::<String>();
let end_of_line = Point::new(row, snapshot.line_len(MultiBufferRow(row)));
(end_of_line..end_of_line, "\n".to_string() + &indent)
});
let edits = selection_end_rows
.into_iter()
.map(|row| {
let indent = snapshot
.indent_and_comment_for_line(MultiBufferRow(row), cx)
.chars()
.collect::<String>();
let end_of_line = Point::new(row, snapshot.line_len(MultiBufferRow(row)));
(end_of_line..end_of_line, "\n".to_string() + &indent)
})
.collect::<Vec<_>>();
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.maybe_move_cursors_with(|map, cursor, goal| {
Motion::CurrentLine.move_point(
@ -1414,4 +1422,16 @@ mod test {
.await
.assert_eq("th th\nth th\nth th\nth th\nth th\nˇth th\n");
}
#[gpui::test]
async fn test_o_comment(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.set_neovim_option("filetype=rust").await;
cx.set_shared_state("// helloˇ\n").await;
cx.simulate_shared_keystrokes("o").await;
cx.shared_state().await.assert_eq("// hello\n// ˇ\n");
cx.simulate_shared_keystrokes("x escape shift-o").await;
cx.shared_state().await.assert_eq("// hello\n// ˇ\n// x\n");
}
}

View file

@ -0,0 +1,8 @@
{"SetOption":{"value":"filetype=rust"}}
{"Put":{"state":"// helloˇ\n"}}
{"Key":"o"}
{"Get":{"state":"// hello\n// ˇ\n","mode":"Insert"}}
{"Key":"x"}
{"Key":"escape"}
{"Key":"shift-o"}
{"Get":{"state":"// hello\n// ˇ\n// x\n","mode":"Insert"}}