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:
parent
98d2e5fe73
commit
78ed0c9312
5 changed files with 73 additions and 16 deletions
|
@ -223,6 +223,7 @@ pub fn render_parsed_markdown(
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
// hello
|
||||||
|
|
||||||
let mut links = Vec::new();
|
let mut links = Vec::new();
|
||||||
let mut link_ranges = Vec::new();
|
let mut link_ranges = Vec::new();
|
||||||
|
@ -3784,6 +3785,9 @@ impl Editor {
|
||||||
pub fn newline_below(&mut self, _: &NewlineBelow, cx: &mut ViewContext<Self>) {
|
pub fn newline_below(&mut self, _: &NewlineBelow, cx: &mut ViewContext<Self>) {
|
||||||
let buffer = self.buffer.read(cx);
|
let buffer = self.buffer.read(cx);
|
||||||
let snapshot = buffer.snapshot(cx);
|
let snapshot = buffer.snapshot(cx);
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
let mut edits = Vec::new();
|
let mut edits = Vec::new();
|
||||||
let mut rows = Vec::new();
|
let mut rows = Vec::new();
|
||||||
|
|
|
@ -123,6 +123,7 @@ impl EditorLspTestContext {
|
||||||
path_suffixes: vec!["rs".to_string()],
|
path_suffixes: vec!["rs".to_string()],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
line_comments: vec!["// ".into(), "/// ".into(), "//! ".into()],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
Some(tree_sitter_rust::LANGUAGE.into()),
|
Some(tree_sitter_rust::LANGUAGE.into()),
|
||||||
|
|
|
@ -2862,6 +2862,30 @@ impl MultiBufferSnapshot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn indent_and_comment_for_line(&self, row: MultiBufferRow, cx: &AppContext) -> String {
|
||||||
|
let mut indent = self.indent_size_for_line(row).chars().collect::<String>();
|
||||||
|
|
||||||
|
if self.settings_at(0, cx).extend_comment_on_newline {
|
||||||
|
if let Some(language_scope) = self.language_scope_at(Point::new(row.0, 0)) {
|
||||||
|
let delimiters = language_scope.line_comment_prefixes();
|
||||||
|
for delimiter in delimiters {
|
||||||
|
if *self
|
||||||
|
.chars_at(Point::new(row.0, indent.len() as u32))
|
||||||
|
.take(delimiter.chars().count())
|
||||||
|
.collect::<String>()
|
||||||
|
.as_str()
|
||||||
|
== **delimiter
|
||||||
|
{
|
||||||
|
indent.push_str(&delimiter);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
indent
|
||||||
|
}
|
||||||
|
|
||||||
pub fn prev_non_blank_row(&self, mut row: MultiBufferRow) -> Option<MultiBufferRow> {
|
pub fn prev_non_blank_row(&self, mut row: MultiBufferRow) -> Option<MultiBufferRow> {
|
||||||
while row.0 > 0 {
|
while row.0 > 0 {
|
||||||
row.0 -= 1;
|
row.0 -= 1;
|
||||||
|
|
|
@ -328,14 +328,18 @@ impl Vim {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|selection| selection.start.row)
|
.map(|selection| selection.start.row)
|
||||||
.collect();
|
.collect();
|
||||||
let edits = selection_start_rows.into_iter().map(|row| {
|
let edits = selection_start_rows
|
||||||
let indent = snapshot
|
.into_iter()
|
||||||
.indent_size_for_line(MultiBufferRow(row))
|
.map(|row| {
|
||||||
.chars()
|
let indent = snapshot
|
||||||
.collect::<String>();
|
.indent_and_comment_for_line(MultiBufferRow(row), cx)
|
||||||
let start_of_line = Point::new(row, 0);
|
.chars()
|
||||||
(start_of_line..start_of_line, indent + "\n")
|
.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.edit_with_autoindent(edits, cx);
|
||||||
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||||
s.move_cursors_with(|map, cursor, _| {
|
s.move_cursors_with(|map, cursor, _| {
|
||||||
|
@ -361,14 +365,18 @@ impl Vim {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|selection| selection.end.row)
|
.map(|selection| selection.end.row)
|
||||||
.collect();
|
.collect();
|
||||||
let edits = selection_end_rows.into_iter().map(|row| {
|
let edits = selection_end_rows
|
||||||
let indent = snapshot
|
.into_iter()
|
||||||
.indent_size_for_line(MultiBufferRow(row))
|
.map(|row| {
|
||||||
.chars()
|
let indent = snapshot
|
||||||
.collect::<String>();
|
.indent_and_comment_for_line(MultiBufferRow(row), cx)
|
||||||
let end_of_line = Point::new(row, snapshot.line_len(MultiBufferRow(row)));
|
.chars()
|
||||||
(end_of_line..end_of_line, "\n".to_string() + &indent)
|
.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| {
|
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||||
s.maybe_move_cursors_with(|map, cursor, goal| {
|
s.maybe_move_cursors_with(|map, cursor, goal| {
|
||||||
Motion::CurrentLine.move_point(
|
Motion::CurrentLine.move_point(
|
||||||
|
@ -1414,4 +1422,16 @@ mod test {
|
||||||
.await
|
.await
|
||||||
.assert_eq("th th\nth th\nth th\nth th\nth th\nˇth th\n");
|
.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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
8
crates/vim/test_data/test_o_comment.json
Normal file
8
crates/vim/test_data/test_o_comment.json
Normal 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"}}
|
Loading…
Add table
Add a link
Reference in a new issue