Z-2303 editor: fix comment extension on the start of line (#2595)
Release Notes: - Improved comment extension for cases where cursor was placed before start of comment marker
This commit is contained in:
parent
fb83ab8e9f
commit
5bef2f1778
2 changed files with 24 additions and 3 deletions
|
@ -2219,11 +2219,23 @@ impl Editor {
|
||||||
buffer
|
buffer
|
||||||
.buffer_line_for_row(start_point.row)
|
.buffer_line_for_row(start_point.row)
|
||||||
.is_some_and(|(snapshot, range)| {
|
.is_some_and(|(snapshot, range)| {
|
||||||
snapshot
|
let mut index_of_first_non_whitespace = 0;
|
||||||
|
let line_starts_with_comment = snapshot
|
||||||
.chars_for_range(range)
|
.chars_for_range(range)
|
||||||
.skip_while(|c| c.is_whitespace())
|
.skip_while(|c| {
|
||||||
|
let should_skip = c.is_whitespace();
|
||||||
|
if should_skip {
|
||||||
|
index_of_first_non_whitespace += 1;
|
||||||
|
}
|
||||||
|
should_skip
|
||||||
|
})
|
||||||
.take(delimiter.len())
|
.take(delimiter.len())
|
||||||
.eq(delimiter.chars())
|
.eq(delimiter.chars());
|
||||||
|
let cursor_is_placed_after_comment_marker =
|
||||||
|
index_of_first_non_whitespace + delimiter.len()
|
||||||
|
<= start_point.column as usize;
|
||||||
|
line_starts_with_comment
|
||||||
|
&& cursor_is_placed_after_comment_marker
|
||||||
})
|
})
|
||||||
.then(|| delimiter.clone())
|
.then(|| delimiter.clone())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1744,6 +1744,15 @@ async fn test_newline_comments(cx: &mut gpui::TestAppContext) {
|
||||||
// Foo
|
// Foo
|
||||||
//ˇ
|
//ˇ
|
||||||
"});
|
"});
|
||||||
|
// Ensure that if cursor is before the comment start, we do not actually insert a comment prefix.
|
||||||
|
cx.set_state(indoc! {"
|
||||||
|
ˇ// Foo
|
||||||
|
"});
|
||||||
|
cx.update_editor(|e, cx| e.newline(&Newline, cx));
|
||||||
|
cx.assert_editor_state(indoc! {"
|
||||||
|
|
||||||
|
ˇ// Foo
|
||||||
|
"});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue