-
-
Notifications
You must be signed in to change notification settings - Fork 470
Add prettier.forceFormatDocument
to code actions
#2500
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
Conversation
@roottool Thank you for taking the initiative to get this long awaited feature in. 🎉 Is there a special reason why you chose to name the action |
@wedi I forgot that I wrote the reason, sorry. It has two reason. First, I wanted to be able to explicitly specify the order of execution. Because I don't know the execution order within
Second, I have decided that
And this comment encouraged me to use
FYI: This prettier-vscode/package.nls.json Line 3 in 991a53f
If you have a good name for it, I'd be glad to hear it! |
@roottool using the standard naming and choosing a custom execution order is not mutually exclusive. You can customize the order of execution by using the full names like this:
"editor.codeActionsOnSave": [
"source.organizeImports",
"source.fixAll.eslint",
"source.fixAll.stylelint"
], As prettier fixes all source [code] styling issues, Personally, I'll be happy with whatever name gets chosen and I'm exited to see this merged. :) |
Using `eslint-plugin-prettier` is slower than running prettier itself, the recommended approach is to run prettier first then follow by eslint to fix all the problems. VScode integration will be resolved once this PR gets merged prettier/prettier-vscode#2500.
@@ -1,10 +1,10 @@ | |||
// Place your settings in this file to overwrite default and user settings. | |||
{ | |||
"editor.codeActionsOnSave": { | |||
"source.prettier.forceFormatDocument": true, |
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.
Is this a new recommended best practice from the VS Code team or just preference?
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.
No, it's not. I'm going to undo this difference if it is not necessary.
This difference has two intent.
First, The intent of this difference is to resolve #1277 by realizing the following quoted portion.
Originally posted by @rohit-gohri in #1277 (comment) is a working solution for running prettier before eslint.
The above solution runs the default formatter (which can be set to Prettier) then uses the code action for eslint afterwards.
#1555 (comment)
Second, it is for testing the created new code action in this PR.
The intention was to make it clear that I was testing through the code action.
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.
It looks like Microsoft's recommended settings
Although you can also use the formatter on save using the setting editor.formatOnSave it is recommended to use the editor.codeActionsOnSave feature since it allows for better configurability.
const FORCE_FORMAT_DOCUMENT_CODE_ACTION_KIND = CodeActionKind.Source.append( | ||
FORCE_FORMAT_DOCUMENT_COMMAND | ||
); | ||
const FORCE_FORMAT_DOCUMENT_TITLE = "Format Document (Forced)"; |
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 is it forced?
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.
Because this code action uses a command of the same name that exists in the command palette.
But I would be willing to adopt any better title. 👍
For example,
const FORCE_FORMAT_DOCUMENT_TITLE = "Format Document (Forced)"; | |
const FORMAT_DOCUMENT_TITLE = "Prettier: Format Document"; |
Could a code action be added for formatting? Such that if a user were to hit ctrl+shift+p in VS Code it would show something like "Prettier: Format Document"
#1555 (comment)
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.
A few comments
I replied to a few comments. But I'm sorry that it took me so long to reply. |
This commit undo two things. - The previous incorrect commit - The `yarn prettier` execution by lint-staged This difference is not focused in this PR.
I kind of missed the "forced" part of this PR. I guess I am confused what the point of this is? Why would you want to always force prettier to format files that you have set to be ignored? |
I think the contributor was re-using an existing command in the project as he mentioned https://github.com/prettier/prettier-vscode/blame/main/package.nls.json#L3 If I understood it correctly, the original commit dcc531f was aimed to provide a way to "Format Document (Forced)" which means 👉 format document specifically with formatter Therefore, the goal of this PR was to re-use the same command so that it can be registered as a CodeAction and allowing users to specify the order of code actions by preference. This ultimately solves the good old "Run Prettier First, then Run ESLint" problem as an alternative approach to doing it via CLI commands or Looking forward to this being merged! |
@nujhong Thank you for the explanation assistance! @ntotten This CodeAction name includes "forced" is because I uniformed the name at an existing command name. The CodeAction re-uses "Format Document (Forced)" command already in this extension. prettier-vscode/package.nls.json Line 3 in b395990
Including the appropriation of this name, the implementation intent is as he commented. |
Any updates? |
I am going to close this. This PR doesn't work and it doesn't have tests. If somebody is interested in contributing this please continue the discussion here: #1555 |
CHANGELOG.md
with a summary of your changesResolves #1555
Overview
This PR adds two changes
prettier.forceFormatDocument
to code actionssettings.json
to this code action additionAdapt
settings.json
to this code action additionI reset
editor.defaultFormatter
and turn offeditor.formatOnSave
. Because this extension is able to format bysource.prettier.forceFormatDocument
ineditor.codeActionsOnSave
without the two items.Run tests screenshot