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

Prettier integration #72

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Prettier integration #72

wants to merge 5 commits into from

Conversation

aabounegm
Copy link
Member

@aabounegm aabounegm commented Jun 19, 2024

This PR implements a hack that would call Prettier's format command after our own formatter completes execution. It also includes a minor refactor and updating deps, but the only relevant commit is 8096181.

Long story

Initially, I tried installing prettier and using its JavaScript API (so that the extension wouldn't even be needed), but I faced issues bundling it with ESBuild. I replaced ESBuild with Rollup and after some config hacks it bundled successfully, but then I faced the most important issue: editing the document in the middleware (even before requesting from the LSP server) causes VS Code to cancel the formatting request, requiring the user to trigger it again.
The best thing to do here is to format the document (in-memory, not on-disk) and then diff the output with the original input and transform that "diff" to LSP's TextEdits, and then merge them with the array returned from our formatter, but that would have been unnecessarily complicated.
The only way around this was to format with Prettier after the server's edits were already applied by VS Code, which means using the ugly setTimeout. Also, I went back on the "not requiring the Prettier extension" part because that felt like forcing Prettier on all users, so instead I decided to check if the prettier.enable setting is true, and then simply invoke command from the Prettier extension itself.

Shortcomings

Since the document is edited (by Prettier) after the requested formatting is complete, it will be left in a dirty state. This is a minor annoyance that is only relevant if the user had the "format on save" option turned on and formatted by saving. I tried working around it by adding vscode.commands.executeCommand("workbench.action.files.saveWithoutFormatting"); after Prettier completes, but that will always force saving even if the user didn't request formatting through saving, which is somewhat intrusive.

@aabounegm aabounegm force-pushed the prettier-integration branch 6 times, most recently from 707b6a1 to 63f1623 Compare June 19, 2024 19:43
@aabounegm aabounegm force-pushed the prettier-integration branch from 63f1623 to 99e6eef Compare June 19, 2024 19:45
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.

1 participant