Toggle comments for empty single line selections (#2594)

This commit is contained in:
Kevin Hovsäter 2023-06-13 20:15:11 +02:00 committed by GitHub
commit b365e48ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 66 deletions

View File

@ -5496,7 +5496,7 @@ impl Editor {
let mut all_selection_lines_are_comments = true; let mut all_selection_lines_are_comments = true;
for row in start_row..=end_row { for row in start_row..=end_row {
if snapshot.is_line_blank(row) { if snapshot.is_line_blank(row) && start_row < end_row {
continue; continue;
} }

View File

@ -4944,7 +4944,7 @@ async fn test_completion(cx: &mut gpui::TestAppContext) {
#[gpui::test] #[gpui::test]
async fn test_toggle_comment(cx: &mut gpui::TestAppContext) { async fn test_toggle_comment(cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {}); init_test(cx, |_| {});
let mut cx = EditorTestContext::new(cx).await;
let language = Arc::new(Language::new( let language = Arc::new(Language::new(
LanguageConfig { LanguageConfig {
line_comment: Some("// ".into()), line_comment: Some("// ".into()),
@ -4952,77 +4952,95 @@ async fn test_toggle_comment(cx: &mut gpui::TestAppContext) {
}, },
Some(tree_sitter_rust::language()), Some(tree_sitter_rust::language()),
)); ));
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
let text = " // If multiple selections intersect a line, the line is only toggled once.
cx.set_state(indoc! {"
fn a() { fn a() {
//b(); «//b();
// c(); ˇ»// «c();
// d(); //ˇ» d();
} }
" "});
.unindent();
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx)); cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx));
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
let (_, view) = cx.add_window(|cx| build_editor(buffer, cx));
view.update(cx, |editor, cx| { cx.assert_editor_state(indoc! {"
// If multiple selections intersect a line, the line is only fn a() {
// toggled once. «b();
editor.change_selections(None, cx, |s| { c();
s.select_display_ranges([ ˇ» d();
DisplayPoint::new(1, 3)..DisplayPoint::new(2, 3), }
DisplayPoint::new(3, 5)..DisplayPoint::new(3, 6), "});
])
});
editor.toggle_comments(&ToggleComments::default(), cx);
assert_eq!(
editor.text(cx),
"
fn a() {
b();
c();
d();
}
"
.unindent()
);
// The comment prefix is inserted at the same column for every line // The comment prefix is inserted at the same column for every line in a
// in a selection. // selection.
editor.change_selections(None, cx, |s| { cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx));
s.select_display_ranges([DisplayPoint::new(1, 3)..DisplayPoint::new(3, 6)])
});
editor.toggle_comments(&ToggleComments::default(), cx);
assert_eq!(
editor.text(cx),
"
fn a() {
// b();
// c();
// d();
}
"
.unindent()
);
// If a selection ends at the beginning of a line, that line is not toggled. cx.assert_editor_state(indoc! {"
editor.change_selections(None, cx, |s| { fn a() {
s.select_display_ranges([DisplayPoint::new(2, 0)..DisplayPoint::new(3, 0)]) // «b();
}); // c();
editor.toggle_comments(&ToggleComments::default(), cx); ˇ»// d();
assert_eq!( }
editor.text(cx), "});
"
fn a() { // If a selection ends at the beginning of a line, that line is not toggled.
// b(); cx.set_selections_state(indoc! {"
c(); fn a() {
// d(); // b();
} «// c();
" ˇ» // d();
.unindent() }
); "});
});
cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx));
cx.assert_editor_state(indoc! {"
fn a() {
// b();
«c();
ˇ» // d();
}
"});
// If a selection span a single line and is empty, the line is toggled.
cx.set_state(indoc! {"
fn a() {
a();
b();
ˇ
}
"});
cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx));
cx.assert_editor_state(indoc! {"
fn a() {
a();
b();
//•ˇ
}
"});
// If a selection span multiple lines, empty lines are not toggled.
cx.set_state(indoc! {"
fn a() {
«a();
c();ˇ»
}
"});
cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx));
cx.assert_editor_state(indoc! {"
fn a() {
// «a();
// c();ˇ»
}
"});
} }
#[gpui::test] #[gpui::test]