From 66dda8e3682430a855d39abb26f2077559ec7919 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Wed, 9 Jul 2025 14:24:05 -0700 Subject: [PATCH] editor: Fix block comment with same prefix as line comment incorrectly extending on new line (#34156) Closes #33930 Release Notes: - Fixed `--[[` incorrectly extending `--` upon a new line in Lua. --- crates/editor/src/editor.rs | 18 +++++++++++++- crates/editor/src/editor_tests.rs | 39 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index c5fe0db74c..d7e6e42659 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4381,7 +4381,7 @@ impl Editor { .take_while(|c| c.is_whitespace()) .count(); let comment_candidate = snapshot - .chars_for_range(range) + .chars_for_range(range.clone()) .skip(num_of_whitespaces) .take(max_len_of_delimiter) .collect::(); @@ -4397,6 +4397,22 @@ impl Editor { }) .max_by_key(|(_, len)| *len)?; + if let Some((block_start, _)) = language.block_comment_delimiters() + { + let block_start_trimmed = block_start.trim_end(); + if block_start_trimmed.starts_with(delimiter.trim_end()) { + let line_content = snapshot + .chars_for_range(range) + .skip(num_of_whitespaces) + .take(block_start_trimmed.len()) + .collect::(); + + if line_content.starts_with(block_start_trimmed) { + return None; + } + } + } + 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 aea84de9b0..b9078301c7 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -3080,6 +3080,45 @@ async fn test_newline_documentation_comments(cx: &mut TestAppContext) { "}); } +#[gpui::test] +async fn test_newline_comments_with_block_comment(cx: &mut TestAppContext) { + init_test(cx, |settings| { + settings.defaults.tab_size = NonZeroU32::new(4) + }); + + let lua_language = Arc::new(Language::new( + LanguageConfig { + line_comments: vec!["--".into()], + block_comment: Some(("--[[".into(), "]]".into())), + ..LanguageConfig::default() + }, + None, + )); + + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| buffer.set_language(Some(lua_language), cx)); + + // Line with line comment should extend + cx.set_state(indoc! {" + --ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + -- + --ˇ + "}); + + // Line with block comment that matches line comment should not extend + cx.set_state(indoc! {" + --[[ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + --[[ + ˇ + "}); +} + #[gpui::test] fn test_insert_with_old_selections(cx: &mut TestAppContext) { init_test(cx, |_| {});