-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support BSP formatting #2134
Comments
Hi @Gedochao @MaciejG604! |
Hi @danielleontiev, I wanted to discuss your intentions regarding the implementation of the For the In the first approach, the |
Hi! The pros are
I suspect that the most common use-case for the feature is formatting individual files from editor and the target opened file may be unsaved when user triggers the formatting, so I agree with you point, probably, we need to consider adding something like The more simple but less user-friendly option would be to forbid user to format unsaved files when formatting is backed by BSP, but it's of course not perfect from the UX perspective |
Thank you for the explanation. I will need to discuss this internally, and I will provide a response tomorrow. |
We discussed it internally and there are some issues with the approach. If you format things in the background the editor might get out of sync very easily and you will get local conflicts. Instead it would be best to use TextEdits (or a single one for the entire file), which can be forwarded to the editor. We could run format for a single file in that case, but not for the entire workspace since this would require opening all the files that changed. I wonder if this is not a bit of an overengineered approach. What we could do instead in Metals is just default to You can also set a global config for your script workspaces to avoid having the scalafmt.conf file near them. I understand this required a lot of work and I should have mentioned my reservations earlier, which I am very sorry about. But all the current tools use scalafmt API for formatting, so using it from Metals is really exactly the same as adding BSP format request and doing it via Scala CLI. |
Ok, no problem!
I am not against this one but it seems to be a build-tool aware approach which is not something necessary bad but something initially we've tried to avoid while discussing scalameta/metals-feature-requests#334. I think that setting a global config for script workspaces path as you mentioned is a good option |
I added the detection of .scalafmt file in .scala-build by default. scalameta/metals#5559 |
When editing
scala-cli
scripts in a text editor with enabled Metals, attempting to invoke the "format file" feature will lead to Metals asking to create a.scalafmt.conf
file because Metals usesscalafmt
under the hood to perform formatting.Metals asks to provide formatting configuration
On the other hand,
scala-cli
is capable of formatting Scala sources usingscalafmt
. Unlike plainscalafmt
,scala-cli
provides reasonable defaults and therefore does not require the creation of a.scalafmt.conf
configuration file to work.So, I originally created the following issue in Metals: scalameta/metals-feature-requests#334. In this issue, I initially proposed invoking
scala-cli fmt
from the Metals side ifscala-cli
is used as the BSP server. This would allow formatting files without creating a.scalafmt.conf
configuration.Instead, I was offered to extend the BSP protocol to natively support formatting driven by the BSP server. This would avoid special-casing build tools on the Metals side. An ongoing discussion is taking place on how to support the change in the protocol: build-server-protocol/build-server-protocol#459
Currently, two approaches have been suggested:
buildTarget/format
method in BSP. The protocol changes for this method have been implemented in the aforementioned PR (feat: add buildTarget/format method and canFormat capability build-server-protocol/build-server-protocol#459)textDocument/format
method in BSP. Unlike the previous method, this method operates on an arbitrary set of files/directories inside the workspace (buildTarget/format
operates onbuildTarget
).Both approaches have pros and cons, and since there is still no consensus in the discussion, I have created draft implementations for both. I kindly ask for your feedback because I hope you will be able to provide insights that will help to choose the correct approach.
The text was updated successfully, but these errors were encountered: