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 1/2] 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 708a22c5c649a51b57b7ea8a5cd17d016fad4ded..0f7a9bfde00553e2350d92e75c6e7b7952fdcf31 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 a94f01a386e2738b8a18b5c70b18490bb11e78d0..ec38f0d9ac773c9bb3a3462a1b8c99aa76b2a529 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() + ); }); } From 6ce3f3bf27306cee6a87a95e2d7306151a783b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Hovs=C3=A4ter?= Date: Fri, 9 Jun 2023 13:22:04 +0200 Subject: [PATCH 2/2] Refactor toggle comment tests --- crates/editor/src/editor_tests.rs | 183 ++++++++++++------------------ 1 file changed, 75 insertions(+), 108 deletions(-) diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index ec38f0d9ac773c9bb3a3462a1b8c99aa76b2a529..8eef2cf9c007fdfd4219d144940f5e5f1d86ea04 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -4921,7 +4921,7 @@ async fn test_completion(cx: &mut gpui::TestAppContext) { #[gpui::test] async fn test_toggle_comment(cx: &mut gpui::TestAppContext) { init_test(cx, |_| {}); - + let mut cx = EditorTestContext::new(cx).await; let language = Arc::new(Language::new( LanguageConfig { line_comment: Some("// ".into()), @@ -4929,128 +4929,95 @@ async fn test_toggle_comment(cx: &mut gpui::TestAppContext) { }, 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() { - //b(); - // c(); - // d(); + «//b(); + ˇ»// «c(); + //ˇ» d(); } - " - .unindent(); + "}); - let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx)); - let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx)); - let (_, view) = cx.add_window(|cx| build_editor(buffer, cx)); + cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx)); - view.update(cx, |editor, cx| { - // If multiple selections intersect a line, the line is only - // toggled once. - editor.change_selections(None, cx, |s| { - s.select_display_ranges([ - 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() - ); + cx.assert_editor_state(indoc! {" + fn a() { + «b(); + c(); + ˇ» d(); + } + "}); - // The comment prefix is inserted at the same column for every line - // in a selection. - editor.change_selections(None, cx, |s| { - 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() - ); + // The comment prefix is inserted at the same column for every line in a + // selection. + cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx)); - // If a selection ends at the beginning of a line, that line is not toggled. - editor.change_selections(None, cx, |s| { - s.select_display_ranges([DisplayPoint::new(2, 0)..DisplayPoint::new(3, 0)]) - }); - editor.toggle_comments(&ToggleComments::default(), cx); - assert_eq!( - editor.text(cx), - " - fn a() { - // b(); - c(); - // d(); - } - " - .unindent() - ); + cx.assert_editor_state(indoc! {" + fn a() { + // «b(); + // c(); + ˇ»// d(); + } + "}); - // If a selection span a single line and is empty, the line is toggled. - editor.set_text( - " + // If a selection ends at the beginning of a line, that line is not toggled. + cx.set_selections_state(indoc! {" + fn a() { + // b(); + «// c(); + ˇ» // d(); + } + "}); - fn a() { } + cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx)); - " - .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() - ); + cx.assert_editor_state(indoc! {" + fn a() { + // b(); + «c(); + ˇ» // d(); + } + "}); - // If a selection span multiple lines, empty lines are not toggled. - editor.set_text( - " + // If a selection span a single line and is empty, the line is toggled. + cx.set_state(indoc! {" + fn a() { + a(); + b(); + ˇ + } + "}); - fn a() { } + cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx)); - " - .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), - " + 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(); - // fn a() { } + c();ˇ» + } + "}); - " - .unindent() - ); - }); + cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx)); + + cx.assert_editor_state(indoc! {" + fn a() { + // «a(); + + // c();ˇ» + } + "}); } #[gpui::test]