Skip to content

editor: Fix splitting brackets inside line comments - #59260

Merged
ChristopherBiscardi merged 1 commit into
zed-industries:mainfrom
lingyaochu:newline
Jun 15, 2026
Merged

editor: Fix splitting brackets inside line comments#59260
ChristopherBiscardi merged 1 commit into
zed-industries:mainfrom
lingyaochu:newline

Conversation

@lingyaochu

Copy link
Copy Markdown
Collaborator

Objective

Closes #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:

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:

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:

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:

// {
// ˇ}

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:

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content adheres to Zed's UI standards (UX/UI and icon guidelines)
  • Tests cover the new/changed behavior
  • 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

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Jun 13, 2026
@zed-community-bot zed-community-bot Bot added community champion Issues filed by our amazing community champions! 🫶 guild Pull requests by someone in Zed Guild. NOTE: the label application is automated via github actions labels Jun 13, 2026
@ChristopherBiscardi ChristopherBiscardi added the area:editor Feedback for code editing, formatting, editor iterations, etc label Jun 15, 2026

@ChristopherBiscardi ChristopherBiscardi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@ChristopherBiscardi
ChristopherBiscardi added this pull request to the merge queue Jun 15, 2026
Merged via the queue into zed-industries:main with commit cccc7b2 Jun 15, 2026
60 checks passed
@lingyaochu
lingyaochu deleted the newline branch June 15, 2026 04:12
@ChristopherBiscardi ChristopherBiscardi self-assigned this Jun 15, 2026
This was referenced Jun 18, 2026
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…9260)

# 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
@zelenenka zelenenka removed the guild Pull requests by someone in Zed Guild. NOTE: the label application is automated via github actions label Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:editor Feedback for code editing, formatting, editor iterations, etc cla-signed The user has signed the Contributor License Agreement community champion Issues filed by our amazing community champions! 🫶

Projects

None yet

Development

Successfully merging this pull request may close these issues.

editor: brackets escape comment on newline

3 participants