Skip to content

Latest commit

 

History

History
97 lines (72 loc) · 3.56 KB

advanced_usage.md

File metadata and controls

97 lines (72 loc) · 3.56 KB

Advanced Usage

Configuration

You can customize some options to meet your needs. To do so, add overrides to the .prettierrc.yml file that you created at the root of your project:

# Prettier configuration
plugins:
  - prettier-plugin-java
overrides:
  - files:
      - "*.java"
    options:
      printWidth: 140
      tabWidth: 4
      useTabs: false
      trailingComma: "none"

Please refer to the Prettier configuration documentation for more information.

Pre-commit hooks:

To share your Git Hooks, we would suggest to follow this procedure:

  1. Create a package.json file. For instance:
    echo "{}" > package.json
  2. Install Husky, Lint-staged, Prettier, the Prettier-Java plugin locally npm install --save-dev husky lint-staged prettier prettier-plugin-java
  3. Add the following to your package.json:
    "husky": {
      "hooks": {
        "pre-commit": "lint-staged"
      }
    },
    "lint-staged": {
      "*.{,java}": [
        "prettier --write",
        "git add"
      ]
    }

Please refer to the Prettier Pre-commit Hook documentation for more information.

IDE Integrations

There are currently no IDE extensions that directly invoke Prettier-Java. However, for VSCode and IntelliJ, this can be configured manually.

Prettier plugin

The easiest way to use Prettier-Java in your IDE is to install it locally in your project (npm install --save-dev --save-exact prettier prettier-plugin-java) and install the Prettier extension related to your IDE.

NOTE: it is not possible to invoke Prettier-Java in your IDE without creating a package.json file unless you use an alternative to npx like prettier-pnp due to the way plugins are loaded. Further, Prettier recommends that the exact version of prettier be set and locked on a per-project basis, as formatting preferences change from version to version.

Please refer to the Prettier Editor integration documentation for more information.

FileWatcher plugin

You can also use File Watcher to reformat your code on save. We will describe the procedure to to this in the VSCode and IntelliJ based IDEs.

VSCode

In VSCode, install the File Watcher extension, and add to your settings, if you have installed the prettier and prettier-plugin-java packages locally:

"filewatcher.commands": [
  {
    "match": "\\.java",
    "isAsync": true,
    "cmd": "npx prettier --write ${file}",
    "event": "onFileChange"
  }
]

If you have installed the prettier and prettier-plugin-java packages globally, replace npx prettier --write ${file} command by prettier --write ${file}.

IntelliJ based IDEs:

Install the Prettier, Prettier-Java, and your .prettierrc.yaml file as previously described.

Open your Preferences. Then, go to the Tools/File Watchers section and create a Watcher. To configure it, fill the form with these values:

  • Name: Prettier Java
  • File Type: Java
  • Scope: Project Files
  • Program: npx
  • Arguments: prettier --write $FilePath$
  • Output path to refresh: $FilePath$
  • Auto-save edited files to trigger the watcher: unchecked
  • Trigger the watcher on external changes: checked

Please refer to the Prettier "Using File Watchers" documentation for more information.