Skip to content

danbrycefairsailcom/prettier-plugin-apex

 
 

Repository files navigation

Prettier Apex

This is a modified version of the Prettier Apex plugin to better suit our current coding conventions.

See the original documentation here.

Changes

These are the changes from the original project. Each one is configurable with an option in .prettierrc.

Empty line at the start of a class / interface / trigger

Option: apexBeginClassWithEmptyLine (boolean)

Before

public class Potato {
    private int size;

    ...
}

After

public class Potato {

    private int size;

    ...
}

Use additional indent for continuation lines

Option: apexUseContinuationIndent (boolean)

Before

doSomething(paramA,
    paramB,
    paramC);

After

doSomething(paramA,
        paramB,
        paramC);

No newline before conditional expressions, in if-statements and loops

Option: apexSkipNewlineBeforeCondition (boolean)

Before

if(
        conditionA
        && conditionB
        && conditionC) {
    ...
}

After

if(conditionA
        && conditionB
        && conditionC) {
    ...
}

No newline before closing parenthesis, for statements with a body

Option: apexSkipNewlineBeforeClosingParenthesis (boolean)

Before

private void doSomething(
        Potato p1,
        Potato p2,
        Potato p3
) {
    ...
}

After

private void doSomething(
        Potato p1,
        Potato p2,
        Potato p3) {
    ...
}

Note that the "Before" style is still permitted for statements without a body, e.g. return / assignment statements.

Still To Do

  • Newline after /* in block comments
  • Support continuation lines inside a long expression
    • if-statements
    • assignment statements
  • Line breaks between methods
  • Line breaks BEFORE operators

Developing the Plugin

Setup

  1. From the prettier-plugin-apex repo:

    npm link
    
  2. From the repo to be formatted:

    npm link prettier-plugin-apex
    
  3. Enable "Format on Save".

    You can also format manually using: Ctrl + Shift + P => Format Document

Testing Changes

After making a change to the Prettier plugin, it must be reloaded using:

Ctrl + Shift + P => Reload Window

Note that it can take some time for Prettier to initialise.

Unit Tests

To run linting and unit tests:

npm test

To run just the unit tests:

npx jest

Useful Links

About

Code formatter for the Apex Programming Language

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 64.5%
  • Apex 34.9%
  • Other 0.6%