editor: Add action to toggle block comments#48752
editor: Add action to toggle block comments#48752SomeoneToIgnore merged 28 commits intozed-industries:mainfrom
Conversation
|
Formatted, ready for rerun. |
|
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 |
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. |
|
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.mp4got: Screencast.From.2026-02-10.15-15-37.mp4as you can see, I run |
|
@niekdomi Thanks a lot, fixed it for gb$, gbc and gbe. |
|
There's another bug (I think this wasn't an issue before). running This seems also to happen with |
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, |
|
Tested following keybindings in vim mode. "gbc, 5gbc, gbe, gbw, gbj, gbk, gb5j, gb$, gb0, gbG, gbgg, gb%", some edge cases too. |
|
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 |
|
Seems like this fix also fixed |
SomeoneToIgnore
left a comment
There was a problem hiding this comment.
Thank you, seems that we need to test it a bit more, I've also left a few more code-related comments.
…sion and add Unicode tests
…s for visual line mode
…ck_comments; handle visual line mode in vim layer
|
Hi @ConradIrwin,
|
|
@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. |
SomeoneToIgnore
left a comment
There was a problem hiding this comment.
Checked with Conrad and seems we're good to move on, thank you for fixing this long-awaited issue!
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>
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`.
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++
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++



Closes #4751
Testing
Release Notes: