From a8d43c6d71710a6ba7a5c53f3844f209132393b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Hovs=C3=A4ter?= Date: Fri, 9 Jun 2023 09:58:55 +0200 Subject: [PATCH] Toggle comments for empty single line selections --- crates/editor/src/editor.rs | 2 +- crates/editor/src/editor_tests.rs | 51 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 708a22c5c6..0f7a9bfde0 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -5480,7 +5480,7 @@ impl Editor { let mut all_selection_lines_are_comments = true; for row in start_row..=end_row { - if snapshot.is_line_blank(row) { + if snapshot.is_line_blank(row) && start_row < end_row { continue; } diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index a94f01a386..ec38f0d9ac 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -4999,6 +4999,57 @@ async fn test_toggle_comment(cx: &mut gpui::TestAppContext) { " .unindent() ); + + // If a selection span a single line and is empty, the line is toggled. + editor.set_text( + " + + fn a() { } + + " + .unindent(), + cx, + ); + editor.change_selections(None, cx, |s| { + s.select_display_ranges([ + DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0), + DisplayPoint::new(2, 0)..DisplayPoint::new(2, 0), + ]) + }); + editor.toggle_comments(&ToggleComments::default(), cx); + assert_eq!( + editor.text(cx), + " + //\x20 + fn a() { } + //\x20 + " + .unindent() + ); + + // If a selection span multiple lines, empty lines are not toggled. + editor.set_text( + " + + fn a() { } + + " + .unindent(), + cx, + ); + editor.change_selections(None, cx, |s| { + s.select_display_ranges([DisplayPoint::new(0, 0)..DisplayPoint::new(2, 0)]) + }); + editor.toggle_comments(&ToggleComments::default(), cx); + assert_eq!( + editor.text(cx), + " + + // fn a() { } + + " + .unindent() + ); }); }