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

VSCode Extension: Add diagnostics and code actions and format range #1376

Merged
merged 6 commits into from
Nov 20, 2024

Conversation

badsyntax
Copy link
Contributor

@badsyntax badsyntax commented Nov 7, 2024

Fixes #1375
Fixes #659

A lot of this code has been taken from one of my open source extensions.

This approach uses the 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.

Screenshot 2024-11-07 at 21 55 03 Screenshot 2024-11-07 at 22 26 38

@badsyntax badsyntax changed the title Add diagnostics and code actions. Refs #1375 VSCode Extension: Add diagnostics and code actions. Refs #1375 Nov 7, 2024
@badsyntax badsyntax force-pushed the badsyntax/diagnostics branch from cdcc356 to 5ad7971 Compare November 7, 2024 22:09
@badsyntax badsyntax force-pushed the badsyntax/diagnostics branch 2 times, most recently from dda0595 to 6b7d239 Compare November 7, 2024 22:51
@badsyntax badsyntax changed the title VSCode Extension: Add diagnostics and code actions. Refs #1375 VSCode Extension: Add diagnostics and code actions Nov 7, 2024
Copy link
Collaborator

@shocklateboy92 shocklateboy92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for your contribution! It was a pretty neat idea to do the comparison in VSCode instead of CSharpier. It won't help for other IDE integrations, but I suppose this it better than nothing.

You mentioned that it fixes #659, but from looking through the code I only see a new code action provider being created.

Will this code make partial formats happen if

"editor.formatOnSaveMode": "modificationsIfAvailable"

is set?

If not, would you be able to make a simple change to add it?

@badsyntax
Copy link
Contributor Author

Hey, apologies for the delay in responding.

You mentioned that it fixes #659, but from looking through the code I only see a new code action provider being created.

Yes sorry, I have not thought about Format Selection. By providing a QuickFIx CodeAction it's possible to format only parts of the code. While not the same as formating selection it provides a similar feature:

fix_part_code.mov

Saying that, I will investigate to see how complicated it'l be to support Format Selection.

Will this code make partial formats happen if "editor.formatOnSaveMode": "modificationsIfAvailable"

At this time it doesn't support modificationsIfAvailable but I'll see if I can support that.

@badsyntax
Copy link
Contributor Author

OK! With the latest change we can format selection.

We generate the diff, then we compare the differences to check if they're in the range of the selection, and if so, apply the diff change as a TextEdit.

format_selection.mov

@badsyntax badsyntax force-pushed the badsyntax/diagnostics branch from 89e077f to f8256f3 Compare November 17, 2024 18:44
@badsyntax
Copy link
Contributor Author

Also, now that we can format within a range, "editor.formatOnSaveMode": "modificationsIfAvailable" & "editor.formatOnSaveMode": "modifications" is now working:

format_modified.mov

@badsyntax badsyntax force-pushed the badsyntax/diagnostics branch from f8256f3 to 3556933 Compare November 17, 2024 19:22
Copy link
Collaborator

@shocklateboy92 shocklateboy92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you very much for your contribution!

@shocklateboy92 shocklateboy92 enabled auto-merge (squash) November 20, 2024 05:14
@shocklateboy92 shocklateboy92 merged commit 84b775f into belav:main Nov 20, 2024
3 checks passed
@shocklateboy92 shocklateboy92 changed the title VSCode Extension: Add diagnostics and code actions VSCode Extension: Add diagnostics and code actions and format range Nov 20, 2024
@badsyntax badsyntax deleted the badsyntax/diagnostics branch November 24, 2024 14:43
@belav
Copy link
Owner

belav commented Dec 7, 2024

@badsyntax thanks for implementing all of this! Sorry for the delay on releasing it, I published version 1.9.0 just now which contains the changes.

@badsyntax
Copy link
Contributor Author

@belav awesome. Code is pretty easy to follow imo but feel free to ping me for any modifications/support

pisolofin pushed a commit to pisolofin/csharpier-editorconfig that referenced this pull request 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VSCode Exension Proposal: Diagnostics and Code Actions Format only the selected code
3 participants