-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
Format only the selected code #659
Comments
I think I accidentally referenced this in a commit message. |
I'm interested in this specific enhancement, can I ask roughly what would be needed to implement this? |
CSharpier can only format code that can be parsed into a syntax tree and a selection of code from a file may not fit that criteria. |
Question, would it be possible to just send an error saying that "this selected code is not a complete statement" or something like that to at least make it so a user can select syntax tree parsable code. For the VSCode portion of the plugin you can grab lines of code from a start to end range and from my understanding pass that to csharpier. Or would that not work for selections? This won't work for incomplete code blocks like if you wanted to format the start of a function to halfway into the function, but if you wanted to format the function I believe this would work as it would parse down to a valid syntax tree. However I don't quite know how you feel about incomplete formatting or throwing errors for incomplete selections. |
Right now when plugins integrate with CSharpier they stream the file contents to it, and read the output back. A successful format writes to stdout. If something is written to stdout I believe it assumes that formatting failed because the file could not be compiled. Modifying the vscode plugin to do what you suggested, when selected text is streamed to CSharpier and it can't be formatted, then the plugin could display some sort of warning to the user. That does seem much easier than trying to map a chunk of code that was formatted. I don't think this would require any changes to CSharpier itself, just on the plugin side. I've also been meaning to figure out how to give feedback to a user. I always work with format on save, and sometimes don't realize that the reason my file isn't formatting is because it can't be compiled. Also on the list is getting a better way to communicate between csharpier and the plugins, because I believe they interpret any type of error from csharpier as "file couldn't be formatted because it can't compile". |
That sounds reasonable, if we send an error over to the plugin side then at least on the VS Code side this sounds relatively simple, since you should just be able to use the If I understand correctly would I just need to change the As you've said this method wouldn't change anything for the Csharpier process, however you pointed out that currently the process just assumes that not receiving new text is indicative of an error with compilation of the file so how would you propose throwing an error/warning from Csharpier? For the time being it seems like we could throw a catchall error that says "selected file/selection not valid for formatting" or some other more descriptive warning or error. Currently I'm thinking of making this change for VSCode since I don't know what the APIs for other code editors do, so that would at least be an MVP for the feature. |
I'd like to implement this. So I'll assign it to myself 😺 🦀. However I would like to ask for your opinion on what to do for the time being with the errors thrown, as I feel like implementing an error handling would be out of scope for this specific issue. Would it be alright with you if I just change the error that's currently being thrown to include "or selected text region is not parsable C#" or something similar and throw that out to the VSCode warnings? .assign |
You are correct that the changes would go into For passing errors between CSharpier and the plugins, it would require changing so using something like protobuf to stream data back and forth. The plugins are versioned independently which complicates things. They would need to understand that version X.Y or higher, use protobuf, lower than X.Y use the existing method. That requires getting all the plugins working with it, then releasing the plugins, then releasing the new version of CSharpier. The current behavior just logs to an output window if I remember correctly. It may make more sense to be a bit more "in your face" if someone selects code and tries to format it but the formatting doesn't happen. I don't know what other options there are for notifications to a user, and you wouldn't want to be too obnoxious about it. Logging works for now unless you want to figure out some other way to notify them. |
Hello. Is this enhancement still on the table? Thanks a lot. |
Also, how is supposed to work with |
Since the format range feature from PR #1376 was merged recently 🥳 and is available in the VS Code extension, are there any plans to add that feature to the Visual Studio 2022 extension as well? |
PRs welcome 😉 |
Fixes belav#1375 Fixes belav#659 A lot of this code has been taken from [one of my open source extensions](https://github.com/badsyntax/vscode-spotless-gradle). This approach uses the [prettier-linter-helpers](https://www.npmjs.com/package/prettier-linter-helpers) package to generate diffs of strings (unformatted code against formatted code) which is used to provide diagnostic information and code actions. This allows for formatting parts of code. <img width="1479" alt="Screenshot 2024-11-07 at 21 55 03" src="https://github.com/user-attachments/assets/93e643fc-91c6-4684-882f-3445128b7580"> <img width="547" alt="Screenshot 2024-11-07 at 22 26 38" src="https://github.com/user-attachments/assets/905cb8f6-87d7-499b-83ca-d470cc6e9d44"> --------- Co-authored-by: Richard Willis <[email protected]> Co-authored-by: Lasath Fernando <[email protected]>
Hello team, here with a feature request.
In most cases, we don't want to format the whole class but certain complex parts of the code. With this in mind, it would be super useful if formatting would be possible only on the selected part of the code.
Please look into the possibility of this implementation.
Thanks in advance! 🤞
The text was updated successfully, but these errors were encountered: