zed icon indicating copy to clipboard operation
zed copied to clipboard

Format ranges via LSP

Open terziele opened this issue 1 year ago • 7 comments

Release Notes:

  • Added range formatting

terziele avatar Oct 04 '24 20:10 terziele

Discussed with @ConradIrwin here

  • https://github.com/zed-industries/zed/pull/16823

terziele avatar Oct 04 '24 20:10 terziele

It's also possible to make a range formatting with Prettier if needed.

terziele avatar Oct 04 '24 20:10 terziele

We landed some other changes in the meantime, can you rebase?

Edit: or merge main into this!

mrnugget avatar Oct 09 '24 17:10 mrnugget

Wow, rebase made this messy. How can I fix that?

terziele avatar Oct 09 '24 19:10 terziele

Reset onto the upstream main and cherry pick your commit

WeetHet avatar Oct 09 '24 19:10 WeetHet

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

notpeter avatar Oct 09 '24 20:10 notpeter

Thanks, it helped. :)

terziele avatar Oct 09 '24 21:10 terziele

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 avatar Oct 10 '24 05:10 mrnugget

@mrnugget Yes, rust-analyzer supports range format only on nightly. I used C++ LSP for testing.

terziele avatar Oct 10 '24 06:10 terziele

@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

WeetHet avatar Oct 10 '24 07:10 WeetHet

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

mrnugget avatar Oct 10 '24 07:10 mrnugget

@WeetHet Maybe it would be better not to show the button in a mouse context menu if range formatting is not possible?

terziele avatar Oct 11 '24 05:10 terziele

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? image

terziele avatar Oct 11 '24 21:10 terziele

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 avatar Oct 14 '24 07:10 mrnugget

@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.

terziele avatar Oct 14 '24 17:10 terziele

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.

mrnugget avatar Oct 16 '24 13:10 mrnugget

It looks like https://github.com/zed-industries/zed/issues/5026 can be closed.

hferreiro avatar Oct 16 '24 14:10 hferreiro

@mrnugget Great! Do we need a prettier support here?

terziele avatar Oct 16 '24 18:10 terziele

If it's possible, that would be neat, yep

mrnugget avatar Oct 17 '24 15:10 mrnugget

editor: format selections is formatting the whole file instead of formatting just the selection.

4nur4g avatar Dec 26 '24 08:12 4nur4g

What language server are you using, @4nur4g?

mrnugget avatar Jan 07 '25 10:01 mrnugget

@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
      }
}}

4nur4g avatar Jan 07 '25 13:01 4nur4g

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 avatar Jan 08 '25 15:01 mrnugget

@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.

4nur4g avatar Jan 09 '25 06:01 4nur4g

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 avatar Jan 09 '25 10:01 mrnugget

@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 avatar Jan 09 '25 12:01 hferreiro

@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.

mrnugget avatar Jan 09 '25 16:01 mrnugget

Also, are you sure the LSP is configured as formatter?

mrnugget avatar Jan 09 '25 16:01 mrnugget

@mrnugget Changing formatter to vtsls worked.

4nur4g avatar Jan 10 '25 12:01 4nur4g

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.

hferreiro avatar Jan 13 '25 10:01 hferreiro