Skip to content

Gate Format Selections on whether the active formatter can actually format ranges#53178

Merged
SomeoneToIgnore merged 9 commits intozed-industries:mainfrom
prertik:issue-25796-lsp-format
Apr 16, 2026
Merged

Gate Format Selections on whether the active formatter can actually format ranges#53178
SomeoneToIgnore merged 9 commits intozed-industries:mainfrom
prertik:issue-25796-lsp-format

Conversation

@prertik
Copy link
Copy Markdown
Contributor

@prertik prertik commented Apr 5, 2026

What Changed

  • compute range-format support from formatter configuration and language-server capabilities
  • show Format Selections only when at least one selected buffer has a range-capable formatter
  • keep Prettier-supported range formatting available without depending on LSP support
  • hide the action for formatter modes that cannot honor selections, such as external-command and code-action formatters
  • keep the action handler safe by rechecking support and returning early when range formatting is unavailable
  • restrict capability checks to the servers actually registered for the current buffer
  • add regression coverage for supported, unsupported, and mixed-server cases
  • document when Format Selections is available in both action docs and user docs

Self-Review Checklist:

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content is consistent with the UI/UX checklist
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Closes #25796

Release Notes:

  • Improved Format Selections so it is only shown when the active formatter supports range formatting.

Testing I did:

Here are the formatters I used:
Screenshot_20260405_102047

Note: I've chosen three formatters:

  1. Prettier, which supports range formatting
  2. clangd (LSP) which also supports range formatting
  3. gopls which do not support range formatting
Screencast_20260405_102431.webm

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Apr 5, 2026
@zed-codeowner-coordinator zed-codeowner-coordinator Bot requested review from a team, as-cii, cameron1024, nathansobo and smitbarmase and removed request for a team April 5, 2026 04:50
@prertik
Copy link
Copy Markdown
Contributor Author

prertik commented Apr 5, 2026

@SomeoneToIgnore Please review.

@SomeoneToIgnore SomeoneToIgnore self-assigned this Apr 5, 2026
@maxdeviant maxdeviant changed the title Gate Format Selections on whether the active formatter can actually format ranges. Gate Format Selections on whether the active formatter can actually format ranges Apr 5, 2026
@prertik prertik force-pushed the issue-25796-lsp-format branch 2 times, most recently from f115af0 to 216ac53 Compare April 8, 2026 13:17
@SomeoneToIgnore
Copy link
Copy Markdown
Contributor

Sorry, test_sidebar_invariants is bad and is going to be fixed by teammates, hopefully soon, this will require another rebase later.

@prertik
Copy link
Copy Markdown
Contributor Author

prertik commented Apr 8, 2026

Sorry, test_sidebar_invariants is bad and is going to be fixed by teammates, hopefully soon, this will require another rebase later.

Thanks, @SomeoneToIgnore. I was wondering why tests are failing all of a sudden and trying to look into issue without much luck.

@SomeoneToIgnore
Copy link
Copy Markdown
Contributor

Seems to be fixed?
#53421

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, the way to disable the action seems way better than my proposal, but the implementation is quite odd.

Let's clean it up, as it looks we can do it much simpler.

Comment thread crates/editor/src/editor.rs Outdated
Comment thread crates/editor/src/editor.rs Outdated
Comment thread crates/editor/src/editor.rs Outdated
Comment thread crates/editor/src/editor.rs Outdated
Comment thread crates/editor/src/editor.rs Outdated
Comment thread crates/editor/src/element.rs Outdated
Comment thread crates/project/src/lsp_store.rs Outdated
@prertik prertik force-pushed the issue-25796-lsp-format branch from ebf2a32 to 05c5247 Compare April 10, 2026 13:45
@prertik
Copy link
Copy Markdown
Contributor Author

prertik commented Apr 10, 2026

Let's clean it up, as it looks we can do it much simpler.

Hi @SomeoneToIgnore, I've cleaned it up based on your suggestions.

@prertik prertik requested a review from SomeoneToIgnore April 10, 2026 14:34
Comment thread crates/project/src/lsp_store.rs Outdated
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.

I've pushed a few fixes as there was a lot of unneeded allocations all over the place.
There's one very odd place left that we need to discuss.

@prertik prertik force-pushed the issue-25796-lsp-format branch from b59628f to c9a83ba Compare April 12, 2026 13:32
@prertik prertik requested a review from SomeoneToIgnore April 12, 2026 13:33
@prertik prertik force-pushed the issue-25796-lsp-format branch from c9a83ba to e4ab8ee Compare April 12, 2026 13:43
@prertik prertik force-pushed the issue-25796-lsp-format branch from e4ab8ee to 09f1da5 Compare April 12, 2026 13:46
Comment thread crates/project/src/lsp_store.rs Outdated
Comment thread crates/project/src/lsp_store.rs
Comment thread crates/project/src/lsp_store.rs Outdated
@SomeoneToIgnore
Copy link
Copy Markdown
Contributor

SomeoneToIgnore commented Apr 13, 2026

Sorry, lsp_store is a very important module and I want to understand what those changes are meant for: I would appreciate an elaboration on what's going on and the purpose of it all, given the #53178 (comment) agreement that the old code was working ok for our needs.

@prertik
Copy link
Copy Markdown
Contributor Author

prertik commented Apr 13, 2026

Sorry, lsp_store is a very important module and I want to understand what those changes are meant for: I would appreciate an elaboration on what's going on and the purpose of all it, given the #53178 (comment) agreement that the old code was working ok for our needs.

Apologies, I agreed to the change, but my commits during rebase, I added wrong changes and committed them. I'll amend and push again.

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.

Given all the back-and-forth, I'll merge this after the next release this Wednesday, to be on a safe side.

@prertik
Copy link
Copy Markdown
Contributor Author

prertik commented Apr 13, 2026

Thank you.

Given all the back-and-forth, I'll merge this after the next release this Wednesday, to be on a safe side.

Sure, @SomeoneToIgnore. Thanks for reviewing this and giving me feedback.

@SomeoneToIgnore SomeoneToIgnore merged commit b6b2b63 into zed-industries:main Apr 16, 2026
31 checks passed
G36maid pushed a commit to G36maid/zed that referenced this pull request Apr 29, 2026
… format ranges (zed-industries#53178)

## What Changed

- compute range-format support from formatter configuration and
language-server capabilities
- show `Format Selections` only when at least one selected buffer has a
range-capable formatter
- keep Prettier-supported range formatting available without depending
on LSP support
- hide the action for formatter modes that cannot honor selections, such
as external-command and code-action formatters
- keep the action handler safe by rechecking support and returning early
when range formatting is unavailable
- restrict capability checks to the servers actually registered for the
current buffer
- add regression coverage for supported, unsupported, and mixed-server
cases
- document when `Format Selections` is available in both action docs and
user docs


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#25796

Release Notes:
- Improved `Format Selections` so it is only shown when the active
formatter supports range formatting.

Testing I did:

Here are the formatters I used:
<img width="829" height="434" alt="Screenshot_20260405_102047"
src="https://github.com/user-attachments/assets/cae4a238-277e-4188-873f-189e9f098699"
/>

Note: I've chosen three formatters:
1. Prettier, which supports range formatting
2. clangd (LSP) which also supports range formatting
3. gopls which do not support range formatting


[Screencast_20260405_102431.webm](https://github.com/user-attachments/assets/69ce3f95-0b73-46a0-853d-3b5be6329dde)

---------

Signed-off-by: Pratik Karki <[email protected]>
Co-authored-by: Kirill Bulatov <[email protected]>
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.

FormatSelections Command does not work

4 participants