From 8742d4ab90be3b114aff25dff60329cecf70fa41 Mon Sep 17 00:00:00 2001 From: smit Date: Thu, 22 May 2025 03:56:20 +0530 Subject: [PATCH] editor: Fix regression causing incorrect delimiter on newline in case of multiple comment prefixes (#31129) Closes #31115 This fixes regression caused by https://github.com/zed-industries/zed/pull/30824 while keeping that fix. - [x] Test Release Notes: - Fixed the issue where adding a newline after the `///` comment would extend it with `//` instead of `///` in Rust and other similar languages. --- crates/editor/src/editor.rs | 15 +++++++------ crates/editor/src/editor_tests.rs | 36 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 1bbd565c16..6b935aeb4d 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -3964,15 +3964,18 @@ impl Editor { .skip(num_of_whitespaces) .take(max_len_of_delimiter) .collect::(); - let (delimiter, trimmed_len) = - delimiters.iter().find_map(|delimiter| { - let trimmed = delimiter.trim_end(); - if comment_candidate.starts_with(trimmed) { - Some((delimiter, trimmed.len())) + let (delimiter, trimmed_len) = delimiters + .iter() + .filter_map(|delimiter| { + let prefix = delimiter.trim_end(); + if comment_candidate.starts_with(prefix) { + Some((delimiter, prefix.len())) } else { None } - })?; + }) + .max_by_key(|(_, len)| *len)?; + let cursor_is_placed_after_comment_marker = num_of_whitespaces + trimmed_len <= start_point.column as usize; if cursor_is_placed_after_comment_marker { diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 487a9369d7..e81b72538d 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -2820,6 +2820,42 @@ async fn test_newline_comments(cx: &mut TestAppContext) { "}); } +#[gpui::test] +async fn test_newline_comments_with_multiple_delimiters(cx: &mut TestAppContext) { + init_test(cx, |settings| { + settings.defaults.tab_size = NonZeroU32::new(4) + }); + + let language = Arc::new(Language::new( + LanguageConfig { + line_comments: vec!["// ".into(), "/// ".into()], + ..LanguageConfig::default() + }, + None, + )); + { + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx)); + cx.set_state(indoc! {" + //ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + // + // ˇ + "}); + + cx.set_state(indoc! {" + ///ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + /// + /// ˇ + "}); + } +} + #[gpui::test] async fn test_newline_documentation_comments(cx: &mut TestAppContext) { init_test(cx, |settings| {