Search code examples
typescriptintellij-ideacode-formattingprettier

Can Prettier rules be applied using IntelliJ IDEA code style rules instead of the Prettier plugin?


I have a project that contains some TypeScript files which I would like to format using the Prettier format while editing in IntelliJ IDEA, so that my resulting code matches that of my colleagues. Ideally, I only want to reformat the portions of the file I'm updating, so that my commits can be focused on my actual changes.

For other projects in IntelliJ IDEA I would typically use the built in code formatting and code style rules to format my code. The built-in TypeScript rules do not match the Prettier rules, and it's unclear whether those rules are published anywhere in an IntelliJ format I could just import.

For this reason, I've investigated the Prettier IntelliJ IDEA plugin. However, using this plugin requires some overhead; most notably, it requires a Node.js installation.

Is there an easy way to just use the Prettier rules using the standard IntelliJ reformatting functionality?


Solution

  • IDEA can import some code style settings (indents and some others) when .prettierrc is present in the project even if the Prettier itself is not installed:

    enter image description here

    But unfortunately it's not possible for us to match Prettier behavior completely because it doesn't have a specification and introduces stylistic changes in patch releases. So in general matching Prettier behavior with the built-in formatter is something that most likely will not happen. It's expected that if someone is using Prettier, they will be either using the Reformat with Prettier action or set up Prettier to run on Reformat Code action (https://www.jetbrains.com/help/idea/prettier.html#ws_prettier_default_formatter).