AppCode 2020.1 Help

Prettier

Prettier is a tool to format .js, .ts, .css, .less, .scss, .vue, and .json code. With AppCode, you can format selected code fragments as well as entire files or directories using the Reformat with Prettier action. AppCode adds this action as soon as you install Prettier as a dependency in your project or globally on your computer.

You can also configure Prettier to reformat specific files every time such file is changed and the changes are saved automatically or manually.

Before you start

  1. Install and enable the Prettier plugin on the Plugins page as described in Installing plugins from repository.

  2. Download and install Node.js.

Install Prettier

  • In the embedded Terminal (⌥F12), type one of the following commands:

    • npm install --save-dev --save-exact prettier
    • npm install --global prettier

    Learn more about installation modes from the Prettier official website.

Configure Prettier in AppCode

  1. In the Preferences dialog ⌘,, go to Languages and Frameworks | JavaScript | Prettier.

  2. Choose the Node.js interpreter to use. If you choose the Project alias, AppCode will automatically use the project default interpreter from the Node interpreter field on the Node.js and NPM page.

    In most cases, AppCode detects the project default interpreter and fills in the field itself. You can also choose another configured local interpreter or click the Browse button and configure a new one.

  3. From the Prettier package list, select the prettier installation to use.

  4. To run Prettier automatically against specific files every time such file is changed and saved, select the Run on save checkbox, and specify the pattern that defines the set of files to be reformatted every time such file is saved.

Reformat code with Prettier

  • In the editor, select the code fragment to reformat and press N/A or select Reformat with Prettier from the context menu. To reformat a file or a folder, select it in the Project tool window and press N/A or select Reformat with Prettier from the context menu.

  • To run Prettier automatically when a file is changed, open the Preferences dialog ⌘,, go to Languages and Frameworks | JavaScript | Prettier, select the Run on save checkbox, and specify the pattern that defines the set of files to be reformatted every time such file is saved.

    With the default pattern, {**/*,*}.{js,ts,jsx,tsx}, Prettier will wake up and process any updated and saved JavaScript, TypeScript, JSX, or TSX file.

Apply Prettier code style rules

AppCode can apply the key code style rules from the Prettier's configuration to the AppCode Code Style settings so that generated code (for example, after refactoring or quick-fix) and the code that is already processed with Prettier are formatted consistently.

  • In the project where Prettier is enabled, open package.json and click Yes in the pane at the top of the tab.

    Pane above package.json: apply Prettier code style

  • To re-apply the Prettier code style (after you've clicked No in the pane or modified the code style), press ⇧⌘A and select Apply Prettier Code Style Rules from the Find Action list.

Learn more about configuring Prettier code style rules from the Prettier official website.

Last modified: 20 May 2020