prettier-vscode icon indicating copy to clipboard operation
prettier-vscode copied to clipboard

Add `prettier.forceFormatDocument` to code actions

Open roottool opened this issue 2 years ago • 7 comments

  • [x] Run tests
  • [x] Update the CHANGELOG.md with a summary of your changes

Resolves #1555

Overview

This PR adds two changes

  • Add prettier.forceFormatDocument to code actions
  • Adapt settings.json to this code action addition

Adapt settings.json to this code action addition

I reset editor.defaultFormatter and turn off editor.formatOnSave. Because this extension is able to format by source.prettier.forceFormatDocument in editor.codeActionsOnSave without the two items.

Run tests screenshot

Run tests screenshot

roottool avatar Apr 03 '22 20:04 roottool

@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 source.prettier.forceFormatDocument? I am asking because it looks like there is a de facto standard to use fixAll.<productname>. This is used in Microsoft's own eslint extension and stylelint mentions in its docs that you can enable all auto fixing by setting "source.fixAll": true.

wedi avatar Apr 14 '22 21:04 wedi

@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 "source.fixAll": true. A codeaction that is added by this PR must be able to run from Prettier to AutoFix by ESLint in that order.

The above solution runs the default formatter (which can be set to Prettier) then uses the code action for eslint afterwards. #1555

Second, I have decided that Source is more appropriate. Because I have determined that a statement ignoring the formatter's settings is not equivalent to a linter error.

Fix all actions automatically fix errors that have a clear fix that do not require user input. VS Code API Docs - SourceFixAll

And this comment encouraged me to use Source.

For exposing actions for the CodeActionsOnSave option, the plugin needs to provide a Code Action by using the CodeAction API, and especially a CodeActionKind of type "source" https://github.com/prettier/prettier-vscode/issues/1555#issuecomment-1086888406

FYI: This codeaction names were appropriated from the existing command name. Since this codeaction uses that the existing command. https://github.com/prettier/prettier-vscode/blob/991a53f27df9f6d2abc782d8065512310c120983/package.nls.json#L3 If you have a good name for it, I'd be glad to hear it!

roottool avatar Apr 18 '22 02:04 roottool

@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:

settings.json:

  "editor.codeActionsOnSave": [
    "source.organizeImports",
    "source.fixAll.eslint",
    "source.fixAll.stylelint"
  ],

As prettier fixes all source [code] styling issues, source.fixAll.prettier sounds reasonable to me but I am not further involved in this project. I just wanted to point out that there is a naming convention which you did not follow.

Personally, I'll be happy with whatever name gets chosen and I'm exited to see this merged. :)

wedi avatar Apr 21 '22 20:04 wedi

I replied to a few comments. But I'm sorry that it took me so long to reply.

roottool avatar Jun 24 '22 02:06 roottool

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?

ntotten avatar Jul 21 '22 17:07 ntotten

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 https://github.com/prettier/prettier-vscode/commit/dcc531f6382fe0580edbf62386fc48ca233e9c2e was aimed to provide a way to "Format Document (Forced)" which means 👉 format document specifically with formatter esbenp.prettier-vscod

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 eslint-plugin-prettier.

Screen Shot 2022-07-25 at 9 45 24 pm

Looking forward to this being merged!

nujhong avatar Jul 25 '22 11:07 nujhong

@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. https://github.com/prettier/prettier-vscode/blob/b395990a96e1cbd754646de0a2f21c4b36593cd3/package.nls.json#L3

Including the appropriation of this name, the implementation intent is as he commented. I have no intention of enforcing the format on me. I just appropriated the name.

roottool avatar Jul 25 '22 13:07 roottool

Any updates?

nd0ut avatar Dec 28 '22 12:12 nd0ut

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: https://github.com/prettier/prettier-vscode/issues/1555

ntotten avatar Jan 11 '23 17:01 ntotten