editor: Fix issue where newline on *
as prefix adds comment delimiter (#31271)
Release Notes: - Fixed issue where pressing Enter on a line starting with * incorrectly added comment delimiter. --------- Co-authored-by: Piotr Osiewicz <peterosiewicz@gmail.com> Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
This commit is contained in:
parent
0201d1e0b4
commit
03ac3fb91a
2 changed files with 42 additions and 19 deletions
|
@ -4005,6 +4005,13 @@ impl Editor {
|
||||||
tab_size: len,
|
tab_size: len,
|
||||||
} = language.documentation()?;
|
} = language.documentation()?;
|
||||||
|
|
||||||
|
let is_within_block_comment = buffer
|
||||||
|
.language_scope_at(start_point)
|
||||||
|
.is_some_and(|scope| scope.override_name() == Some("comment"));
|
||||||
|
if !is_within_block_comment {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let (snapshot, range) =
|
let (snapshot, range) =
|
||||||
buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?;
|
buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?;
|
||||||
|
|
||||||
|
@ -4013,6 +4020,8 @@ impl Editor {
|
||||||
.take_while(|c| c.is_whitespace())
|
.take_while(|c| c.is_whitespace())
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
|
// It is safe to use a column from MultiBufferPoint in context of a single buffer ranges, because we're only ever looking at a single line at a time.
|
||||||
|
let column = start_point.column;
|
||||||
let cursor_is_after_start_tag = {
|
let cursor_is_after_start_tag = {
|
||||||
let start_tag_len = start_tag.len();
|
let start_tag_len = start_tag.len();
|
||||||
let start_tag_line = snapshot
|
let start_tag_line = snapshot
|
||||||
|
@ -4021,8 +4030,7 @@ impl Editor {
|
||||||
.take(start_tag_len)
|
.take(start_tag_len)
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
if start_tag_line.starts_with(start_tag.as_ref()) {
|
if start_tag_line.starts_with(start_tag.as_ref()) {
|
||||||
num_of_whitespaces + start_tag_len
|
num_of_whitespaces + start_tag_len <= column as usize
|
||||||
<= start_point.column as usize
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@ -4036,8 +4044,7 @@ impl Editor {
|
||||||
.take(delimiter_trim.len())
|
.take(delimiter_trim.len())
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
if delimiter_line.starts_with(delimiter_trim) {
|
if delimiter_line.starts_with(delimiter_trim) {
|
||||||
num_of_whitespaces + delimiter_trim.len()
|
num_of_whitespaces + delimiter_trim.len() <= column as usize
|
||||||
<= start_point.column as usize
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@ -4059,14 +4066,13 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(end_tag_offset) = end_tag_offset {
|
if let Some(end_tag_offset) = end_tag_offset {
|
||||||
let cursor_is_before_end_tag =
|
let cursor_is_before_end_tag = column <= end_tag_offset;
|
||||||
start_point.column <= end_tag_offset;
|
|
||||||
if cursor_is_after_start_tag {
|
if cursor_is_after_start_tag {
|
||||||
if cursor_is_before_end_tag {
|
if cursor_is_before_end_tag {
|
||||||
insert_extra_newline = true;
|
insert_extra_newline = true;
|
||||||
}
|
}
|
||||||
let cursor_is_at_start_of_end_tag =
|
let cursor_is_at_start_of_end_tag =
|
||||||
start_point.column == end_tag_offset;
|
column == end_tag_offset;
|
||||||
if cursor_is_at_start_of_end_tag {
|
if cursor_is_at_start_of_end_tag {
|
||||||
indent_on_extra_newline.len = (*len).into();
|
indent_on_extra_newline.len = (*len).into();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2863,7 +2863,8 @@ async fn test_newline_documentation_comments(cx: &mut TestAppContext) {
|
||||||
settings.defaults.tab_size = NonZeroU32::new(4)
|
settings.defaults.tab_size = NonZeroU32::new(4)
|
||||||
});
|
});
|
||||||
|
|
||||||
let language = Arc::new(Language::new(
|
let language = Arc::new(
|
||||||
|
Language::new(
|
||||||
LanguageConfig {
|
LanguageConfig {
|
||||||
documentation: Some(language::DocumentationConfig {
|
documentation: Some(language::DocumentationConfig {
|
||||||
start: "/**".into(),
|
start: "/**".into(),
|
||||||
|
@ -2871,10 +2872,15 @@ async fn test_newline_documentation_comments(cx: &mut TestAppContext) {
|
||||||
prefix: "* ".into(),
|
prefix: "* ".into(),
|
||||||
tab_size: NonZeroU32::new(1).unwrap(),
|
tab_size: NonZeroU32::new(1).unwrap(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
..LanguageConfig::default()
|
..LanguageConfig::default()
|
||||||
},
|
},
|
||||||
None,
|
Some(tree_sitter_rust::LANGUAGE.into()),
|
||||||
));
|
)
|
||||||
|
.with_override_query("[(line_comment)(block_comment)] @comment.inclusive")
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut cx = EditorTestContext::new(cx).await;
|
let mut cx = EditorTestContext::new(cx).await;
|
||||||
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
|
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
|
||||||
|
@ -3038,6 +3044,17 @@ async fn test_newline_documentation_comments(cx: &mut TestAppContext) {
|
||||||
*/
|
*/
|
||||||
ˇtext
|
ˇtext
|
||||||
"});
|
"});
|
||||||
|
|
||||||
|
// Ensure if not comment block it doesn't
|
||||||
|
// add comment prefix on newline
|
||||||
|
cx.set_state(indoc! {"
|
||||||
|
* textˇ
|
||||||
|
"});
|
||||||
|
cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx));
|
||||||
|
cx.assert_editor_state(indoc! {"
|
||||||
|
* text
|
||||||
|
ˇ
|
||||||
|
"});
|
||||||
}
|
}
|
||||||
// Ensure that comment continuations can be disabled.
|
// Ensure that comment continuations can be disabled.
|
||||||
update_test_language_settings(cx, |settings| {
|
update_test_language_settings(cx, |settings| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue