Format ranges via LSP
Release Notes:
- Added range formatting
Discussed with @ConradIrwin here
- https://github.com/zed-industries/zed/pull/16823
It's also possible to make a range formatting with Prettier if needed.
We landed some other changes in the meantime, can you rebase?
Edit: or merge main into this!
Wow, rebase made this messy. How can I fix that?
Reset onto the upstream main and cherry pick your commit
Wow, rebase made this messy. How can I fix that?
git checkout main
git pull
git branch -D formatting-selections
git checkout -b formatting-selections
git cherry-pick 8b69e51
git push -f
Thanks, it helped. :)
How do I test this out? With which language server? I tried it with rust-analyzer and it didn't work:
https://github.com/user-attachments/assets/666e2c22-1545-44e5-839d-cbf40ec5ae44
@mrnugget Yes, rust-analyzer supports range format only on nightly. I used C++ LSP for testing.
@mrnugget Yes, rust-analyzer supports range format only on nightly. I used C++ LSP for testing.
Could we handle the error if range formatting is not supported and show something in the activity indicator or in a notification
Could we handle the error if range formatting is not supported and show something in the activity indicator or in a notification
We have this, which could be used: https://github.com/zed-industries/zed/blob/fe1078ef685d4e34ea079f779c2ac0c87ca806c9/crates/activity_indicator/src/activity_indicator.rs#L344-L358
@WeetHet Maybe it would be better not to show the button in a mouse context menu if range formatting is not possible?
It seems that activity indicator was broken by ResultExt::log_err(), which consumes the error.
And also I can't open log from activity indicator, because log file doesn't exists. Is it possible that log file is missing because of debug build?
It seems that activity indicator was broken by
ResultExt::log_err(), which consumes the error.
Are you sure that you add the logging failure to the last_format_failure in the LspStore?
https://github.com/zed-industries/zed/blob/ec5d6e96bb304886f49680b54e58c9b0e7221cd6/crates/project/src/lsp_store.rs#L5106-L5113
@mrnugget Correct me if I'm wrong, but as I understood, the provided piece of code is already processing the result of format operation.
I've removed the log_err().flatten() from the LspStore, because it log_err() consumes the Result::Err and returning the Option<T> instead. Because of this, the format operation never fails, and returns Ok, so last_formatting_failure is never replaced.
Alright! I tried it out locally, using clangd (which works), using rust-analyzer, using a custom formatter. I think everything works as expected.
Good stuff, thank you! I pushed some minor cosmetic changes and will merge once build is green.
It looks like https://github.com/zed-industries/zed/issues/5026 can be closed.
@mrnugget Great! Do we need a prettier support here?
If it's possible, that would be neat, yep
editor: format selections is formatting the whole file instead of formatting just the selection.
What language server are you using, @4nur4g?
@mrnugget I'm using the default language server for javascript. I haven't modified it.
{"JavaScript": {
"language_servers": ["!typescript-language-server", "vtsls", "..."],
"prettier": {
"allowed": true
}
}}
Does that show an error message? Does it support formatting via ranges? See discussion, that includes information that not all language servers support the feature.
@mrnugget , I don’t see any error on the UI. However, upon checking the logs, I found this:
2025-01-09T12:23:43.552048+05:30 [INFO] Language server with id 20 sent unhandled notification eslint/status:
{
"uri": "file:///Users/anuragpradhan/Desktop/pb/posp_api_node/src/database/services/rmActivity/rmActivity.js",
"state": 1,
"validationTime": 38
}
Could you please share the reference to the discussion? I went through the current page and searched for similar issues but couldn’t find whether inline formatting is supported by the default JavaScript language server.
I meant these comments up here: https://github.com/zed-industries/zed/pull/18752#issuecomment-2404160498
I just tested this myself and seems like for TypeScript the default formatter is prettier. Change it to vtsls and it should work:
{
"languages": {
"TypeScript": {
"formatter": [{ "language_server": { "name": "vtsls" } }]
}
}
}
We don't have range-based formatting for Prettier.
@mrnugget there are comments related to this at https://github.com/zed-industries/zed/issues/22816#issuecomment-2579950071. I'm getting non-selected lines formatted with clangd.
https://github.com/user-attachments/assets/5161c4b6-1e37-4813-8b14-63fecfb61b53
@hferreiro are you using remoting? are you in a multi-buffer? do you use collab? I can't reproduce the issue with clang exactly like you have it. There is an issue where it would sometimes format block-wise, but that seems different.
Also, are you sure the LSP is configured as formatter?
@mrnugget Changing formatter to vtsls worked.
are you using remoting? are you in a multi-buffer? do you use collab?
Local file, single buffer, no collaboration.
Also, are you sure the LSP is configured as formatter?
I had no setting. But, after adding "formatter": "language_server", the result is the same.