Skip to content
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

Closed
danijelh opened this issue May 5, 2022 · 12 comments · Fixed by #662 or #1376
Closed

Format only the selected code #659

danijelh opened this issue May 5, 2022 · 12 comments · Fixed by #662 or #1376
Assignees

Comments

@danijelh
Copy link

danijelh commented May 5, 2022

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! 🤞

@belav
Copy link
Owner

belav commented May 23, 2022

I think I accidentally referenced this in a commit message.

@Exiled1
Copy link

Exiled1 commented Aug 9, 2023

I'm interested in this specific enhancement, can I ask roughly what would be needed to implement this?

@belav
Copy link
Owner

belav commented Aug 9, 2023

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.
My thought would be that this requires sending all of the code to csharpier, and then determining which part of that maps to the selection in the editor. I'm not really sure how you'd do that mapping though.

@Exiled1
Copy link

Exiled1 commented Aug 9, 2023

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.

@belav
Copy link
Owner

belav commented Aug 9, 2023

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

@Exiled1
Copy link

Exiled1 commented Aug 9, 2023

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 window.createOutputChannel("Csharpier Warnings/Errors") function to create an output channel and then just use the appendLine and show functions to output it to the window.

If I understand correctly would I just need to change the FormattingService.ts file and create/throw errors there? Or would you like me to do it some other way? Would I need to change other files?

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.

@Exiled1
Copy link

Exiled1 commented Aug 10, 2023

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

@belav
Copy link
Owner

belav commented Aug 11, 2023

You are correct that the changes would go into FormattingService.cs. I don't recall how to support formatting a selection, it may require telling VSCode that the formatter supports it.

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.

@Meligy
Copy link

Meligy commented Jun 10, 2024

Hello. Is this enhancement still on the table? Thanks a lot.

@Meligy
Copy link

Meligy commented Jun 10, 2024

Also, how is supposed to work with "editor.formatOnSaveMode": "modificationsIfAvailable" ? Thanks.

@gpshonik
Copy link

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?

@shocklateboy92
Copy link
Collaborator

PRs welcome 😉

pisolofin pushed a commit to pisolofin/csharpier-editorconfig that referenced this issue Dec 14, 2024
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment