Skip to content

editor: Add action to toggle block comments#48752

Merged
SomeoneToIgnore merged 28 commits intozed-industries:mainfrom
ozacod:toggle-block
Apr 8, 2026
Merged

editor: Add action to toggle block comments#48752
SomeoneToIgnore merged 28 commits intozed-industries:mainfrom
ozacod:toggle-block

Conversation

@ozacod
Copy link
Copy Markdown
Contributor

@ozacod ozacod commented Feb 9, 2026

Closes #4751

Testing

  • Manually tested by comparing the behaviors with vscode.
  • Those requirements are added to unit tests.

Release Notes:

  • Added action to toggle block comments

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Feb 9, 2026
@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Feb 9, 2026

Formatted, ready for rerun.

@niekdomi
Copy link
Copy Markdown
Contributor

niekdomi commented Feb 10, 2026

I tested this PR and it works quite well 😄.

One thing I realized is, that it may be more intuitive to exit the visual mode (quit selection) after running the block-comment command

Also, do you plan to add keybindings in this PR? E.g., in NeoVim, this one is likely the most popular plugin for block-comments which uses gb.. motion to toggle block comments

@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Feb 10, 2026

I tested this PR and it works quite well 😄.

One thing I realized is, that it may be more intuitive to exit the visual mode (quit selection) after running the block-comment command

Also, do you plan to add keybindings in this PR? E.g., in NeoVim, this one is likely the most popular plugin for block-comments which uses gb.. motion to toggle block comments

I added and tested keybindings for both vim(gb) and the editor(shift + alt + a). Since I rarely use Vim, I would appreciate it if you could test them too.

@niekdomi
Copy link
Copy Markdown
Contributor

niekdomi commented Feb 10, 2026

List of bugs I could find:

  • gb$ should comment out from current position to end

    expected:
    image
    got:
    image
    As you can see, the last character ; is not commented out. (off by one error?)

  • gbc should comment out the whole line with block comments e.g.,

    image

    currently, with gbc the cursor just jumps to a certain position, though I don't understand why (likely a bug?)

Otherwise this seems to work quite well, thanks 😄

@niekdomi
Copy link
Copy Markdown
Contributor

I just realized that if there are multiple block comments on the same line, it might uncomment a comment that should not be uncommented. I added a video that explains this better.

expected:

Screencast.From.2026-02-10.15-16-14.mp4

got:

Screencast.From.2026-02-10.15-15-37.mp4

as you can see, I run gbe twice, which should twice comment out from cursor position to end of the word.

@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Feb 10, 2026

@niekdomi Thanks a lot, fixed it for gb$, gbc and gbe.

@niekdomi
Copy link
Copy Markdown
Contributor

There's another bug (I think this wasn't an issue before). running gbj should comment out the current and next line. Though, if the next line is a newline/empty line, zed crashes.

This seems also to happen with gbk which comments out the current line and the one above, though, here zed crashes, if the current line (where the cursor is positioned) is a new line.

@ozacod ozacod marked this pull request as draft February 10, 2026 23:16
@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Feb 10, 2026

There's another bug (I think this wasn't an issue before). running gbj should comment out the current and next line. Though, if the next line is a newline/empty line, zed crashes.

This seems also to happen with gbk which comments out the current line and the one above, though, here zed crashes, if the current line (where the cursor is positioned) is a new line.

Fixed it. I definitely should have run more comprehensive stress tests. Until then, I am switching this PR to draft. I really appreciate your help. Thanks,

@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Feb 11, 2026

Tested following keybindings in vim mode. "gbc, 5gbc, gbe, gbw, gbj, gbk, gb5j, gb$, gb0, gbG, gbgg, gb%", some edge cases too.
Ready for review.

@ozacod ozacod marked this pull request as ready for review February 11, 2026 12:06
@niekdomi
Copy link
Copy Markdown
Contributor

It looks like the issue in #48752 (comment) is still popping up on my end. Could you double-check if the latest changes went through

@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Feb 11, 2026

It looks like the issue in #48752 (comment) is still popping up on my end. Could you double-check if the latest changes went through

The previous crashes do not occur on my end. "gbj" was not working properly, just fixed it. Same problem still exists for "gcj", added screencast for it. Since "togglecomments" is out of scope for this PR, I plan to address in a separate PR.

after.mov

@niekdomi
Copy link
Copy Markdown
Contributor

Seems like this fix also fixed gbk 🎉. You're right that gcj and gck behave incorrectly in the specific edge case mentioned in #48752 (comment) , though this issue is also on main branch

Copy link
Copy Markdown
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Thank you, seems that we need to test it a bit more, I've also left a few more code-related comments.

Comment thread crates/vim/src/normal.rs
Comment thread crates/languages/src/rust/config.toml Outdated
Comment thread assets/keymaps/default-macos.json
Comment thread crates/editor/src/editor.rs Outdated
Comment thread crates/editor/src/editor.rs
Comment thread crates/editor/src/editor.rs Outdated
Comment thread crates/editor/src/editor.rs Outdated
@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Mar 27, 2026

Hi @ConradIrwin,
@SomeoneToIgnore pointed this out in code review. What should be our action here?

I know this is how VSCode has this, but in our case seems that we should use something else as alt-letter would be expected to type letters in macOS?

cc @ConradIrwin for this and the Vim-related part to check out.

@ConradIrwin
Copy link
Copy Markdown
Member

@ozacod If you override option-shift-a, then people on a qwerty keyboard lose the ability to type Å. Similar things apply on other layouts (but often worse because many keyboards often require option to type characters in the ascii range – we used to have a binding on option-z, but that's the @ sign on a polish keyboard). https://zed.dev/blog/keyboard-localization has more information.

As toggle comment is cmd-/, maybe this could be cmd-option-/ ? or cmd-k cmd-/ ?

@ozacod
Copy link
Copy Markdown
Contributor Author

ozacod commented Apr 2, 2026

@ozacod If you override option-shift-a, then people on a qwerty keyboard lose the ability to type Å. Similar things apply on other layouts (but often worse because many keyboards often require option to type characters in the ascii range – we used to have a binding on option-z, but that's the @ sign on a polish keyboard). https://zed.dev/blog/keyboard-localization has more information.

As toggle comment is cmd-/, maybe this could be cmd-option-/ ? or cmd-k cmd-/ ?

I have changed keybindings for macOS to cmd-k cmd-/, and for windows and linux to ctrl-k ctrl-/. Should I have kept for linux/windows as is? I am leaving the preference between cmd-option-/ ? or cmd-k cmd-/ ? those to your preference too.

Copy link
Copy Markdown
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Checked with Conrad and seems we're good to move on, thank you for fixing this long-awaited issue!

@SomeoneToIgnore SomeoneToIgnore merged commit 525f10a into zed-industries:main Apr 8, 2026
30 checks passed
piper-of-dawn pushed a commit to piper-of-dawn/zed that referenced this pull request Apr 25, 2026
Closes zed-industries#4751

## Testing
- Manually tested by comparing the behaviors with vscode.
- Those requirements are added to unit tests.

Release Notes:

- Added action to toggle block comments

---------

Co-authored-by: ozacod <ozacod@users.noreply.github.com>
zerowidth added a commit to zerowidth/zed that referenced this pull request Apr 26, 2026
Fixes zed-industries#54737.

PR zed-industries#48752 added empty-prefix `block_comment` entries to several language configs
(Go, C, C++, JSONC, Python, JSX inner) to support the new toggle-block-comments
action. In `Editor::rewrap_impl`, the comment-format matcher used
`buffer.contains_str_at(indent_end, &config.prefix)` to decide whether the
current line is a continuation of a block comment. When the language is
configured with an empty prefix, this is true on every line. `//` (and `#`) line
comments inside a `comment` override scope were classified as `BlockLine("")`
and never reached the line-comment fallback. The result was that the
line-comment prefix was not stripped before wrapping and not re-prepended after,
embedding `//` markers as text in the wrapped paragraph.

Skip the BlockLine arm when the configured prefix is empty so the matcher falls
through to `line_comment_prefixes`.
ADEMOLA200 pushed a commit to ADEMOLA200/zed that referenced this pull request Apr 27, 2026
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 is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Closes zed-industries#54737.

zed-industries#48752 added empty-prefix `block_comment` entries to several language
configs (Go, C, C++, JSONC, Python, JSX inner) to support the new
toggle-block-comments action.

In `Editor::rewrap_impl`, the comment-format matcher used
`buffer.contains_str_at(indent_end, &config.prefix)` to decide whether
the current line is a continuation of a block comment. When the language
is configured with an empty prefix, this is true on every line. `//`
(and `#`) line comments inside a `comment` override scope were
classified as `BlockLine("")` and never reached the line-comment
fallback. The result was that the line-comment prefix was not stripped
before wrapping and not re-prepended after, embedding `//` markers as
text in the wrapped paragraph.

Skip the BlockLine arm when the configured prefix is empty so the
matcher falls through to `line_comment_prefixes`.

I've included regression tests for both golang (which adds a new
treesitter dep to the editor package) and C/C++.

Release Notes:

- Fixed line comment rewrapping in golang and C/C++
ebaah46 pushed a commit to ebaah46/zed that referenced this pull request May 6, 2026
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 is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Closes zed-industries#54737.

zed-industries#48752 added empty-prefix `block_comment` entries to several language
configs (Go, C, C++, JSONC, Python, JSX inner) to support the new
toggle-block-comments action.

In `Editor::rewrap_impl`, the comment-format matcher used
`buffer.contains_str_at(indent_end, &config.prefix)` to decide whether
the current line is a continuation of a block comment. When the language
is configured with an empty prefix, this is true on every line. `//`
(and `#`) line comments inside a `comment` override scope were
classified as `BlockLine("")` and never reached the line-comment
fallback. The result was that the line-comment prefix was not stripped
before wrapping and not re-prepended after, embedding `//` markers as
text in the wrapped paragraph.

Skip the BlockLine arm when the configured prefix is empty so the
matcher falls through to `line_comment_prefixes`.

I've included regression tests for both golang (which adds a new
treesitter dep to the editor package) and C/C++.

Release Notes:

- Fixed line comment rewrapping in golang and C/C++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Block comment command

4 participants