Skip to content

Commit a603d8b

Browse files
authored
editor: Fix splitting brackets inside line comments (zed-industries#59260)
# Objective Closes zed-industries#59229 When adding a newline, Zed has a bit of intelligence to do more work than just inserting a `\n`. For comment lines, it simply extends the comment: ```rust Before // This is a comment, ˇThis is another comment After // This is a comment, // ˇThis is another comment ``` And when the cursor is inside bracket pairs, Zed inserts another newline to move the closing bracket to a new line: ```rust Before fn main() {ˇ} After fn main() { ˇ } ``` However, when we have a comment line with the cursor inside a bracket pair, both mechanisms apply, resulting in an unintended state where the closing bracket is kicked out of the comment: ```rust Before // {ˇ} After // { // ˇ } ``` We definitely do not need the bracket extension rule when we are adding a newline inside comments. We want comment lines inside brackets to be split like this: ```rust // { // ˇ} ``` ## Solution - Disable Bracket Extra Newline in Comments: When a line comment delimiter is detected, we override the default `newline_config` to set `extra_line_additional_indent` to `None` . - Avoid Rule Conflicts (Short-circuiting): The three newline customization helpers (line comment, block comment, and list item) are mutually exclusive. I refactored their execution from a sequential list into an `if let Some ... else if let Some` branch. This prevents subsequent rules from having side effects on `newline_config` when a match has already occurred. ## Testing A new test named `test_newline_comments_with_brackets` is introduced in `crates/editor/src/editor_tests.rs`, All tests in the `editor` crate were verified using `cargo test -p editor` . ## Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content adheres to Zed's UI standards ([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) and [icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md) guidelines) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable --- Release Notes: - Fixed an issue where splitting brackets inside line comments inserted an extra line and broke the comment formatting
1 parent f906b9e commit a603d8b

2 files changed

Lines changed: 56 additions & 12 deletions

File tree

crates/editor/src/editor_tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4664,6 +4664,43 @@ async fn test_newline_comments_with_multiple_delimiters(cx: &mut TestAppContext)
46644664
}
46654665
}
46664666

4667+
#[gpui::test]
4668+
async fn test_newline_comments_with_brackets(cx: &mut TestAppContext) {
4669+
init_test(cx, |settings| {
4670+
settings.defaults.tab_size = NonZeroU32::new(4)
4671+
});
4672+
let language = Arc::new(Language::new(
4673+
LanguageConfig {
4674+
line_comments: vec!["// ".into()],
4675+
brackets: BracketPairConfig {
4676+
pairs: vec![BracketPair {
4677+
start: "(".to_string(),
4678+
end: ")".to_string(),
4679+
close: false,
4680+
surround: false,
4681+
newline: true,
4682+
}],
4683+
..BracketPairConfig::default()
4684+
},
4685+
..LanguageConfig::default()
4686+
},
4687+
None,
4688+
));
4689+
4690+
{
4691+
let mut cx = EditorTestContext::new(cx).await;
4692+
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
4693+
cx.set_state(indoc! {"
4694+
// (ˇ)
4695+
"});
4696+
cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx));
4697+
cx.assert_editor_state(indoc! {"
4698+
// (
4699+
// ˇ)
4700+
"})
4701+
}
4702+
}
4703+
46674704
#[gpui::test]
46684705
async fn test_newline_comments_repl_separators(cx: &mut TestAppContext) {
46694706
init_test(cx, |settings| {

crates/editor/src/input.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,9 @@ impl Editor {
578578
},
579579
prevent_auto_indent: false,
580580
};
581+
let mut delimiter = None;
581582

582-
let comment_delimiter = maybe!({
583+
if let Some(comment_delimiter) = maybe!({
583584
if !selection_is_empty {
584585
return None;
585586
}
@@ -593,9 +594,16 @@ impl Editor {
593594
&buffer,
594595
language,
595596
);
596-
});
597-
598-
let doc_delimiter = maybe!({
597+
}) {
598+
delimiter = Some(comment_delimiter);
599+
if let NewlineConfig::Newline {
600+
extra_line_additional_indent,
601+
..
602+
} = &mut newline_config
603+
{
604+
*extra_line_additional_indent = None;
605+
}
606+
} else if let Some(doc_delimiter) = maybe!({
599607
if !selection_is_empty {
600608
return None;
601609
}
@@ -610,9 +618,9 @@ impl Editor {
610618
language,
611619
&mut newline_config,
612620
);
613-
});
614-
615-
let list_delimiter = maybe!({
621+
}) {
622+
delimiter = Some(doc_delimiter);
623+
} else if let Some(list_delimiter) = maybe!({
616624
if !selection_is_empty {
617625
return None;
618626
}
@@ -627,12 +635,11 @@ impl Editor {
627635
language,
628636
&mut newline_config,
629637
);
630-
});
638+
}) {
639+
delimiter = Some(list_delimiter);
640+
}
631641

632-
(
633-
comment_delimiter.or(doc_delimiter).or(list_delimiter),
634-
newline_config,
635-
)
642+
(delimiter, newline_config)
636643
} else {
637644
(
638645
None,

0 commit comments

Comments
 (0)