-
Notifications
You must be signed in to change notification settings - Fork 126
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
Better ocamlformatrpc integration #549
Conversation
Signed-off-by: Paul-Elliot <[email protected]>
Signed-off-by: Paul-Elliot <[email protected]>
Signed-off-by: Paul-Elliot <[email protected]>
The way this works is by sending formatting snippets, but the end result isn't necessarily formatted. I'm curious if you tested this out, does it seem to work well in practice? |
let+ formattedText = | ||
Ocamlformat_rpc.(format_type state.ocamlformat_rpc ~typ:newText) | ||
in | ||
match formattedText with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatted_text
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update my variable names. Should newText
also be renamed to new_text
, or has this variable a reason to be formatted in camelCase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
camelCase naming is fine if used later for punning, I believe
| Ok res -> Some (code_action_of_case_analysis doc uri res) | ||
| Ok (loc, newText) -> ( | ||
let+ formattedText = | ||
Ocamlformat_rpc.(format_type state.ocamlformat_rpc ~typ:newText) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need the module open? I think Ocamlformat_rpc.format_type
should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, it is enough. I don't remember why I opened it.
Some (code_action_of_intf doc intf params.range) | ||
| Intf -> ( | ||
let* intf = Inference.infer_intf ~force_open_impl:true state doc in | ||
let+ formattedIntf = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatted_intf
It works, I would say, better than without formatting, even though a subsequent call to ocamlformat for the whole file is needed to have the right formatting. However, I don't think automatically formatting the whole file is good for user experience, as it modifies the file at position far from the cursor without telling the user. As a comparison, emacs does the same (formatting the generated code only), but is better just in the fact that it indent well the generated lines (but I think this is thanks to tuareg-mode and not merlin-related). |
Just to be sure, it seems that when using ocamlformat rpc the version put in Screen.Recording.2021-11-18.at.14.22.33.mov |
Merged after a few tweaks. |
AFAIU, |
panglsed can keep working on it in a separate PR. Even without the configuration, it's still an improvement over no formatting at all.
Rudi.
…On Nov 22, 2021, 7:03 AM -0600, Ulugbek Abdullaev ***@***.***>, wrote:
AFAIU, Ocamlformat_rpc.configure fn, which is called in Ocamlformat_rpc.(create : unit -> t), specifies formatting config tailored to suit "vscode popups", which isn't what we want when we format source files (we want to get the config from the .ocamlformat file). Shall we revert and let @panglesd continue working on the PR or have a follow up PR?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
I think you're missing that this PR formats source files as well, and the current master is pretty much unusable since any source file reformatted incorrectly on save (if they have formatting on save on). @panglesd do you have time to fix that? |
Sure, I'll try to fix this soon (I hope by tomorrow evening) |
We'll give you the week :) |
I am not anymore sure that we should format the entire file with As I start to think that there are more disadvantages in using ocamlformat_rpc instead of calling ocamlformat directly, as we need to reinvent the wheel for two reasons: sending What do you think? Should I revert back to |
IMO this is a bug in ocamlformat_rpc's server. I don't see how it makes sense for their rpc server to just ignore the .ocamlformat file. It's clearly an implementation detail when using the binary, so why is using the protocol any different?
Rudi.
…On Nov 23, 2021, 11:41 AM -0600, panglesd ***@***.***>, wrote:
I am not anymore sure that we should format the entire file with ocamlformat_rpc.
As ocamlformat_rpc acts completely independently from the .ocamlformat file, we need to find and parse this file, to send to the ocamlformat_rpc server all the options of .ocamlformat.
The problem is that ocamlformat_rpc does not have a counterpart to the "version" value in the .ocamlformat file: To fail on wrong versions, we would also need to parse the file to find the required version, call ocamlformat-rpc --version to know the installed version, compare the two and fail if they differ.
I start to think that there are more disadvantages in using ocamlformat_rpc instead of calling ocamlformat directly, as we need to reinvent the wheel for two reasons: sending .ocamlformat options, and compare .ocamlformat version with installed version. Thats' a lot!
What do you think? Should I revert back to ocamlformat for formatting whole file? Or should continue the ocamlformat_rpc way? (I have something working except for the version comparison)
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
That indeed seems limitation on the ocamlformat-rpc side, which should be fixed. I don't know how much we would like to handle
|
I don't mind adding these features but I want all the heavy lifting to be on the ocamlformat side through rpc. It's their format and its interpretationshould be a black box to us as much as possible.
That's something that should be done through dune. |
I will make a PR reverting file formatting to using ocamlformat, and file an issue to the ocamlformat repo about using |
Instead of reverting you can also add a flag to disable it. Might save the effort of having to reintroduce the patch after the issue is fixed upstream. |
This PR addresses issue #495.